如何将JSON文件中的数据转换为值(double,int ..)

How to Convert Data in Json File to Values (double,int...)

本文关键字:double int 转换 数据 JSON 文件      更新时间:2023-10-16

我是JSON文件的新手,所以这可能是一个非常愚蠢的错误:

我使用c 中的gdal将形状文件转换为geojson文件。我使用的代码是C 的GDAL API中显示的代码(链接(。我正确读取了形状文件,然后将其转换为带有以下代码的JSON文件

    char * json_poly = Polygon->exportToJson();
    Json::Value GeoJson;
    Json::Reader reader;
    reader.parse(json_poly,GeoJson);
    

多边形是GDAL对象。现在Geojson是一个JSON ::值,看起来像这样

   "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’

如何施放值以与它们进行算术操作?

链接在主题下说明"获取数据集信息"

double        adfGeoTransform[6];
printf("Driver: %s/%sn", poDataset->GetDriver()->GetDescription(),
       poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
printf("Size is %dx%dx%dn",
       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)在此处记录:link