Wednesday 3 May 2017

arcgis 10.0 - How to programmatically determine whether a spatial reference factory code is valid?


I am using the following code segment to create a spatial reference.


    spatialReferance = arcpy.SpatialReference()
spatialReferance.factoryCode = spatialReferanceFactoryCode
spatialReferance.create()

But it gives the following error:



Runtime error : ERROR 999999: Error executing function. the input string is not a geographic or projected coordinate system




How can I programmatically determine whether a factory code is valid or not?



Answer



See Do ArcGIS SpatialReference object factory codes correspond with EPSG numbers? in particular @mkennedy and @blah238 answers.


As to whether a factory code is valid or not... In Python, you can use the EAFP method (Easier to Ask for Forgiveness than Permission)


try:
spatialReference.factoryCode = spatialReferenceFactoryCode
spatialReference.create()
except RuntimeError:
print 'factoryCode is not valid'

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...