1
from osgeo import ogr
from osgeo import osr

source = osr.SpatialReference()
source.ImportFromEPSG(4326)

target = osr.SpatialReference()
target.ImportFromEPSG(3857)

transform = osr.CoordinateTransformation(source, target)
poly = ogr.CreateGeometryFromJson(str("{'type': 'Polygon', 'coordinates': [[[-117.10825, 47.603493], [-117.10825, 47.887733], [-116.619302, 47.887733], [-116.619302, 47.603493], [-117.10825, 47.603493]]]}"))
poly.Transform(transform)

Throws error:

ERROR 1: latitude or longitude exceeded limits
ERROR 1: latitude or longitude exceeded limits
ERROR 1: latitude or longitude exceeded limits
ERROR 1: latitude or longitude exceeded limits
ERROR 1: latitude or longitude exceeded limits

when I reorder coordinates like -117.10825, 47.603493 with 47.603493, -117.10825 it is able to work , but thats not how geojson is supposed to be, What i am missing here ?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Aman RAJ
  • 87
  • 1
  • 5

3 Answers3

0

The problem is that you are passing the coordinates in the wrong order at your line of

poly = ogr.CreateGeometryFromJson(str("{'type': 'Polygon', 'coordinates': [[[-117.10825, 47.603493], [-117.10825, 47.887733], [-116.619302, 47.887733], [-116.619302, 47.603493], [-117.10825, 47.603493]]]}"))

as you experimentally confirmed already. Valid latitude values are between -90 and 90 and valid longitude coordinates are between -180 and 180

enter image description here

Your coordinate of [-117.10825, 47.603493] (and others) are therefore invalid, because the first coordinate is latitude and the second is longitude, whereas the value passed for latitude is outside its supported bounds, hence, it is very likely that you assumed that the first coordinate is longitude and second coordinate is latitude, whereas it is the other way around.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • I agree that but why gdal accept geometries with order of latitude and longitude, whereas a typical geojson has longitude and latitude, see here https://stackoverflow.com/questions/19098667/order-of-coordinates-in-geojson – Aman RAJ Sep 26 '22 at 05:26
  • ignore it figured it out, https://gis.stackexchange.com/questions/421771/ogr-coordinatetransformation-appears-to-be-inverting-xy-coordinates – Aman RAJ Sep 26 '22 at 05:49
0

As stated here https://gis.stackexchange.com/questions/421771/ogr-coordinatetransformation-appears-to-be-inverting-xy-coordinates we will have to update source

from osgeo import ogr
from osgeo import osr

source = osr.SpatialReference()
source.ImportFromEPSG(4326)
source.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)


target = osr.SpatialReference()
target.ImportFromEPSG(3857)

transform = osr.CoordinateTransformation(source, target)
poly = ogr.CreateGeometryFromJson(str("{'type': 'Polygon', 'coordinates': [[[-117.10825, 47.603493], [-117.10825, 47.887733], [-116.619302, 47.887733], [-116.619302, 47.603493], [-117.10825, 47.603493]]]}"))
poly.Transform(transform)
Aman RAJ
  • 87
  • 1
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 30 '22 at 01:23
0

Setting the mapping strategy as @Aman RAJ shows is the correct way to deal with this, and a general method that works for every projection.

Specifically for WGS84 it can also be done with:

source = osr.SpatialReference()
source.SetFromUserInput("urn:ogc:def:crs:OGC:1.3:CRS84")

Or equivalently:

source = osr.SpatialReference()
source.ImportFromWkt(osr.GetUserInputAsWKT("urn:ogc:def:crs:OGC:1.3:CRS84"))
Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97