Select to view content in your preferred language

Issues with spatial reference object from currently active map

1965
1
01-27-2023 07:11 AM
SeanConway
Emerging Contributor

I'm having an arcpy issue where I'm trying to pull a spatial reference object from the currently active map to use to create a point geometry.  It works fine if I pull it from the first layer in the ToC: 

 

insr = arcpy.Describe(layers[0]).spatialReference 

 

But if I pull it from the currently active map: 

 

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
insr = m.spatialReference

 

then I get an error. Both the layer and the map return a spatial reference object, but when I use the one from the map, it ends up erroring out on this line: 

 

point1 = arcpy.PointGeometry(arcpy.Point(extent.XMin,extent.YMin),insr)

 

Traceback (most recent call last):
  File "<string>", line 12, in <module>
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\geometries.py", line 86, in __init__
    super().__init__(inputs, spatial_reference, has_z, has_m)
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\mixins.py", line 223, in __init__
    self._arc_object = gp.CreateObject('geometry', self.__type_string__,
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 43, in gp_fixargs
    new_args.append(gp_fixarg(arg, string_results, pass_arc_object))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 30, in gp_fixarg
    if isinstance(arg, Result) and string_results:
RuntimeError: SpatialReference: Get attribute __class__ does not exist

The attached images are an example of the object it's returning where insr is the SR object from a layer and insr2 is the object from the map. They are different projections but that shouldn't be an issue.  If I use exportToString() on the map object and then use that to loadFromString() on a new SR object then it will also work, but that seems like a needless workaround.

Thanks in advance for any help.

0 Kudos
1 Reply
JohannesLindner
MVP Frequent Contributor

Yeah, sometimes arcpy can be tricky with the conversion between Result objects, geoprocessing xyz objects and the "pure" xyz objects.

In this case, you can just create a new SpatialReference with the factory code of your extracted sr:

m = arcpy.mp.ArcGISProject("current").activeMap
extent = m.defaultCamera.getExtent()

map_sr = m.spatialReference
map_point = arcpy.PointGeometry(arcpy.Point(extent.XMin,extent.YMin), map_sr)  # error

map_sr = arcpy.SpatialReference(map_sr.factoryCode)
map_point = arcpy.PointGeometry(arcpy.Point(extent.XMin,extent.YMin), map_sr)  # works

Have a great day!
Johannes
0 Kudos