读取shp的几何坐标
// gdal_sample.cpp : Defines the entry point for the console application.
//
#include “stdafx.h”
#include “gdal_version.h”
#include “gdal_priv.h”
#include “gdal.h”
#include <iostream>
#include “ogrsf_frmts.h”
#include “ogrsf_frmts/shape/ogrshape.h”
#define DYZ_DEBUG_SWITCH “ON”
#if 1
#define DYZ_TEST_FILE_PATH “D:\\code\\gdal-2.3.0\\x64\\Debug\\”
#define DYZ_TEST_DWG_FILE DYZ_TEST_FILE_PATH “jiaju.shp”
#else
#define DYZ_TEST_DWG_FILE DYZ_TEST_FILE_PATH “jiaju.shp”
#define DYZ_TEST_DWG_FILE DYZ_TEST_FILE_PATH “shoudongjiaju.dxf”
#define DYZ_TEST_DWG_FILE DYZ_TEST_FILE_PATH “renwutu.dxf”
#define DYZ_TEST_DWG_FILE DYZ_TEST_FILE_PATH “wolunxiang.dwg”
#endif
int main()
{
GDALAllRegister();
CPLSetConfigOption(“CPL_DEBUG”, DYZ_DEBUG_SWITCH);
CPLSetConfigOption(“GDAL_DATA”, DYZ_TEST_FILE_PATH);
CPLSetConfigOption(“GDAL_FILENAME_IS_UTF8”, “NO”);
CPLSetConfigOption(“SHAPE_ENCODING”, “CP936”);
GDALDatasetH hInDS = GDALOpenEx(DYZ_TEST_DWG_FILE, GDAL_OF_VECTOR | GDAL_OF_VERBOSE_ERROR, nullptr, nullptr, nullptr);
//GDALDataset* cInDS = GDALDataset::FromHandle(hInDS);
OGRShapeDataSource* cInDS = (OGRShapeDataSource*)(hInDS);
if (NULL == cInDS)
{
return -1;
}
const char* pcDrvName = cInDS->GetDriverName();
if (NULL != pcDrvName)
{
std::cout << “Driver Name:” << pcDrvName << std::endl;
}
int nLayerCnt = cInDS->GetLayerCount();
int nLayerIdx = 0;
for (;nLayerIdx < nLayerCnt; ++nLayerIdx)
{
OGRLayer* curLayer = cInDS->GetLayer(nLayerIdx);
GIntBig nFtrCount = curLayer->GetFeatureCount();
if (NULL != curLayer->GetName())
{
std::cout << curLayer->GetName() << ” ‘s FeatureCount :” << nFtrCount << std::endl;
}
OGRFeature * ftr = curLayer->GetNextFeature();
while (ftr)
{
OGRGeometry * poGeom = ftr->GetGeometryRef();
switch (wkbFlatten(poGeom->getGeometryType()))
{
case wkbPoint:
case wkbPoint25D:
{
OGRPoint* poPS = poGeom->toPoint();
double dfDeltaX = poPS->getX();
double dfDeltaY = poPS->getY();
}
break;
case wkbLineString:
{
OGRLineString* poLS = static_cast<OGRLineString*>(poGeom);
const char* pGeoName = poLS->getGeometryName();
if (NULL != pGeoName)
{
std::cout << ” Geo name:” << pGeoName << std::endl;
}
int iPnum = poLS->getNumPoints();
std::cout << ” Point Count:” << iPnum;
double iLen = poLS->get_Length();
std::cout << ” Len :” << iLen;
for (int i = 0; i < iPnum;i++)
{
double dfX = poLS->getX(i);
double dfY = poLS->getY(i);
std::cout << ” X : ” << dfX << ” , Y : ” << dfY;
}
std::cout << std::endl;
}
break;
case wkbPolygon:
{
OGRPolygon* poPoly = static_cast<OGRPolygon*>(poGeom);
for (int i = 0; i < poPoly->getNumInteriorRings();i++)
{
poPoly->getInteriorRing(i);
}
}
break;
case wkbMultiPolygon:
{
OGRMultiPolygon* poMP = static_cast<OGRMultiPolygon*>(poGeom);
for (int i = 0; i < poMP->getNumGeometries();i++)
{
poMP->getGeometryRef(i);
}
}
break;
default:
break;
}
ftr = curLayer->GetNextFeature();
}
//delete curLayer;
}
GDALClose(hInDS);
return 0;
}
转载自:https://blog.csdn.net/durongze/article/details/80793077