1

This should be simple. I hope I don't get down votes!

I just got the following code from an article about using Google Maps API and ArcGIS to calculate routes without network analysis and just can't get it to run. I am getting a syntax error on line 7. I have tried to type in other values before it and they are fine, also tried the to change the value "fromFile" to something else and it still gives the same error.

Any ideas?

#Estimating O–D travel time matrix by Google Maps API: implementation, advantages, and implications
#http://dx.doi.org/10.1080/19475683.2011.625977
import arcpy
import urllib 
import time 
from xml.etree.ElementTree import XML, fromstring, tostring
fromFile = arcpy.GetParameterAsText(0)
toFile = arcpy.GetParameterAsText(1)
Resultfile = arcpy.GetParameterAsText(2) 
Result = open(Resultfile,”w”) 
Result.write(“FromFID,toFID,TravelTime”) 
Result.write(“\n”) 
fromCursor = arcpy.SearchCursor(fromFile) 
toCursor = arcpy.SearchCursor(toFile) 
fromRow = fromCursor.reset() 
fromRow = fromCursor.next() 
while (fromRow!=None): 
fromX = fromRow.shape.centroid.X 
fromY = fromRow.shape.centroid.Y 
fromFID = fromRow.FID 
arcpy.AddMessage(str(fromFID)) 
toCursor = arcpy.SearchCursor(toFile) 
toRow = toCursor.reset() 
toRow = toCursor.next() 
while (toRow!=None): 
toX = toRow.shape.centroid.X 
toY = toRow.shape.centroid.Y 
toFID = toRow.FID 
arcpy.AddMessage(str(toFID)) 
googletext = “http://maps.googleapis.com/maps/api/directions/xml?origin=(“ + str(fromY) + “,” + str(fromX) + “) &destination=(“ + str(toY) + “,” + str(toX) + “) &sensor=false” 
time.sleep(3) 
xmlfile = urllib.urlopen(googletext) 
xml = xmlfile.read() 
value = “NA” 
dom = fromstring(xml) 
nodelist = dom.getchildren() 
if (nodelist[0].text =  = “OK”): 
arcpy.AddMessage(nodelist[0].text) 
route=nodelist[1] 
leg=route.getchildren()[1] 
duration = leg.find(“duration”) 
value = duration.getchildren()[0].text 
else: 
arcpy.AddError(nodelist[0].text) 
Result.write(str(fromFID)) 
Result.write(“,”) 
Result.write(str(toFID)) 
Result.write(“,”) 
Result.write(value) 
Result.write(“|n”) 
toRow = toCursor.next() 
fromRow = fromCursor.next() 
Result.close() 
del fromCursor 
del toCursor

closed as off-topic by Nathan W, whuber Mar 27 '14 at 2:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development are off-topic here unless they relate directly to Geographical Information Systems, but they can be asked on Stack Overflow." – Nathan W, whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You're too vague on what the error states and what type of data are the fromFile and toFile? Point locations? – ianbroad Mar 27 '14 at 1:02
  • @ian It just say's Syntax Error - invalid syntax (DrivingDistance.py, line 7). Also in PyScripter it highlights the fromFile = section of the line and when you hover over it, it say Syntax Error: Invalid Syntax – GeorgeC Mar 27 '14 at 1:05
  • 3
    You have some indentation issues here. Python is heavily dependent on consistent indentation. Your while loops and if statements need to have indentation. Also, are you using this as a stand-alone script? Or a script tool? arcpy.GetParameterAsText(0) will only work in a tool. – Fezter Mar 27 '14 at 1:29
  • @Fezter -thanks. I am just correcting the indentation. – GeorgeC Mar 27 '14 at 1:35
  • 2
    There are several kinds of double quotation marks appearing, but they are not used consistently, such as immediately after line 7. Those will likely be interpreted as syntax errors. – whuber Mar 27 '14 at 2:29
3

You have some type of encoding issue. If I take and copy/paste your script into Notepad++ the characters are messed up. And the first error is right on line 7 where you get the syntax error.

If you copy/pasted that code from somewhere, I'd say you need to retype it or get it out of the encoding it's in.

When I tried to save it using normal notepad into ANSI it tells me the characters are in Unicode.

Here's an example of what I see:

enter image description here

I have it cleaned up here, but that website didn't format the indenting correctly for the while loops and if statements. I'm not sure what is suppose to be part of what. But this should help you for now.

