0

I have a raster image that has a custom projection system. I used the following code to retrieve the projection information from the raster and turn it into an ArcGIS recognized PCS following this example...

import arcpy
from osgeo import gdal

raster_path = r"\\tiff_inputs\tiff_UTM_inputs\amz_jurisdiction_UTM20.tif"
in_ds = gdal.Open(raster_path)
projection = in_ds.GetProjection()
pcs = arcpy.SpatialReference(text=projection)

Here is the projection string when printed to the console...

'PROJCS["Brasil, Zona UTM 20, hemisf\udce9rio sul",GEOGCS["br_utm20sad_s",DATUM["South American 1969",SPHEROID["International 1967 (truncated)",6378160,298.24999999573],TOWGS84[-67.35,3.88,-38.22,0,0,0,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-63],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]'

However when I input this projection string into arcpy.SpatialReference I receive the following error:

ValueError: SpatialReference: Spatial reference string is invalid.

I cannot use the display name of the coordinate system as an input either because it is not recognized by arcpy.

How can I reformat the gdal projection string to be valid parameter for arcpy.SpatialReference?

2 Answers 2

2

Try creating an osr.SpatialReference() object from the GDAL WKT, then using MorphToESRI() to make any WKT it spits out ESRI-compatible.

Should look something like this:

from osgeo import osr

gdal_wkt = in_ds.GetProjection()
gdal_srs = osr.SpatialReference()
gdal_srs.ImportFromWkt(gdal_wkt)
gdal_srs.MorphToESRI()
esri_wkt = gdal_srs.ExportToWkt()
esri_srs = arcpy.SpatialReference(text=esri_wkt)

Python docs: https://gdal.org/api/python/osgeo.osr.html#osgeo.osr.SpatialReference.MorphToESRI

C++ docs with a better description: https://gdal.org/doxygen/classOGRSpatialReference.html#aac948450b15fa3d8814ab440b9f705b7

3
  • Thanks for your suggestion. It appears that my wkt is not valid. I received an error TypeError: not a string when running gdal_srs.ImportFromWkt(gdal_wkt) using the projection string that I provided in the post. What do you suspect could be the issue?
    – ESimonson
    Commented Dec 7, 2023 at 16:14
  • Hmm, well what does type(gdal_wkt) give? If it's None then maybe you're trying a raster with no projection defined, or perhaps gdal is already giving back a SpatialReference?
    – mikewatt
    Commented Dec 7, 2023 at 18:05
  • 1
    I used encoding and decoding and this fixed my issues with the projection string. gdal_wkt = in_ds.GetProjection().encode('utf-8', 'backslashreplace').decode('utf-8')
    – ESimonson
    Commented Dec 11, 2023 at 15:00
0

While I am not sure why your projection isn't being recognised by ArcGIS Pro, the closest projection to the one you have provided looks like SIRGAS_2000_Brazil_Mercator. Try reprojecting your raster into that via ArcGIS Pro.

enter image description here

You can access the spatial reference in arcpy by using the following code:

pcs = arcpy.SpatialReference('SIRGAS_2000_Brazil_Mercator')
1
  • Hi Angus, I am trying to automate this part of the process and use the spatial reference at a later stage.
    – ESimonson
    Commented Dec 6, 2023 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.