[gdal-dev] ogr.VectorTranslate to json appears to be incomplete

Michael Smith michael.smith.erdc at gmail.com
Fri May 26 10:09:12 PDT 2023


Interesting, that is different from what I get. I guess I have to look at how I'm building gdal from conda-forge. I do compile in two plugins (mrsid and oci) but I wouldn't that it would affect things.


at 01:05:23 PM ❯ ipython
Python 3.11.3 | packaged by conda-forge | (main, Apr  6 2023, 08:57:19) [GCC 11.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.13.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from osgeo import ogr, gdal

In [2]: ds = gdal.OpenEx('{ "type": "Polygon", "coordinates": [ [ [179.828821134787177, 66.686572724877635 ], [ -179.718786846664386, 66.695
   ...: 039153881993 ], [ -179.699204739767083, 66.515803857102057 ], [ 179.851649370943306, 66.507409512673448 ], [ 179.828821134787177,66.
   ...: 686572724877635 ] ] ] }')
/opt/conda/envs/grid/lib/python3.11/site-packages/osgeo/gdal.py:287: FutureWarning: Neither gdal.UseExceptions() nor gdal.DontUseExceptions() has been explicitly called. In GDAL 4.0, exceptions will be enabled by default.
  warnings.warn(

In [3]: gdal.VectorTranslate("/vsimem/out.json", ds, format="GeoJSON",
   ...: layerCreationOptions=["RFC7946=YES", "WRAPDATELINE=YES", "WRITE_BBOX=YES"])
Out[3]: <osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x7f546c1c3780> >

In [4]: f = gdal.VSIFOpenL("/vsimem/out.json", "rb")
   ...: content = gdal.VSIFReadL(1, 10000, f).decode("UTF-8")
   ...: gdal.VSIFCloseL(f)
   ...: gdal.Unlink("/vsimem/out.json")
   ...: print(content)
{
"type": "FeatureCollection",

"features": [
{ "type": "Feature", "properties": { }, "bbox": [ 179.8288211, 66.5074095, -179.6992047, 66.6950392 ], "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 180.0, 66.6897763 ], [ 179.8288211, 66.6865727 ], [ 179.8516494, 66.5074095 ], [ 180.0, 66.5101821 ], [ 180.0, 66.6897763 ] ] ], [ [ [ -179.7187868, 66.6950392 ], [ -180.0, 66.6897763 ], [ -180.0, 66.5101821 ], [ -179.6992047, 66.5158039 ], [ -179.7187868, 66.6950392 ] ] ] ] } }

In [5]: newds = ogr.Open(content)
ERROR 1: JSON parsing error: continue (at offset 620)
ERROR 4: Failed to read GeoJSON data



Mike


-- 

Michael Smith 
Remote Sensing/GIS Center 
US Army Corps of Engineers 





On 5/26/23, 9:28 AM, "Even Rouault" <even.rouault at spatialys.com <mailto:even.rouault at spatialys.com>> wrote:


Michael,


I've also just tried with 3.7.0 from conda-forge and this works fine for me:


I've just slightly modified your script to output more info:


from osgeo import ogr, gdal


ds = gdal.OpenEx('{ "type": "Polygon", "coordinates": [ [ [ 
179.828821134787177, 66.686572724877635 ], [ -179.718786846664386, 
66.695039153881993 ], [ -179.699204739767083, 66.515803857102057 ], [ 
179.851649370943306, 66.507409512673448 ], [ 179.828821134787177, 
66.686572724877635 ] ] ] }')
gdal.VectorTranslate("/vsimem/out.json", ds, format="GeoJSON", 
layerCreationOptions=["RFC7946=YES", "WRAPDATELINE=YES", "WRITE_BBOX=YES"])
f = gdal.VSIFOpenL("/vsimem/out.json", "rb")
content = gdal.VSIFReadL(1, 10000, f).decode("UTF-8")
gdal.VSIFCloseL(f)
gdal.Unlink("/vsimem/out.json")
print(content)
print(len(content))


newds = ogr.Open(content)
lyr = newds.GetLayer(0)
f = lyr.GetNextFeature()
f.DumpReadable()


and it outputs:


{
"type": "FeatureCollection",
"bbox": [ 179.8288211, 66.5074095, -179.6992047, 66.6950392 ],
"features": [
{ "type": "Feature", "properties": { }, "bbox": [ 179.8288211, 
66.5074095, -179.6992047, 66.6950392 ], "geometry": { "type": 
"MultiPolygon", "coordinates": [ [ [ [ 180.0, 66.6897763 ], [ 
179.8288211, 66.6865727 ], [ 179.8516494, 66.5074095 ], [ 180.0, 
66.5101821 ], [ 180.0, 66.6897763 ] ] ], [ [ [ -179.7187868, 66.6950392 
], [ -180.0, 66.6897763 ], [ -180.0, 66.5101821 ], [ -179.6992047, 
66.5158039 ], [ -179.7187868, 66.6950392 ] ] ] ] } }
]
}


625
OGRFeature(OGRGeoJSON):0
MULTIPOLYGON (((180.0 66.6897763,179.8288211 66.6865727,179.8516494 
66.5074095,180.0 66.5101821,180.0 66.6897763)),((-179.7187868 
66.6950392,-180 66.6897763,-180 66.5101821,-179.6992047 
66.5158039,-179.7187868 66.6950392)))


Even




Le 26/05/2023 à 15:12, Michael Smith a écrit :
> I assume I am doing something incorrect here but I am taking an incoming geojson feature and attempting to split it on the dateline:
>
> Using gdal 3.7.0 from conda-forge
>
> ds = gdal.OpenEx('{ "type": "Polygon", "coordinates": [ [ [ 179.828821134787177, 66.686572724877635 ], [ -179.718786846664386, 66.695039153881993 ], [ -179.699204739767083, 66.515803857102057 ], [ 179.851649370943306, 66.507409512673448 ], [ 179.828821134787177, 66.686572724877635 ] ] ] }')
> gdal.VectorTranslate("/vsimem/out.json", ds, format="GeoJSON", layerCreationOptions=["RFC7946=YES", "WRAPDATELINE=YES", "WRITE_BBOX=YES"])
> f = gdal.VSIFOpenL("/vsimem/out.json", "rb")
> content = gdal.VSIFReadL(1, 10000, f).decode("UTF-8")
> gdal.VSIFCloseL(f)
> gdal.Unlink("/vsimem/out.json")
>
> If I just do:
>
> newds = ogr.Open(content)
>
> I get RuntimeError: Failed to read GeoJSON data
> It appears to be missing the closing “]}”.
>
> If I do:
> newds = ogr.Open(content+"]}")
>
> Then it works fine.
>
>
> -- 
> Michael Smith
> Remote Sensing/GIS Center
> US Army Corps of Engineers
>
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org <mailto:gdal-dev at lists.osgeo.org>
> https://lists.osgeo.org/mailman/listinfo/gdal-dev <https://lists.osgeo.org/mailman/listinfo/gdal-dev>


-- 
http://www.spatialys.com <http://www.spatialys.com>
My software is free, but my time generally not.








More information about the gdal-dev mailing list