#Estimating O–D travel time matrix by Google Maps API: implementation, advantages, and implications
#http://dx.doi.org/10.1080/19475683.2011.625977
import arcpy
import urllib 
import time 
from xml.etree.ElementTree import XML, fromstring, tostring
fromFile = arcpy.GetParameterAsText(0)
toFile = arcpy.GetParameterAsText(1)
Resultfile = arcpy.GetParameterAsText(2) 
Result = open(Resultfile, "w") 
Result.write("FromFID,toFID,TravelTime") 
Result.write("\n") 
fromCursor = arcpy.SearchCursor(fromFile) 
toCursor = arcpy.SearchCursor(toFile) 
fromRow = fromCursor.reset() 
fromRow = fromCursor.next() 
while (fromRow!=None): 
fromX = fromRow.shape.centroid.X 
fromY = fromRow.shape.centroid.Y 
fromFID = fromRow.FID 
arcpy.AddMessage(str(fromFID)) 
toCursor = arcpy.SearchCursor(toFile) 
toRow = toCursor.reset() 
toRow = toCursor.next() 
while (toRow!=None): 
toX = toRow.shape.centroid.X 
toY = toRow.shape.centroid.Y 
toFID = toRow.FID 
arcpy.AddMessage(str(toFID)) 
googletext = "http://maps.googleapis.com/maps/api/directions/xml?origin=(" + str(fromY) + "," + str(fromX) + ") &destination=(" + str(toY) + "," + str(toX) + ") &sensor=false" 
time.sleep(3) 
xmlfile = urllib.urlopen(googletext) 
xml = xmlfile.read() 
value = "NA" 
dom = fromstring(xml) 
nodelist = dom.getchildren() 
if (nodelist[0].text == "OK"): 
arcpy.AddMessage(nodelist[0].text) 
route=nodelist[1] 
leg=route.getchildren()[1] 
duration = leg.find("duration") 
value = duration.getchildren()[0].text 
else: 
arcpy.AddError(nodelist[0].text) 
Result.write(str(fromFID)) 
Result.write(",") 
Result.write(str(toFID)) 
Result.write(",") 
Result.write(value) 
Result.write("|n") 
toRow = toCursor.next() 
fromRow = fromCursor.next() 
Result.close() 
del fromCursor 
del toCursor

This is my guess with only taking a second to look at it:

#Estimating O–D travel time matrix by Google Maps API: implementation, advantages, and implications
#http://dx.doi.org/10.1080/19475683.2011.625977
import arcpy
import urllib 
import time 
from xml.etree.ElementTree import XML, fromstring, tostring

fromFile = arcpy.GetParameterAsText(0)
toFile = arcpy.GetParameterAsText(1)
Resultfile = arcpy.GetParameterAsText(2) 

Result = open(Resultfile, "w") 
Result.write("FromFID,toFID,TravelTime") 
Result.write("\n") 

fromCursor = arcpy.SearchCursor(fromFile) 
toCursor = arcpy.SearchCursor(toFile) 
fromRow = fromCursor.reset() 
fromRow = fromCursor.next() 

while (fromRow!=None): 
    fromX = fromRow.shape.centroid.X 
    fromY = fromRow.shape.centroid.Y 
    fromFID = fromRow.FID 
    arcpy.AddMessage(str(fromFID)) 
    toCursor = arcpy.SearchCursor(toFile) 
    toRow = toCursor.reset() 
    toRow = toCursor.next() 

    while (toRow!=None): 
        toX = toRow.shape.centroid.X 
        toY = toRow.shape.centroid.Y 
        toFID = toRow.FID 
        arcpy.AddMessage(str(toFID)) 
        googletext = "http://maps.googleapis.com/maps/api/directions/xml?origin=(" + str(fromY) + "," + str(fromX) + ") &destination=(" + str(toY) + "," + str(toX) + ") &sensor=false" 
        time.sleep(3) 
        xmlfile = urllib.urlopen(googletext) 
        xml = xmlfile.read() 
        value = "NA" 
        dom = fromstring(xml) 
        nodelist = dom.getchildren() 

        if (nodelist[0].text == "OK"): 
            arcpy.AddMessage(nodelist[0].text) 
            route=nodelist[1] 
            leg=route.getchildren()[1] 
            duration = leg.find("duration") 
            value = duration.getchildren()[0].text 
        else: 
            arcpy.AddError(nodelist[0].text) 
            Result.write(str(fromFID)) 
            Result.write(",") 
            Result.write(str(toFID)) 
            Result.write(",")
            Result.write(value) 
            Result.write("|n") 

    toRow = toCursor.next() 
    fromRow = fromCursor.next() 

Result.close() 
del fromCursor 
del toCursor
  • Thanks, I see the same thing now when I open it in notepad ++. I don't understand why, it is just copy pasted from tandfonline.com/doi/full/10.1080/… (scroll to the end). I am trying to figure out how to convert it to ascii automatically. – GeorgeC Mar 27 '14 at 1:28
  • I correct the encoding, now you just need to figure out what's included in the 2 while loops and if statements. – ianbroad Mar 27 '14 at 1:32
  • -thank. I corrected the encoding by replacing the little block and the wierd " signs. Am just going through the indentation issues as well. – GeorgeC Mar 27 '14 at 1:36
  • No problem, glad I could help. – ianbroad Mar 27 '14 at 1:50
  • It seems to run -outputs 0 and Ok but hasn't completed yet. I will contact the author and see if they can help. It will be a very useful process. – GeorgeC Mar 27 '14 at 2:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.