3

I have a function that extracts the well known text representation of a UTM zone:

def find_utm_zone(shp):
    """
    -Generates a UTM zone for the feature
    """
    arcpy.env.overwriteOutput = 1
    gdb = arcpy.env.scratchGDB
    fc = os.path.join(gdb, "temp_fc")
    arcpy.CopyFeatures_management(shp, fc)
    arcpy.AddField_management(fc, "utmzone", field_type = "TEXT", field_length = 1000)

    # Execute CalculateUTMZone
    arcpy.CalculateUTMZone_cartography(fc, "utmzone")

    prj = [row[0] for row in arcpy.da.SearchCursor(fc, "utmzone")][0]
    return prj

The output looks like the following (i.e. the same as a .prj file):

PROJCS["WGS 84 UTM Zone 18L (Calculated)",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",10000000.0],PARAMETER["Central_Meridian",-75.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]

When I try to create a Spatial Reference object from the output I get a generic RuntimeError: ERROR 999999: Error executing function. error:

shp = r'C:\path\to\myshapefile.shp'
sr = find_utm_zone(shp)
print arcpy.SpatialReference(sr)

What is the preferred method of creating a Spatial Reference object from the Calculate UTM Zone output?

3

One of the options for arcpy.SpatialReference is a prj file, so write your output to a temp prj and read that in. Something like:

shp = r'C:\path\to\myshapefile.shp'
sr = find_utm_zone(shp)

myprj = r"myTempPrj.prj"

with open(myprj, "w") as prjFile:
    prjFile.write(sr)

print arcpy.SpatialReference(myprj)

Which for me returns:

<geoprocessing spatial reference object object at 0x000000000D2E29B0>

Then just delete your temporary prj file when it's finished.

  • 1
    Thanks, this works, although I needed to replace print(sr, file=prjFile) with prjFile.write(sr). – Aaron Oct 13 '16 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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