Solved! Go to Solution.
"%4i %2i %5.3f %3i %2i %5.3f " % dd2dms(!LATDD!,!LONDD!)
def dd2dms(dd1,dd2,ndec=6): """Convert a decimal degree coordinate pair to a six-tuple of degrees, minutes seconds. The returned values are not rounded. Arguments dd1, dd2 - coordinate pair, in decimal degrees Example >>> dd2dms(-74.25,32.1) (-74, 15, 6.9444444444444444e-05, 32, 6, 2.7777777777778172e-05) """ # Author: Curtis Price, http://profile.usgs.gov/cprice # Disclaimer: Not approved by USGS. (Provisional, subject to revision.) def ToDMS(dd): dd1 = abs(float(dd)) cdeg = int(dd1) minsec = dd1 - cdeg cmin = int(minsec * 60) csec = (minsec % 60) / float(3600) if dd < 0: cdeg = cdeg * -1 return cdeg,cmin,csec try: # return a six-tuple return ToDMS(dd1) + ToDMS(dd2) except: raise Exception, "Invalid input" def dms2dd(deg1,min1,sec1,deg2,min2,sec2): """Convert a degrees-minutes seconds coordinate pair to decimal degrees. The returned values are not rounded. Arguments deg1,min1,sec1,deg2,min2,sec2 - DMS coordinate pair (six values) Example >>> dms2deg(-74,45,0,34,10,20) (-74.75, 34.172222222222217) """ # Author: Curtis Price, http://profile.usgs.gov/cprice # Disclaimer: Not approved by USGS. (Provisional, subject to revision.) def ToDD(deg,min=0,sec=0): dd = abs(deg) + min / 60.0 + sec / 3600.0 if deg < 0: dd = dd * -1.0 return dd try: return ToDD(deg1,min1,sec1), ToDD(deg2,min2,sec2) except Exception: raise Exception, "Invalid input"
>>> lat = 45.543210 >>> deg = int(lat) >>> min = int((lat-deg)*60) >>> sec = ((lat-deg)*60) - min >>> print deg, min, sec 45 32 0.5926
rows = arcpy.UpdateCursor(input) for row in rows: Xcoord = str(row.X) Xcoord2 = Xcoord.split(".") XDegrees = Xcoord2[0] Xcoord3 = float("." + Xcoord2[1]) Xcoord4 = Xcoord3 * 60 Xcoord5 = str(Xcoord4) Xcoord6 = Xcoord5.split(".") XMinutes = Xcoord6[0] Xcoord7 = float("." + Xcoord6[1]) Xcoord8 = Xcoord7 * 60 Xcoord9 = str(Xcoord8) XSeconds = Xcoord9 DMS = XDegrees + " " + XMinutes + "' " + XSeconds + "''" row.Lat = DMS rows.updateRow(row) del row, rows
rows = arcpy.UpdateCursor(input) for row in rows: lat = int(row.X) deg = int(lat) minutes = int(lat-deg)*60 sec = ((lat-deg)*60) DMS = str(deg) + " " + str(minutes) + "' " + str(sec)+ "''" row.Lat = DMS rows.updateRow(row) del row, rows
def dd2dms(dd1,dd2): """Convert decimal degree value to a tuple of degrees, minutes seconds. Argument dd1, dd2 - value in decimal degrees Example: >>> deg2dms(-74.49999) (-74, 29, 3539.9639999999918) Author: Curtis Price, http://profile.usgs.gov/cprice Disclaimer: Not approved by USGS. (Provisional, subject to revision.) """ def ToDMS(dd): deg = int(abs(dd)) min = int((abs(dd) - deg) * 60) sec = (abs(dd) - (deg - (min / 60.0))) * 3600.0 if dd < 0: deg = deg * -1 return deg,min,sec try: # return a six-tuple return ToDMS(dd1) + ToDMS(dd2) except: raise Exception, "Invalid input" def dms2dd(deg1,min1,sec1,deg2,min2,sec2): """Convert degrees, minutes seconds to decimal degrees values. Argument deg1,min1,sec1,deg2,min2,sec2 - coordinate pair (2 x 3) Example >>> dms2deg(-74,45,0,34,10,20) (-74.75, 34.172222222222217) Author: Curtis Price, http://profile.usgs.gov/cprice Disclaimer: Not approved by USGS. (Provisional, subject to revision.) """ def ToDD(deg,min=0,sec=0): dd = abs(deg) + min / 60.0 + sec / 3600.0 if deg < 0: dd = dd * -1.0 return dd try: return ToDD(deg1,min1,sec1), ToDD(deg2,min2,sec2) except Exception: raise Exception, "Invalid input"
"%4i %2i %5.3f %3i %2i %5.3f " % dd2dms(!LATDD!,!LONDD!)
def dd2dms(dd1,dd2,ndec=6): """Convert a decimal degree coordinate pair to a six-tuple of degrees, minutes seconds. The returned values are not rounded. Arguments dd1, dd2 - coordinate pair, in decimal degrees Example >>> dd2dms(-74.25,32.1) (-74, 15, 6.9444444444444444e-05, 32, 6, 2.7777777777778172e-05) """ # Author: Curtis Price, http://profile.usgs.gov/cprice # Disclaimer: Not approved by USGS. (Provisional, subject to revision.) def ToDMS(dd): dd1 = abs(float(dd)) cdeg = int(dd1) minsec = dd1 - cdeg cmin = int(minsec * 60) csec = (minsec % 60) / float(3600) if dd < 0: cdeg = cdeg * -1 return cdeg,cmin,csec try: # return a six-tuple return ToDMS(dd1) + ToDMS(dd2) except: raise Exception, "Invalid input" def dms2dd(deg1,min1,sec1,deg2,min2,sec2): """Convert a degrees-minutes seconds coordinate pair to decimal degrees. The returned values are not rounded. Arguments deg1,min1,sec1,deg2,min2,sec2 - DMS coordinate pair (six values) Example >>> dms2deg(-74,45,0,34,10,20) (-74.75, 34.172222222222217) """ # Author: Curtis Price, http://profile.usgs.gov/cprice # Disclaimer: Not approved by USGS. (Provisional, subject to revision.) def ToDD(deg,min=0,sec=0): dd = abs(deg) + min / 60.0 + sec / 3600.0 if deg < 0: dd = dd * -1.0 return dd try: return ToDD(deg1,min1,sec1), ToDD(deg2,min2,sec2) except Exception: raise Exception, "Invalid input"