OGR 写点线面对象和单独的dbf文件

//写点对象
void  saveNodeShp(string path)
{
 if (m_NodeTopologyvec.size() == 0)
 return;

 int nodenum = m_NodeTopologyvec.size();

 const char *pszDriverName = "ESRI Shapefile";
 OGRSFDriver *poDriver;

 OGRRegisterAll();

 poDriver = (OGRSFDriver *)OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
 if (poDriver == NULL)
 {
  printf("%s driver not available.\n", pszDriverName);
  exit(1);
 }
 OGRDataSource *poDS;
 //poDS = poDriver->CreateDataSource("road.shp", NULL);

 string shapename = path + "\\node.shp";
 poDS = poDriver->CreateDataSource(shapename.c_str(), NULL);

 if (poDS == NULL)
 {
  printf("Creation of output file failed.\n");
  exit(1);
 }
 
 OGRLayer *poLayer;
 poDS->CreateLayer("node", NULL, wkbPoint, NULL);
 poLayer = NULL;
 poLayer = poDS->GetLayer(0);//shp文件只有一个图层;
 if (poLayer == NULL)
 {
  printf("Layer creation failed.\n");
  exit(1);
 }

 OGRFieldDefn Field01("jdid", OFTInteger); Field01.SetWidth(32);
 OGRFieldDefn Field02("jdlxdm", OFTInteger); Field02.SetWidth(32);
 OGRFieldDefn Field03("x_coord", OFTReal); Field03.SetWidth(12); Field03.SetPrecision(6);
 OGRFieldDefn Field04("y_coord", OFTReal); Field04.SetWidth(12); Field04.SetPrecision(6);

 poLayer->CreateField(&Field01);
 poLayer->CreateField(&Field02);
 poLayer->CreateField(&Field03);
 poLayer->CreateField(&Field04);
 
 for (int i = 0;i < nodenum;i++)
 {
  NodeTopology tempJBNode = m_NodeTopologyvec[i];

  OGRFeature *poFeature;
  poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());//必须用CreateFeature来生成对象,用new生成对象出错

  poFeature->SetField("jdid"     , tempJBNode.m_nodeId);
  poFeature->SetField("jdlxdm"   , tempJBNode.m_nodeType);
  poFeature->SetField("x_coord"  , tempJBNode.m_xNodeCoord);
  poFeature->SetField("y_coord"  , tempJBNode.m_yNodeCoord);

  //先构建对象,再添加进来

  OGRPoint pt;
  double x = tempJBNode.m_xNodeCoord;
  double y = tempJBNode.m_yNodeCoord;
  pt.setX(x);
  pt.setY(y);
  poFeature->SetGeometry(&pt);
  
  if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)
  {
   printf("Failed to create feature in shapefile.\n");
   exit(1);
  }

  OGRFeature::DestroyFeature(poFeature);
 }

 OGRDataSource::DestroyDataSource(poDS);
}

//写线对象
void saveRoadShp(string path)
{
 if (m_Roadvec.size() == 0)
  return;
  
 int roadnum = m_Roadvec.size();
 
 const char *pszDriverName = "ESRI Shapefile";
 OGRSFDriver *poDriver;
 OGRRegisterAll();

 poDriver = (OGRSFDriver *)OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
 if (poDriver == NULL)
 {
  printf("%s driver not available.\n", pszDriverName);
  exit(1);
 }
 OGRDataSource *poDS;
 //poDS = poDriver->CreateDataSource("road.shp", NULL);

 string shapename = path + "\\road.shp";
 poDS = poDriver->CreateDataSource(shapename.c_str(), NULL);


 if (poDS == NULL)
 {
  printf("Creation of output file failed.\n");
  exit(1);
 }
 OGRLayer *poLayer;
 poDS->CreateLayer("road", NULL, wkbLineString, NULL);
 poLayer = NULL;
 poLayer = poDS->GetLayer(0);//shp文件只有一个图层;
 if (poLayer == NULL)
 {
  printf("Layer creation failed.\n");
  exit(1);
 }

 OGRFieldDefn Field01("ysbh", OFTInteger); Field01.SetWidth(32);
 OGRFieldDefn Field02("mc", OFTString); Field02.SetWidth(50);
 OGRFieldDefn Field03("kd", OFTReal); Field03.SetWidth(32); Field03.SetPrecision(6);
 
 poLayer->CreateField(&Field01);
 poLayer->CreateField(&Field02);
 poLayer->CreateField(&Field03);


 for (int i = 0;i < roadnum;i++)
 {
  JBRoad tempJBRoad = m_Roadvec[i];

  OGRFeature *poFeature;
  poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());//必须用CreateFeature来生成对象,用new生成对象出错

  poFeature->SetField("ysbh", tempJBRoad.m_ElementCode);
  poFeature->SetField("mc", tempJBRoad.m_RoadName.c_str());
  poFeature->SetField("kd", tempJBRoad.m_RoadWidth);

  OGRLineString line;

  for (int j = 0;j < tempJBRoad.m_vPoints.size();j++)
  {
   double x = tempJBRoad.m_vPoints[j].getX();
   double y = tempJBRoad.m_vPoints[j].getY();
   line.addPoint(x, y);
  }

  poFeature->SetGeometry(&line);

  if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)
  {
   printf("Failed to create feature in shapefile.\n");
   exit(1);
  }

  OGRFeature::DestroyFeature(poFeature);
 }

 OGRDataSource::DestroyDataSource(poDS);
}

