7

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.

Andrew Brēza
  • 327
  • 1
  • 6
  • That data should exists already, try contacting local government or buy it from some map company like google maps – ssch Feb 04 '13 at 21:51
  • 4
    You can use Mathematica to implement any Web-based method described on the GIS site. For instance, about 20 months ago I searched that site for solutions and implemented two of them in MMA--Google Maps and Yahoo Maps. These are contractually limited to a few thousand per day (which was good enough for me), but there are others with fewer limitations. The GIS community can also provide advice on commercial solutions. – whuber Feb 04 '13 at 22:44
  • 2
    This exists within Wolfram Language now: FindGeoLocation. – Carl Lange Feb 02 '20 at 17:11
  • Thanks @CarlLange! – Andrew Brēza Feb 10 '20 at 15:57

1 Answers1

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