我正在尝试使用下面的脚本对一堆邮政编码进行地理编码以获取其坐标 由于我主要尝试转换德语邮政编码,因此我使用国家ISO代码作为前缀,以确保Google不会返回美国地区。
我的数据如下:
DE-01099
DE-01108
DE-01109
这些邮政编码位于德国德累斯顿附近。现在,当查询DE-01099
时,即在GoogleMaps中,它会显示德累斯顿。同样的事情,当我对其进行地理编码(http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=DE-01099
)时,它返回“德累斯顿”。
在GoogleMaps中搜索DE-01108
也会显示德累斯顿。但是,在对此邮政编码进行地理编码时,它会返回Springfield, MA 01108, USA
,即使我明确地说DE-
。
知道为什么/做什么? 提前谢谢!
import urllib
import sqlite3
import json
import time
import ssl
serviceurl = "http://maps.googleapis.com/maps/api/geocode/json?"
# Deal with SSL certificate anomalies Python > 2.7
# scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
scontext = None
conn = sqlite3.connect('geodata.sqlite')
cur = conn.cursor()
cur.execute('''
CREATE TABLE IF NOT EXISTS Locations (address TEXT, geodata TEXT)''')
fh = open("where2.data")
for line in fh:
address = line.strip()
print ''
cur.execute("SELECT geodata FROM Locations WHERE address= ?", (buffer(address), ))
# try:
# data = cur.fetchone()[0]
# print "Found in database ",address
# continue
# except:
# pass
print 'Resolving', address
url = serviceurl + urllib.urlencode({"sensor":"false", "address": address})
print 'Retrieving', url
uh = urllib.urlopen(url, context=scontext)
data = uh.read()
print 'Retrieved',len(data),'characters',data[:20].replace('\n',' ')
try:
js = json.loads(str(data))
# print js # We print in case unicode causes an error
except:
continue
if 'status' not in js or (js['status'] != 'OK' and js['status'] != 'ZERO_RESULTS') :
print '==== Failure To Retrieve ===='
print data
break
cur.execute('''INSERT INTO Locations (address, geodata)
VALUES ( ?, ? )''', ( buffer(address),buffer(data) ) )
conn.commit()
time.sleep(0.5)
答案 0 :(得分:1)
您可能希望在请求中使用component filtering。将&components=country:DE
添加到请求中,而不是将“DE-”附加到邮政编码:
http://maps.googleapis.com/maps/api/geocode/json?address=01108&components=country:DE
返回德累斯顿的一个结果:
{
"results" : [
{
"address_components" : [
{
"long_name" : "01108",
"short_name" : "01108",
"types" : [ "postal_code" ]
},
{
"long_name" : "Weixdorf",
"short_name" : "Weixdorf",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Dresden",
"short_name" : "Dresden",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Dresden",
"short_name" : "DD",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Sachsen",
"short_name" : "SN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Germany",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "01108 Dresden, Germany",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.17815,
"lng" : 13.8371901
},
"southwest" : {
"lat" : 51.1285391,
"lng" : 13.742293
}
},
"location" : {
"lat" : 51.1443239,
"lng" : 13.7997057
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.17815,
"lng" : 13.8371901
},
"southwest" : {
"lat" : 51.1285391,
"lng" : 13.742293
}
}
},
"place_id" : "ChIJedA1NUPMCUcRsJIzlc6xIRw",
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
答案 1 :(得分:0)
请注意,地址参数中的“DE-”前缀不适用任何严格过滤器。如果您需要严格的过滤器,则必须使用@geocodezip
建议的component filtering您的请求将有一个表单
所以你说地理编码服务:“请在德国找到邮政编码01099”。