Create geometry form Geojson string

1,944 views
Skip to first unread message

cesa...@gmail.com

unread,
Nov 5, 2014, 1:39:29 PM11/5/14
to geod...@googlegroups.com
Hi,

I am trying to create a geometry from a Geojson string:

polygonstr = u'{"type": "Feature","geometry":{"type":"Polygon","coordinates":[[[-6.0, 40.0],[-5.0, 40.0],[-5.0, 38.0],[-7.0, 37.0],[-6.0, 40.0]]]},"properties": {"id": 0} }'
polygon = GEOSGeometry(polygonstr)

This should work, according to Geodjango documentation [1], but it fails.
    raise OGRException('Invalid geometry pointer returned from "%s".' % func.__name__)
OGRException: Invalid geometry pointer returned from "OGR_G_CreateGeometryFromJson".

Note that I can correctly create the geometry using WKT:
polygonwkt = "POLYGON((-6.0 40.0, -5.0 40.0, -5.0 38.0, -7.0 37.0, -6.0 40.0))"
polygon = GEOSGeometry(polygonwkt)

What am I doing wrong?

Best regards,

César Martínez Izquierdo

[1] https://docs.djangoproject.com/en/dev/ref/contrib/gis/geos/#creating-a-geometry

Claude Paroz

unread,
Nov 6, 2014, 4:01:22 AM11/6/14
to geod...@googlegroups.com
Le mercredi 05 novembre 2014 à 10:39 -0800, cesa...@gmail.com a
écrit :
> Hi,
>
> I am trying to create a geometry from a Geojson string:
>
> polygonstr = u'{"type":
> "Feature","geometry":{"type":"Polygon","coordinates":[[[-6.0,
> 40.0],[-5.0, 40.0],[-5.0, 38.0],[-7.0, 37.0],[-6.0,
> 40.0]]]},"properties": {"id": 0} }'
> polygon = GEOSGeometry(polygonstr)
>
> This should work, according to Geodjango documentation [1], but it
> fails.
> raise OGRException('Invalid geometry pointer returned from "%s".'
> % func.__name__)
> OGRException: Invalid geometry pointer returned from
> "OGR_G_CreateGeometryFromJson".
>
> Note that I can correctly create the geometry using WKT:
> polygonwkt = "POLYGON((-6.0 40.0, -5.0 40.0, -5.0 38.0, -7.0 37.0,
> -6.0 40.0))"
> polygon = GEOSGeometry(polygonwkt)
>
> What am I doing wrong?

Hi César,

You should only feed the 'geometry' part of the GeoJSON to the
GEOSGeometry constructor:
polygon = GEOSGeometry('{"type":"Polygon","coordinates":[[[-6.0,
40.0],[-5.0, 40.0],[-5.0, 38.0],[-7.0, 37.0],[-6.0, 40.0]]]}')

Cheers,

Claude
--
www.2xlibre.net

César Martínez Izquierdo

unread,
Nov 6, 2014, 9:22:40 AM11/6/14
to geod...@googlegroups.com
Thank you very much Claude, I was not able to see the difference by myself :-O


César
> --
> You received this message because you are subscribed to the Google Groups "geodjango" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to geodjango+...@googlegroups.com.
> To post to this group, send email to geod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/geodjango.
> For more options, visit https://groups.google.com/d/optout.



--
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
César Martínez Izquierdo
GIS developer
- - - - - - - - - - - - - - - - - - - -
Blog: http://geotechnotes.wordpress.com/
ETC-SIA: http://sia.eionet.europa.eu/
Universitat Autònoma de Barcelona (SPAIN)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Marco Silva

unread,
Oct 25, 2018, 2:33:36 AM10/25/18
to geodjango
I'm somehow have this error with only different data.
I'm trying to make this polygon:

polygon2 = GEOSGeometry('{"type": "Polygon", "coordinates": [[[37.50272976153131, -8.769023268730317], [37.504772480804654, -8.764132229162282], [37.504159670891134, -8.763531575180249], [37.5048746153012, -8.76166525745033],[37.50432989637168, -8.761171863107947], [37.50455118891613, -8.76048540141418], [37.5040745580053, -8.760421045630396], [37.50293403597371, -8.764411104225363], [37.50358090135408, -8.764904498567766],[37.502150980909484, -8.768680037883433]]]}'

and I have that same error. Is anything wrong with my data? Am I missing something? Did the API change in the meanwhile?

Claude Paroz

unread,
Oct 25, 2018, 2:49:39 AM10/25/18
to geod...@googlegroups.com
Le 24.10.18 à 18:09, Marco Silva a écrit :
> I'm somehow have this error with only different data.
> I'm trying to make this polygon:
>
> polygon2 = GEOSGeometry('{"type": "Polygon", "coordinates":
> [[[37.50272976153131, -8.769023268730317], [37.504772480804654,
> -8.764132229162282], [37.504159670891134, -8.763531575180249],
> [37.5048746153012, -8.76166525745033],[37.50432989637168,
> -8.761171863107947], [37.50455118891613, -8.76048540141418],
> [37.5040745580053, -8.760421045630396], [37.50293403597371,
> -8.764411104225363], [37.50358090135408,
> -8.764904498567766],[37.502150980909484, -8.768680037883433]]]}'
>
> and I have that same error. Is anything wrong with my data? Am I missing
> something? Did the API change in the meanwhile?

It is probably related to the point list you provided which is an
unclosed linear ring.

Claude
--
www.2xlibre.net
Reply all
Reply to author
Forward
0 new messages