2

very new to scripting and I have been struggling with my script for a while. I'm not sure why it doesn't work in field calculator. I am trying to calculate a list of distance between two long lat points. The following is the code that I have implemented in the Pre-Logic Script Code for FieldName1. I am currently using ArcGIS 10.1

def Distance(Latitude, Latitude2, Longitude, Longitude2)
    Longitude, Latitude, Longitude2, Latitude2 = map(math.radians, [Longitude, Latitude, Longitude2, Latitude2])
    dlong = Longitude2-Longitude
    dlat = Latitude2 - Latitude
    a = math.sin(dlat/2)**2 + math.cos(Latitude * math.cos(Latitude2) * math.sin(dlong/2)**2
    c = 2 * math.asin(math.sqrt(a))
    Distance = 6371 * C * 1000

The following are sample points for reference: Latitude :48.5761 Longitude : -124.31111 Latitude 2: 48.57611 Longitude 2: -124.31111

I kept getting error 000989: Python syntax error: Parsing error SyntaxError:invalid syntax(line1). It also adds that the parameter are not valid.

3

According to this tutorial the map function returns a list:

>>> items = [1, 2, 3, 4, 5]
>>> 
>>> def sqr(x): return x ** 2

>>> list(map(sqr, items))
[1, 4, 9, 16, 25]
>>> 

So your code should be something like this:

def Distance(Latitude, Latitude2, Longitude, Longitude2)
    pList = map(math.radians, [Longitude, Latitude, Longitude2, Latitude2])
    Longitude  = pList[0]
    Latitude   = pList[1]
    Longitude2 = pList[2]
    Latitude2  = pList[3]
    dlong = Longitude2-Longitude
    dlat = Latitude2 - Latitude
    a = math.sin(dlat/2)**2 + math.cos(Latitude * math.cos(Latitude2) * math.sin(dlong/2)**2
    c = 2 * math.asin(math.sqrt(a))
    Distance = 6371 * C * 1000

Which would be the same as writing:

def Distance(Latitude, Latitude2, Longitude, Longitude2)
    Longitude  = math.radians(Longitude)
    Latitude   = math.radians(Latitude)
    Longitude2 = math.radians(Longitude2)
    Latitude2  = math.radians(Latitude2)
    dlong = Longitude2-Longitude
    dlat = Latitude2 - Latitude
    a = math.sin(dlat/2)**2 + math.cos(Latitude * math.cos(Latitude2) * math.sin(dlong/2)**2
    c = 2 * math.asin(math.sqrt(a))
    Distance = 6371 * C * 1000

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.