postgresql数据库shape字段转C++ shape对象

1.数据库查询shape的文本字符串

SELECT REPLACE(REPLACE(CAST(st_astext(shape) AS TEXT),'LINESTRING ( ',''),')','') shape from road ORDER BY road_ ;

2.文本字符串转 vector<point>
CreatePointVec(c[0].as<string>().c_str(), my_road.m_vPoints);

void RoadData::CreatePointVec(CString XY_str, vector<CAutoPoint> &vecPoint)
{
 vector<CString> v_point = SplitCString(XY_str, ",");

 int n_of_point = v_point.size();

 for (size_t i = 0; i < n_of_point; i++)
 {
  vector<CString> temp_xy = SplitCString(v_point[i], " ");

  double x = atof(temp_xy[0]) * 3600;
  double y = atof(temp_xy[1]) * 3600;
  CAutoPoint pt(x, y);

  vecPoint.push_back(pt);
  //printf("%f,%f;", x, y);
 }
}


vector<CString> RoadData::SplitCString(CString strSource, CString ch)
{
 vector<CString> vecString;
 int iPos = 0;
 CString strTmp;
 strTmp = strSource.Tokenize(ch, iPos);
 while (strTmp.Trim() != _T(""))
 {
  vecString.push_back(strTmp);
  strTmp = strSource.Tokenize(ch, iPos);
 }
 return vecString;
}


3.点集合转shape

SHPObject*  m_pShapeObject = points2obj(RoadLinkDBF.m_vPoints, SHPT_ARC);

SHPObject * CAutoMap::points2obj(vector<CAutoPoint> v_point, int TYPE)
{

 int n_of_point = v_point.size();

 double* padfX = new double[n_of_point];
 double* padfY = new double[n_of_point];

 for (int i = 0;i < v_point.size(); i++)
 {
  padfX[i] = v_point[i].x;
  padfY[i] = v_point[i].y;
 }

 SHPObject * result_shape = SHPCreateSimpleObject(TYPE, n_of_point, padfX, padfY, NULL);

 delete []padfX;
 delete []padfY;

 return result_shape;
}

原文地址:https://www.cnblogs.com/roea1/p/13284045.html