0

I am trying to insert a bearing string into an attribute table, I want the string to look like this -> S 58°15'31" W , but what I am getting in the table is this -> S 58°15'31" W

The code I am using is in a ArcMap toolbox.tbx

Here is the function I am using to create the bearing string

def returnBearingString(azimuth):
bearing=None
dmsbearing=None
if azimuth>270 and azimuth<=360:
 bearing = 360 - azimuth
 dmsbearing=ddToDms(bearing)
 dmsbearing = """N {0}°{1}'{2}" W""".format(int(dmsbearing[0]),int(dmsbearing[1]),int(round(dmsbearing[2],0)))
 return dmsbearing
if azimuth>=0 and azimuth<=90:
 dmsbearing=ddToDms(azimuth)
 dmsbearing = """N {0}°{1}'{2}" E""".format(int(dmsbearing[0]),int(dmsbearing[1]),int(round(dmsbearing[2],0)))
 return dmsbearing
if azimuth>90 and azimuth<=180:
 bearing= 180 - azimuth
 dmsbearing=ddToDms(bearing)
 dmsbearing = """S {0}°{1}'{2}" E""".format(int(dmsbearing[0]),int(dmsbearing[1]),int(round(dmsbearing[2],0)))
 return dmsbearing
if azimuth>180 and azimuth<=270:
 bearing = azimuth-180
 dmsbearing=ddToDms(bearing)
 dmsbearing = """S {0}°{1}'{2}" W""".format(int(dmsbearing[0]),int(dmsbearing[1]),int(round(dmsbearing[2],0)))
 return dmsbearing

Here is the function I am using to convert dd to dms

def ddToDms(dd):
negative = dd<0
dd=abs(dd)
minutes,seconds = divmod(dd*3600,60)
degrees,minutes = divmod(minutes,60)
if negative:
 if degrees>0:
 degrees = -degrees
 elif minutes>0:
 minutes = -minutes
 else:
 seconds = -seconds
return (int(degrees),int(minutes),round(seconds,3))

Here is how I am using it to write to feature class.

ic = arcpy.da.InsertCursor(leaseRoadSegment.name,["OBJECTID","SHAPE@","BEARING_STRING"])
bear = returnBearingString(az)
newRow = 6000,polyShape,bear
ic.insertRow(newRow)

where the azimuth is just a number between 0 and 360.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 8, 2019 at 20:48
1

1 Answer 1

1

I had forgotten to add the Header

#!/usr/bin/python
# -*- coding: utf-8 -*-

before I added the script to the toolbox, I added the header after and it didn't affect the encoding until I deleted and then added the script to the toolbox again,

answered Jun 9, 2019 at 18:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.