上两期文章,说明了如何创建点类型的图层和多边形类型的图层,创建好这两个图层之后,将两个图层求交得到TestResult.shp,此shp文件包含了求交后的结果。
int TestIntersection() {
GDALAllRegister();
GDALDriver* shpDriver = GetGDALDriverManager()->GetDriverByName("ESRI Shapefile");
if (shpDriver == NULL) {
printf("%s\n", "ESRI Shapefile驱动创建失败!");
return -1;
}
GDALDataset* shipDataSet = (GDALDataset*)GDALOpenEx("..\\shp\\TestPoint.shp", GDAL_OF_VECTOR, NULL, NULL, NULL);
GDALDataset* regDataSet = (GDALDataset*)GDALOpenEx("..\\shp\\TestPolygon.shp", GDAL_OF_VECTOR, NULL, NULL, NULL);
OGRSpatialReference osr;
//osr.SetWellKnownGeogCS("WGS84");
if (osr.importFromEPSG(4326) != OGRERR_NONE) {
printf("%s\n", "坐标系创建失败!");
return -1;
}
OGRLayer* shipLayer = shipDataSet->GetLayerByName("TestPoint");
OGRLayer* regLayer = regDataSet->GetLayerByName("TestPolygon");
GDALDataset* resultDataSet = shpDriver->Create("..//shp//TestResult.shp", 0, 0, 0, GDT_Unknown, NULL);
char** papszOptions = NULL;
papszOptions = CSLSetNameValue(papszOptions, "ENCODING", "UTF-8");
OGRLayer* resultLayer = resultDataSet->CreateLayer("resultLyr", &osr, wkbPoint, papszOptions);
regLayer->Intersection(shipLayer, resultLayer, NULL, GDALTermProgress, NULL);
GDALClose(shipDataSet);
GDALClose(regDataSet);
}
TestPoint.shp的图层有三个点,与TestPolygon.shp的图层求交集以后得到TestResult.shp。它们求交集之后会自动合并字段。