Skip to content

Instantly share code, notes, and snippets.

@mfunk
Created August 1, 2018 18:35
Show Gist options
  • Save mfunk/0e89e4f0280deb2e73ab3690d4bfd0cb to your computer and use it in GitHub Desktop.
Save mfunk/0e89e4f0280deb2e73ab3690d4bfd0cb to your computer and use it in GitHub Desktop.
Returns ArcPy spatial reference for input lat/long location.
def selectUTMZone(self, longitude, latitude):
'''
return UTM/UPS Zone spatial reference based on longitude and latitude
* Datum is GCS_WGS_1984
Based on gis.stackexchange.com referenced on 1/24/2017:
http://gis.stackexchange.com/questions/13291/computing-utm-zone-from-lat-long-point
Inputs:
longitude: latitude (assume GCS WGS 1984) of UTM zone
latitude: longitude (assume GCS WGS 1984) of UTM zone
Output:
targetSpatialReference: Spatial Reference object of UTM zone of input lon/lat.
'''
try:
targetSpatialReference = None
if latitude > 84.0:
arcpy.AddMessage("Area above 84.0 degrees within North polar zone. Using Universal Polar Stereographic North projection.")
srName = "UPS North"
elif latitude < -80.0:
arcpy.AddMessage("Area below -80.0 degrees within South polar zone. Using Universal Polar Stereographic South projection.")
srName = "UPS South"
else:
z = None
hemisphere = "N"
if latitude < 0.0:
hemisphere = "S"
if (latitude >= 56.0 and latitude < 64.0 and longitude >= 3.0 and longitude < 12.0):
z = 32
elif (latitude >= 72.0 and latitude < 84.0):
if (longitude >= 0.0 and longitude < 9.0):
z = 31
elif (longitude >= 9.0 and longitude < 21.0):
z = 33
elif (longitude >= 21.0 and longitude < 33.0):
z = 35
elif (longitude >= 33.0 and longitude < 42.0):
z = 37
else:
z = int(math.floor((longitude + 180.0)/6.0) + 1)
srName = "WGS 1984 UTM Zone {0}{1}".format(z, hemisphere)
arcpy.AddMessage("Using {0} for {1},{2}.".format(srName, longitude, latitude))
targetSpatialReference = arcpy.SpatialReference(srName)
return targetSpatialReference
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages() + "\n"
err = "{0}\n{1}".format(pymsg, msgs)
raise Exception(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment