#include#include using namespace std; int main() { //支持中文路径 CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); //支持属相表中文字符 CPLGetConfigOption("SHAPE_ENCODING", "UTF-8"); const char * shpFile = "Supermarket.shp"; GDALAllRegister(); GDALDataset * ds; ds = (GDALDataset *)GDALOpenEx(shpFile, GDAL_OF_VECTOR and GDAL_OF_READONLY,NULL,NULL,NULL); if (ds == NULL) { cout << "打开数据失败!" << endl; return 0; } OGRLayer * lyr = ds->GetLayer(0); lyr->ResetReading(); OGRFeatureDefn * lyrDefn = lyr->GetLayerDefn(); int fieldCount = lyrDefn->GetFieldCount(); while (true) { OGRFeature * feature; feature = lyr->GetNextFeature(); if (feature == NULL) { cout << "数据读取结束" << endl; break; } for (int i = 0;i < fieldCount;i++) { OGRFieldDefn * fieldDefn=feature->GetFieldDefnRef(i); const char * fieldName= fieldDefn->GetNameRef(); OGRFieldType fieldType = fieldDefn->GetType(); switch (fieldType) { case OFTString: printf("%s:%sn", fieldName,feature->GetFieldAsString(i)); break; case OFTReal: printf("%s:%0.5fn", fieldName, feature->GetFieldAsDouble(i)); break; default: break; } } //输出坐标 OGRPoint * point = (feature->GetGeometryRef())->toPoint(); cout << point->getX() << endl; cout << point->getY() << endl; } return 0; }



