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.

0

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,

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.