Issues with ArcPy SpatialReference Class

9470
25
04-27-2016 01:34 AM
MattBrehm
New Contributor II

     I am attempting to write a Python script that uses a general reference map of the NAD 1983 UTM Zones to automate the selection of an appropriate map projection for a specific area. I have completed the script up until the point at which I need to create a SpatialReference object that contains the appropriate projection. I have been getting a 999999 general runtime error whenever I try to assign the SpatialReference object the value of "NAD 1983 UTM Zone 9N" or lower, but past "NAD 1983 UTM Zone 10N", it works.

     This screenshot is the result of a test script in which I import ArcPy, and then try to initialize a SpatialReference object with the respective values mentioned above. In the second run, I printed the SpatialReference object's name in order to test to make sure that the value was assigned correctly. Am I missing something here, or is this a known issue with assigning the names of projections to SpatialReference objects? I would otherwise use the WKID for the projection, but the reference map that I am using only contains a field for the name of the projection.

Edit: I am using general reference maps for both the NAD 1983 UTM Zones as well as the USA State Plane Zones NAD83 in order to find an appropriate projection for an input dataset. I first find the centroid of the dataset, and then select by location from the reference projection maps. From the responses here and my own testing, it seems as if trying to assign SpatialReference objects the projections by name does not work for certain types of projections. Namely, the NAD 1983 UTM Zone 1-9 projections. I have also yet to find a U.S. State Plane projection in which the SpatialReference object does not give an error when attempting to assign the projection by name. The solution seems to be to assign the SpatialReference object by WKID. but this could be quite cumbersome if the assignment of projection WKIDs does not follow an intuitive pattern and you're working with hundreds of reference projections.

Tags (2)
0 Kudos
25 Replies
DavidWasserman
Occasional Contributor III

I have a related question to spatial references.
I usually reference the incoming feature class or use the specific factory code for a projection.

Examples:

OutPut = arcpy.CreateFeatureclass_management(workspace, tempOutName, "POLYLINE", template=inFeatureClass,
                                                     spatial_reference=inFeatureClass)

# or

webMercatorAux = arcpy.SpatialReference(3857)

I was curious while reading this you could use either of these approaches?  Is there a stability advantage? (One of my projects has some cursor instability issues ATM and I am not sure what is causing it).

David Wasserman, AICP
0 Kudos
AdrianWelsh
MVP Honored Contributor

David, I would think that either method should work fine. I personally prefer to reference in the spatial reference from an existing feature class just so I know it will keep things in the same system.

What instability issues are you seeing? Are you getting error messages?

0 Kudos
DavidWasserman
Occasional Contributor III

Hey Adrian,

I was thinking about making a post about the scripts in question it, and I don't want to hijack this one. I will tag you when I do. It has been a low priority because I got my temporary fixes to work on the projects I needed it on, but I would appreciate the input.

David

David Wasserman, AICP
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Based on what Melita Kennedy​ said earlier about spacing, along with the fact you are using UTM NAD83, some basic string formatting should work for you:

>>> sr_names = ['NAD 1983 UTM Zone {}N'.format(i) for i in range(1,18)]
>>> for name in sr_names:
...     name = "{} {} {} {} {:>3}".format(*name.split())
...     sr = arcpy.SpatialReference(name)
...     print sr.name
...     
NAD_1983_UTM_Zone_1N
NAD_1983_UTM_Zone_2N
NAD_1983_UTM_Zone_3N
NAD_1983_UTM_Zone_4N
NAD_1983_UTM_Zone_5N
NAD_1983_UTM_Zone_6N
NAD_1983_UTM_Zone_7N
NAD_1983_UTM_Zone_8N
NAD_1983_UTM_Zone_9N
NAD_1983_UTM_Zone_10N
NAD_1983_UTM_Zone_11N
NAD_1983_UTM_Zone_12N
NAD_1983_UTM_Zone_13N
NAD_1983_UTM_Zone_14N
NAD_1983_UTM_Zone_15N
NAD_1983_UTM_Zone_16N
NAD_1983_UTM_Zone_17N
>>>
RobertStevens
Occasional Contributor III

I have just today run into this same issue. I read all the correspondence and finally, by using the two spaces between "Zone" and the numeral got it to work.

Thanks for the solution. But ....

What a complete crock. The larger crok is this use of whitespace at all -- it is a monumental headache for anyone scripting in eg cygwin. And the smaller crock is ESRI not using something like the "name" field returned in the spatial reference. And, here we are, over two years on from the original problem and ESRI still cannot come up with something better.

0 Kudos
SeanBrooks
New Contributor II

This is really old but is still an issue in 10.8.1. It seems the ESRI online PDF's referenced in: https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-classes/spatialreference.htm  does not show the names that the arcpy.SpatialReference() expects.  I have found that the names returned by arcpy.ListSpatialReferences() are the names that can be used for arcpy.SpatialReference(). This will list the names for you:

import arcpy

# Get the list of spatial references and print it.
srs = arcpy.ListSpatialReferences(spatial_reference_type="GCS")
for sr_name in srs:
    print(sr_name)

Code referenced from: https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/listspatialreferences.htm 

 

0 Kudos