I am writing a research proposal for a project on voter turnout as related to proximity to mass transit. The first part of my project would require geocoding hundreds of thousands of addresses, but I'd rather not have my laptop running ArcGIS for days on end to do it. My school's supercomputer does not have GIS software but it does have Mathematica Version 8. Is there a way to geocode addresses in Mathematica? I've found several workaround solutions online, but nothing that I think would get the approval of the campus IT office.
Asked
Active
Viewed 1,173 times
7
1 Answers
10
Now Google needs a key for the API. Here is my updated function:
geocoder[address_String] :=
Module[{data, url, results, key = "your_google_api_key"},
url = "https://maps.googleapis.com/maps/api/geocode/json?address="<>URLEncode[address]<>"&sensor=false&key="<>key;
data = Import[url,"RawJSON"];
If[data[["status"]] =!= "OK", data];
results = Association/@Transpose@{
Thread["location_type"-> data[["results", All,"geometry", "location_type"]]]
,Thread["formatted_address"-> data[["results", All, "formatted_address"]]]
,Thread["latlon"-> Values@data[["results", All, "geometry", "location"]]]
};
<|
"status" -> data[["status"]]
,"address_quantity" -> Length@data[["results"]]
, "results" -> results
, "results_original" -> data["results"]
|>
]
After replace your API key in the code above, you can test it like:
geocoder["Av Paulista, 1578, Sao Paulo, Brasil"]["results"]
you get the right coordinates:
{<|
"location_type" -> "ROOFTOP"
,"formatted_address" -> "Av. Paulista, 1578 - Bela Vista, São Paulo - SP, 01310-200, Brazil"
, "latlon" -> {-23.5615, -46.656}|
|>}
Here is the Google Documentation about geocoding, and how to get your API key.
Murta
- 26,275
- 6
- 76
- 166
-
1You can try other geocoding API: 7 Free Geocoding APIs: Google, Bing, Yahoo and MapQuest, APIs are very similar – Tuku Feb 05 '13 at 15:51
-
1I implemented your geocoder into my init.m file and it worked for awhile but now I get the following error message: " Import::noelem: The Import element "1" is not present when importing as JSON. " What has changed? – user43188 Oct 21 '18 at 23:32
FindGeoLocation. – Carl Lange Feb 02 '20 at 17:11