System: ArcGIS 10.3 with Python 2.7.8 on Windows7 x86
Background: Build a Python script do get drive times from the Google Maps DistanceMatrix and copy the results to an attribute table. The script uses the coordinates of two point FC to calculate the distances via simplejson. Quantity of origins and destinations is variable.
Problem:
Script works as it should if I use more then one destination and more then zero origins. Now it could happen, that there is only one destination: The script is running but stops working after "for row in cursor:" (Line 13 in the excerpt of the script). The output table is build but the fields are empty. If I switch origin and destination (so only one origin and many destinations) the script works fine.
How to solve this problem, because the route from origin to destination is (often) unequal to the route from destination to origin?!
... #Before this part only the used varaibles/ parameters are set #and a query table/ table are build if GoogleMapsService == "GoogleMaps Distance Matrix" and FC_mode != "transit": try: #if arcpy.Exists("DistanzMatrix"): fields = [("{0}_BetrNr" .format(FC_start)), ("{0}_Adresse" .format(FC_start)), ("{0}_Gemeinde" .format(FC_start)), ("{0}_XY_Koordinaten" .format(FC_start)), ("{0}_BetrNr" .format(FC_ziel)), ("{0}_Adresse" .format(FC_ziel)), ("{0}_Gemeinde" .format(FC_ziel)), ("{0}_XY_Koordinaten" .format(FC_ziel)), ("Distanz_Kilometer"), ("Distanz_Minuten"), ("Mittlere_Geschwindigkeit_in_km_h"), ("GoogleMapsService") ] with arcpy.da.UpdateCursor("DistanzMatrix", fields) as cursor: for row in cursor: #At this point the script stops working print("{0}".format(row[0])) FC_start_BetrNr = row[0] FC_start_Adresse = row[1] FC_start_Gemeinde = row[2] FC_start_XY_Koordinaten = row[3] FC_ziel_BetrNr = row[4] FC_ziel_Adresse = row[5] FC_ziel_Gemeinde = row[6] FC_ziel_XY_Koordinaten = row[7] url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode={2}&language=de-DE&sensor=false".format(str(FC_start_XY_Koordinaten),str(FC_ziel_XY_Koordinaten),str(FC_mode)) result= simplejson.load(urllib.urlopen(url)) driving_time = float(result['rows'][0]['elements'][0]['duration']['value']) driving_distance = float(result['rows'][0]['elements'][0]['distance']['value']) ... # Some other calculations following after that line except Exception: pass
أكثر...
Background: Build a Python script do get drive times from the Google Maps DistanceMatrix and copy the results to an attribute table. The script uses the coordinates of two point FC to calculate the distances via simplejson. Quantity of origins and destinations is variable.
Problem:
Script works as it should if I use more then one destination and more then zero origins. Now it could happen, that there is only one destination: The script is running but stops working after "for row in cursor:" (Line 13 in the excerpt of the script). The output table is build but the fields are empty. If I switch origin and destination (so only one origin and many destinations) the script works fine.
How to solve this problem, because the route from origin to destination is (often) unequal to the route from destination to origin?!
... #Before this part only the used varaibles/ parameters are set #and a query table/ table are build if GoogleMapsService == "GoogleMaps Distance Matrix" and FC_mode != "transit": try: #if arcpy.Exists("DistanzMatrix"): fields = [("{0}_BetrNr" .format(FC_start)), ("{0}_Adresse" .format(FC_start)), ("{0}_Gemeinde" .format(FC_start)), ("{0}_XY_Koordinaten" .format(FC_start)), ("{0}_BetrNr" .format(FC_ziel)), ("{0}_Adresse" .format(FC_ziel)), ("{0}_Gemeinde" .format(FC_ziel)), ("{0}_XY_Koordinaten" .format(FC_ziel)), ("Distanz_Kilometer"), ("Distanz_Minuten"), ("Mittlere_Geschwindigkeit_in_km_h"), ("GoogleMapsService") ] with arcpy.da.UpdateCursor("DistanzMatrix", fields) as cursor: for row in cursor: #At this point the script stops working print("{0}".format(row[0])) FC_start_BetrNr = row[0] FC_start_Adresse = row[1] FC_start_Gemeinde = row[2] FC_start_XY_Koordinaten = row[3] FC_ziel_BetrNr = row[4] FC_ziel_Adresse = row[5] FC_ziel_Gemeinde = row[6] FC_ziel_XY_Koordinaten = row[7] url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode={2}&language=de-DE&sensor=false".format(str(FC_start_XY_Koordinaten),str(FC_ziel_XY_Koordinaten),str(FC_mode)) result= simplejson.load(urllib.urlopen(url)) driving_time = float(result['rows'][0]['elements'][0]['duration']['value']) driving_distance = float(result['rows'][0]['elements'][0]['distance']['value']) ... # Some other calculations following after that line except Exception: pass
أكثر...