std::string imgPath = CT2A(strTifPath);//tif文件路径
strShpTemp += CFileToolKit::GetFileLogicName(strShpPath);
std::string tempShpPath = CT2A(strShpTemp);
GDALDataset * pInDataset = (GDALDataset *)GDALOpen(imgPath.c_str(), GA_ReadOnly);
if (pInDataset == NULL)
{
printf("读取图像失败!");
return false;
}
int nDemWidth = pInDataset->GetRasterXSize(); //获取图像宽
int nDemHeight = pInDataset->GetRasterYSize(); //获取图像高
int Count = pInDataset->GetRasterCount(); //波段数
//读取图像数据波段
GDALRasterBand *pInRasterBand = pInDataset->GetRasterBand(1);
float *pData = new float[nDemWidth*nDemHeight]();
CPLErr err = pInRasterBand->RasterIO(GF_Read, 0, 0, nDemWidth, nDemHeight, pData, nDemWidth, nDemHeight, GDT_Float32, 0, 0);
if (err == CE_Failure)
{
cout << "读取DEM图像数据时出错!" << endl;
GDALDestroyDriverManager();
return false;
}
//判断图像中是否有异常值,并获取异常值实际值
float fNoData = 0;
int nIdx;
for (int i = 0; i < nDemHeight; i++)
{
for (int j = 0; j < nDemWidth; j++)
{
nIdx = i*nDemWidth + j;
if (pData[nIdx] <= -9999)
{
fNoData = pData[nIdx];
}
}
}
//创建矢量图
const char *pszDriverName = "ESRI Shapefile";
GDALDriver *poDriver;
GDALAllRegister();
poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName);
if (poDriver == NULL)
{
printf("%s driver not available.\n", pszDriverName);
exit(1);
}
GDALDataset *poDS = poDriver->Create(tempShpPath.c_str(), 0, 0, 0, GDT_Unknown, NULL); //创建数据源
if (poDS == NULL)
{
printf("Creation of output file failed.\n");
exit(1);
}
// const char *szWkt = pInDataset->GetProjectionRef();
CString strWkt = CSpatialRef::WebMercatorWkt;
std::string str = CW2A(strWkt);
OGRSpatialReference *poSpatialRef = new OGRSpatialReference(pInDataset->GetProjectionRef());
OGRLayer *poLayer = poDS->CreateLayer("Elevation", poSpatialRef, wkbLineString, NULL); //创建图层
if (poLayer == NULL)
{
printf("Layer creation failed.\n");
exit(1);
}
OGRFieldDefn ofieldDef1("Elevation", OFTInteger); //在矢量图中创建高程值字段
if (poLayer->CreateField(&ofieldDef1) != OGRERR_NONE)
{
cout << "创建矢量图层属性表失败!" << endl;
GDALClose((GDALDatasetH)poDS);
GDALClose(pInDataset);
return false;
}
//根据图像波段生成矢量图等高线
if (FLOAT_EQUAL2(0.0, fNoData))
GDALContourGenerate(pInRasterBand, dfContourInterval, 0, 0, NULL, FALSE, 0, (OGRLayerH)poLayer, -1, 0, NULL, NULL);
else //有异常值时,不对异常值进行处理
GDALContourGenerate(pInRasterBand, dfContourInterval, 0, 0, NULL, TRUE, fNoData, (OGRLayerH)poLayer, -1, 0, NULL, NULL);
// 对生成shp文件重投影坐标转换成墨卡托
GDALDataset *poLDS = poDriver->Create(shpPath.c_str(), 0, 0, 0, GDT_Unknown, NULL); //创建数据源
OGRSpatialReference *poLSpatialRef = new OGRSpatialReference(str.c_str());
OGRCoordinateTransformation* ctx = OGRCreateCoordinateTransformation(poSpatialRef, poLSpatialRef);
OGRLayer *poLLayer = poLDS->CreateLayer("Elevation", poLSpatialRef, wkbLineString, NULL); //创建图层
OGRFeatureDefn* layer_def = poLayer->GetLayerDefn();
for (int i = 0; i < layer_def->GetFieldCount(); ++i)
{
OGRFieldDefn* field_def = layer_def->GetFieldDefn(i);
poLLayer->CreateField(field_def);
}
OGRFeature* src_feature = poLayer->GetNextFeature();
while (src_feature)
{
OGRGeometry* geometry = src_feature->GetGeometryRef();
geometry->transform(ctx);
poLLayer->CreateFeature(src_feature);
src_feature = poLayer->GetNextFeature();
}
GDALClose(poLDS);
GDALClose(poDS);
GDALClose(pInDataset);
delete[] pData;
pData = NULL;