//写面对象
void  savecityShp(string path)
{
 if (m_Cityvec.size() == 0)
 return;

 int citynum = m_Cityvec.size();

 const char *pszDriverName = "ESRI Shapefile";
 OGRSFDriver *poDriver;

 OGRRegisterAll();

 poDriver = (OGRSFDriver *)OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
 if (poDriver == NULL)
 {
  printf("%s driver not available.\n", pszDriverName);
  exit(1);
 }
 OGRDataSource *poDS;
 //poDS = poDriver->CreateDataSource("road.shp", NULL);

 string shapename = path + "\\city.shp";
 poDS = poDriver->CreateDataSource(shapename.c_str(), NULL);

 if (poDS == NULL)
 {
  printf("Creation of output file failed.\n");
  exit(1);
 }
 
 OGRLayer *poLayer;
 poDS->CreateLayer("city", NULL, wkbPolygon, NULL);
 poLayer = NULL;
 poLayer = poDS->GetLayer(0);//shp文件只有一个图层;
 if (poLayer == NULL)
 {
  printf("Layer creation failed.\n");
  exit(1);
 }

 OGRFieldDefn Field01("id", OFTInteger); Field01.SetWidth(32);
 OGRFieldDefn Field02("dm", OFTInteger); Field02.SetWidth(32);
 OGRFieldDefn Field03("area1", OFTReal); Field03.SetWidth(12); Field03.SetPrecision(6);
 OGRFieldDefn Field04("area2", OFTReal); Field04.SetWidth(12); Field04.SetPrecision(6);

 poLayer->CreateField(&Field01);
 poLayer->CreateField(&Field02);
 poLayer->CreateField(&Field03);
 poLayer->CreateField(&Field04);
 
 for (int i = 0;i < citynum;i++)
 {
  Cityclass tempCity = m_Cityvec[i];

  OGRFeature *poFeature;
  poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());//必须用CreateFeature来生成对象,用new生成对象出错

  poFeature->SetField("id"     , tempCity.m_nodeId);
  poFeature->SetField("dm"   , tempCity.m_code);
  poFeature->SetField("area1"  , tempCity.area1);
  poFeature->SetField("area2"  , tempCity.area2);

  //先构建对象,再添加进来

  OGRPolygon area;
  OGRLineaRing linering;

  for(int j = 0;j < tempCity.m_vPoints.size();j++)
  {
   double x = tempCity.m_vPoints[j].getX();
   double y = tempCity.m_vPoints[j].getY();
   linering.addPoint(x,y);
  }

  area.addRing(&linering);
  poFeature->SetGeometry(&area);
  
  if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)
  {
   printf("Failed to create feature in shapefile.\n");
   exit(1);
  }

  OGRFeature::DestroyFeature(poFeature);
 }

 OGRDataSource::DestroyDataSource(poDS);
}


//只写dbf文件
void  saveGroupNodeShp(string path)
{
 if (m_GroupNodevec.size() == 0)
 return;

 int Gnodenum = m_GroupNodevec.size();

 const char *pszDriverName = "ESRI Shapefile";
 OGRSFDriver *poDriver;

 OGRRegisterAll();

 poDriver = (OGRSFDriver *)OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
 if (poDriver == NULL)
 {
  printf("%s driver not available.\n", pszDriverName);
  exit(1);
 }
 OGRDataSource *poDS;
 //poDS = poDriver->CreateDataSource("GroupNode.shp", NULL);

 string shapename = path + "\\GroupNode.shp";
 poDS = poDriver->CreateDataSource(shapename.c_str(), NULL);

 if (poDS == NULL)
 {
  printf("Creation of output file failed.\n");
  exit(1);
 }
 
 OGRLayer *poLayer;
 poDS->CreateLayer("GroupNode", NULL, wkbNone, NULL);
 poLayer = NULL;
 poLayer = poDS->GetLayer(0);//shp文件只有一个图层;
 if (poLayer == NULL)
 {
  printf("Layer creation failed.\n");
  exit(1);
 }

 OGRFieldDefn Field01("id", OFTInteger); Field01.SetWidth(32);
 OGRFieldDefn Field02("dm", OFTInteger); Field02.SetWidth(32);
 OGRFieldDefn Field03("area1", OFTReal); Field03.SetWidth(12); Field03.SetPrecision(6);
 OGRFieldDefn Field04("area2", OFTReal); Field04.SetWidth(12); Field04.SetPrecision(6);

 poLayer->CreateField(&Field01);
 poLayer->CreateField(&Field02);
 poLayer->CreateField(&Field03);
 poLayer->CreateField(&Field04);
 
 for (int i = 0;i < Gnodenum;i++)
 {
  NodeTopology tempNode = m_GroupNodevec[i];

  OGRFeature *poFeature;
  poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());//必须用CreateFeature来生成对象,用new生成对象出错

  poFeature->SetField("id"     , tempNode.m_nodeId);
  poFeature->SetField("dm"   , tempNode.m_code);
  poFeature->SetField("area1"  , tempNode.area1);
  poFeature->SetField("area2"  , tempNode.area2);

  //先构建对象,再添加进来
  poFeature->SetGeometry(NULL);
  
  if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)
  {
   printf("Failed to create feature in shapefile.\n");
   exit(1);
  }

  OGRFeature::DestroyFeature(poFeature);
 }

 OGRDataSource::DestroyDataSource(poDS);
}

OGR 写点线面对象和单独的dbf文件

上一篇:Mysql分组函数与查询


下一篇:docker(django+mysql)部署,解决多项目环境问题