Search code examples
pythondjangogeojsongeos

Error encountered checking Geometry returned from GEOS C function


I'm using GEOS API with Python/Django.

I want to create a MultiPolygon from geoJSON. I'm running the following code into a python Shell:

mp2 = GEOSGeometry('{"type":"MultiPolygon", "coordinates": [[[[-56.14914894104003, -33.189642368629116], [-56.14914894104003, -33.18583537264943], [-56.14185333251953, -33.18583537264943], [-56.14185333251953, -33.189642368629116]], [[-56.14743232727051, -33.18834944515198], [-56.14743232727051, -33.186769179430186], [-56.14494323730469, -33.186769179430186], [-56.14494323730469, -33.18834944515198]]], [[[-56.14957809448242, -33.19244363735929], [-56.14957809448242, -33.19000151065257], [-56.14434242248535, -33.19000151065257], [-56.14434242248535, -33.19244363735929]]]]}')

And getting the error:

"GEOSException: Error encountered checking Geometry returned from GEOS C function "GEOSWKBReader_read_r"."

While the next example code works well.

mp2 = GEOSGeometry('{"type": "MultiPolygon","coordinates": [[[[-101.2, -1.2], [-101.8, -1.2], [-101.8, -1.8], [-101.2, -1.8], [-101.2, -1.2]],[[-101.2, -1.2], [-101.3, -1.2], [-101.3, -1.3], [-101.2, -1.3], [-101.2, -1.2]]],[[[-100.0, 0.0], [-101.0, 0.0], [-101.0, -1.0], [-100.0, -1.0], [-100.0, 0.0]]]]}')

I can't figure out which is the problem with my data.

NOTE: Both examples are Multipolygon with holes.


Solution

  • I found the problem with my data.

    The working code has the same start and end but my data doesn't.

    So, this fix the problem

    mp2 = GEOSGeometry('{"type":"MultiPolygon", "coordinates": [[[[-56.14914894104003, -33.189642368629116], [-56.14914894104003, -33.18583537264943], [-56.14185333251953, -33.18583537264943], [-56.14185333251953, -33.189642368629116], [-56.14914894104003, -33.189642368629116]], [[-56.14743232727051, -33.18834944515198], [-56.14743232727051, -33.186769179430186], [-56.14494323730469, -33.186769179430186], [-56.14494323730469, -33.18834944515198], [-56.14743232727051, -33.18834944515198]]], [[[-56.14957809448242, -33.19244363735929], [-56.14957809448242, -33.19000151065257], [-56.14434242248535, -33.19000151065257], [-56.14434242248535, -33.19244363735929], [-56.14957809448242, -33.19244363735929]]]]}')