Reputation: 28892
I am trying to geocode a bunch of zip codes to get their coordinates using the script below.
Since I'm mostly trying to convert German zip codes, I am using the country ISO code as prefix to make sure Google doesn't return US locations.
My data looks as follows:
DE-01099
DE-01108
DE-01109
These zip codes are near Dresden, Germany. Now, when looking up DE-01099
i.e. in GoogleMaps, it shows Dresden, as it should. Same thing, when I geocode it (http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=DE-01099
), it returns "Dresden".
Searching for DE-01108
in GoogleMaps also shows Dresden, as supposed to. However, when geocoding this zip code, it returns Springfield, MA 01108, USA
, even though I explicitely said DE-
.
Any idea why / what to do?
Thanks in advance!
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)
Upvotes: 0
Views: 302
Reputation: 32100
Please note that "DE-" prefix in address parameter doesn't apply any strict filter. If you need a strict filter you must use a component filtering as suggested by @geocodezip
Your request will have a form
So you say to geocoding service: "please find postal code 01099 in Germany".
Upvotes: 0
Reputation: 161324
You probably want to use component filtering on your request. Add &components=country:DE
to the request rather than appending "DE-" to the zip code:
http://maps.googleapis.com/maps/api/geocode/json?address=01108&components=country:DE
returns the one result for Dresden:
{
"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"
}
Upvotes: 1