Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#31366 closed Bug (invalid)

GEOSGeometry fails on instance with GEOJson and srid not 4326.

Reported by: Walter Lorenzetti Owned by: nobody
Component: GIS Version: dev
Severity: Normal Keywords: Geodjango
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

On creating a GEOSGeometry intance by a GEOJson string with a srid not 4326, I obtain this error:
coord = '{"type": "Point", "coordinates": [10000, 10000]}'

g = GEOSGeometry(coord, srid=3857)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/envs/g3w_suite_dj22/lib/python3.6/site-packages/django/contrib/gis/geos/geometry.py", line 732, in __init__
    raise ValueError('Input geometry already has SRID: %d.' % input_srid)
ValueError: Input geometry already has SRID: 4326.

So to fix it I use this not beautyfull solution:

g = GEOSGeometry(GEOSGeometry(coord).wkt, srid=3857)

Thanks in advance.
W

Change History (2)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: invalid
Status: newclosed
Summary: GEOSGeometry fails on instance with GEOJson and srid not 4326GEOSGeometry fails on instance with GEOJson and srid not 4326.
Version: 2.2master

It's documented that "for the GeoJSON format, the SRID is set based on the crs member. If crs isn’t provided, the SRID defaults to 4326". So if you want to use GeoJSON with a different SRID you need to use crs, e.g.

{
  "type": "Point",
  "coordinates": [2, 49],
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:EPSG::3857"
    }
  }
}

comment:2 by MangoMango666, 4 years ago

I would argue in favor of this bug that when a GeoJSON is created from PostGIS using the ST_AsGeoJSON function, there is 'crs' member (only 'type' and 'coordinates').

Also, in the same page of the doc, it is written "All these constructors take the keyword argument srid.", hence the confusion of Walter.

Note: See TracTickets for help on using tickets.
Back to Top