我是JSON文件的新手,所以这可能是一个非常愚蠢的错误:
我已将形状文件转换为GeoJson文件在C ++中使用GDAL。我使用的代码是GDAL API for c ++(http://www.gdal.org/gdal_tutorial.html)中显示的代码。我正确读取了shape文件,然后使用以下代码将其转换为JSON文件
char * json_poly = Polygon->exportToJson();
Json::Value GeoJson;
Json::Reader reader;
reader.parse(json_poly,GeoJson);
其中Polygon是GDAL对象。现在GeoJson是一个JSON :: VALUE,看起来像这样
"coordinates" :
[
[
[
586417.77972987387,
6884063.8642021669
],
[
586655.1163914972,
6884198.7810712075
],
[
586707.31919375062,
6884238.8010942638
],
[
586703.2053746446,
6884258.7722300319
],
[
586754.77872091066,
6884309.6043649064
],
[
586832.7780266488,
6884413.5168099366
]
但是,当我尝试使用简单的循环计算质心时,如下面的代码段
double x = 0 ;
double y = 0 ;
for( int i = 0 ; i < GeoJson["coordinates"][0].size() ; i++ )
{
x += GeoJson["coordinates"][0][i][0];
y += GeoJson["coordinates"][0][i][1];
}
x /= GeoJson["coordinates"][0].size() ;
y /= GeoJson["coordinates"][0].size() ;
cout << "The centroid is in " << x<< " , " << y << endl;
我收到以下错误
no match for ‘operator+=’ (operand types are ‘double’ and ‘Json::Value’)
如果我尝试将值转换为double double(GeoJson["coordinates"][0][i][0])
,我会收到以下错误
error: invalid cast from type ‘Json::Value’ to type ‘double’
如何对值进行转换以便对它们进行算术运算?
答案 0 :(得分:1)
http://www.gdal.org/gdal_tutorial.html在主题&#34;获取数据集信息&#34;
double adfGeoTransform[6];
printf("Driver: %s/%s\n", poDataset->GetDriver()->GetDescription(),
poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
printf("Size is %dx%dx%d\n",
poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
poDataset->GetRasterCount() );
if( poDataset->GetProjectionRef() != NULL )
printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );
if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
{
printf( "Origin = (%.6f,%.6f)\n",
adfGeoTransform[0], adfGeoTransform[3] );
printf( "Pixel Size = (%.6f,%.6f)\n",
adfGeoTransform[1], adfGeoTransform[5] );
}
此处记录了 virtual CPLErr GetGeoTransform (double *padfTransform)
:http://www.gdal.org/classGDALDataset.html