Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-遍历地址表,使用函数进行地理定位,将lat/long复制到行?_Python_Geolocation_Arcpy - Fatal编程技术网

Python-遍历地址表,使用函数进行地理定位,将lat/long复制到行?

Python-遍历地址表,使用函数进行地理定位,将lat/long复制到行?,python,geolocation,arcpy,Python,Geolocation,Arcpy,我有一个地址表,我需要Python脚本使用我的Google API Geologite函数返回每个地址的lat/long坐标,并为每个地址添加到同一行中的新字段。geocode函数工作正常-我无法让脚本遍历表中的每一行,将地址添加到函数中,然后将输出lat/long复制到同一行的字段中。以下是我所拥有的: import urllib, json, time, arcpy arcpy.env.workspace = "D:/GIS/addr.dbf" #sets variables, adds

我有一个地址表,我需要Python脚本使用我的Google API Geologite函数返回每个地址的lat/long坐标,并为每个地址添加到同一行中的新字段。geocode函数工作正常-我无法让脚本遍历表中的每一行,将地址添加到函数中,然后将输出lat/long复制到同一行的字段中。以下是我所拥有的:

import urllib, json, time, arcpy

arcpy.env.workspace = "D:/GIS/addr.dbf"

#sets variables, adds new field to hold lat/long coordinates
fc = 'addr.dbf'
field1 = 'address'
field2 = 'loc'
arcpy.AddField_management(fc, field2, "TEXT")


#function that uses Google API to geolocate- this part works consistently
def geolocate(address, 
api="key_here",delay=4):
  base = r"https://maps.googleapis.com/maps/api/geocode/json?"
  addP = "address=" + address.replace(" ","+")
  gUrl = base + addP + "&key=" + api
  response = urllib.urlopen(gUrl)
  jres = response.read()
  jData = json.loads(jres)
  if jData['status'] == 'OK':
    resu = jData['results'][0]
    finList = [resu['formatted_address'],resu['geometry']['location'] 
    ['lat'],resu['geometry']['location']['lng']]
  else:
    finList = [None,None,None]
  time.sleep(delay)
  return finList


#adds address field as text to geolocate in function, adds output lat/long 
#(indexed locations 0 and 1 from the finList output)
##this is the part that doesn't work!
geo = geolocate(address = field1)
cursor = arcpy.UpdateCursor(fc, [field1, field2])
for row in cursor:
  field2 = geo[0], geo[1]
  cursor.updateRow(row);

您正在使用字符串“address”调用geologite函数

field1='address'
地理位置=地理位置(地址=字段1)
您希望使用实际地址调用geolocate,这意味着您需要在遍历游标的循环中执行此操作。所以应该是这样的:

fields = [field1, field2])
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        address = row[0]
        geo = geolocate(address = address)
        row[1] = geo[0], geo[1]
        cursor.updateRow(row)
注:我使用了ArcGIS 10.1中引入的数据访问模块中的光标。我还为游标使用了“with”语法,以便它自动处理删除游标的操作