Graphics Programs Reference
In-Depth Information
print “Address,City,State,Zip Code,Latitude,Longitude”
for row in costcos:
full_addy = row[1] + “,” + row[2] + “,” + row[3] + “,” + row[4]
place, (lat, lng) = list(g.geocode(full_addy, exactly_one=False))[0]
print full_addy + “,” + str(lat) + “,” + str(lng)
That's it. Run the Python script, and save the output as costcos-geocoded.csv .
Here's what the first few lines of the data looks like:
Address,City,State,Zip Code,Latitude,Longitude
1205 N. Memorial Parkway,Huntsville,Alabama,35801-5930,34.7430949,-86
.6009553
3650 Galleria Circle,Hoover,Alabama,35244-2346,33.377649,-86.81242
8251 Eastchase Parkway,Montgomery,Alabama,36117,32.363889,-86.150884
5225 Commercial Boulevard,Juneau,Alaska,99801-7210,58.3592,-134.483
330 West Dimond Blvd,Anchorage,Alaska,99515-1950,61.143266,-149.884217
...
Pretty cool. By some stroke of luck, latitude and longitude coordinates are
found for every address. That usually doesn't happen. If you do run into
that problem, you should add error checking at the second to last line of
the preceding script.
try:
place, (lat, lng) = list(g.geocode(full_addy, exactly_one=False))
[0]
print full_addy + “,” + str(lat) + “,” + str(lng)
except:
print full_addy + “,NULL,NULL”
This tries to find the latitude and longitude coordinates, and if it fails, it
prints the row with the address and null coordinate values. Run the Python
script and save the output as a file, and you can go back and look for the
nulls. You can either try a different service for the missing addresses via
Geopy, or you can just manually enter the addresses in Geocoder.us.
Just Points
Now that you have points with latitude and longitude, you can map them.
The straightforward route is to do the computer equivalent of putting
pushpins in a paper map on a billboard. As shown in the framework in Fig-
ure 8-1, you place a marker for each location on the map.
Search WWH ::




Custom Search