How to Geocode a Simple Address Using Data Science Toolbox

How to GeoCode a simple address using Data Science Toolbox

Like this:

library(httr)
library(rjson)

data <- paste0("[",paste(paste0("\"",dff$address,"\""),collapse=","),"]")
url <- "http://www.datasciencetoolkit.org/street2coordinates"
response <- POST(url,body=data)
json <- fromJSON(content(response,type="text"))
geocode <- do.call(rbind,sapply(json,
function(x) c(long=x$longitude,lat=x$latitude)))
geocode
# long lat
# San Francisco, California, United States -117.88536 35.18713
# Mobile, Alabama, United States -88.10318 30.70114
# La Jolla, California, United States -117.87645 33.85751
# Duarte, California, United States -118.29866 33.78659
# Little Rock, Arkansas, United States -91.20736 33.60892
# Tucson, Arizona, United States -110.97087 32.21798
# Redwood City, California, United States -117.88536 35.18713
# New Haven, Connecticut, United States -72.92751 41.36571
# Berkeley, California, United States -122.29673 37.86058
# Hartford, Connecticut, United States -72.76356 41.78516
# Sacramento, California, United States -121.55541 38.38046
# Encinitas, California, United States -116.84605 33.01693
# Birmingham, Alabama, United States -86.80190 33.45641
# Stanford, California, United States -122.16750 37.42509
# Orange, California, United States -117.85311 33.78780
# Los Angeles, California, United States -117.88536 35.18713

This takes advantage of the POST interface to the street2coordinates API (documented here), which returns all the results in 1 request, rather than using multiple GET requests.

The absence of Phoenix seems to be a bug in the street2coordinates API. If you go the API demo page and try "Phoenix, Arizona, United States", you get a null response. However, as your example shows, using their "Google-style Geocoder" does give a result for Phoenix. So here's a solution using repeated GET requests. Note that this runs much slower.

geo.dsk <- function(addr){ # single address geocode with data sciences toolkit
require(httr)
require(rjson)
url <- "http://www.datasciencetoolkit.org/maps/api/geocode/json"
response <- GET(url,query=list(sensor="FALSE",address=addr))
json <- fromJSON(content(response,type="text"))
loc <- json['results'][[1]][[1]]$geometry$location
return(c(address=addr,long=loc$lng, lat= loc$lat))
}
result <- do.call(rbind,lapply(as.character(dff$address),geo.dsk))
result <- data.frame(result)
result
# address long lat
# 1 Birmingham, Alabama, United States -86.801904 33.456412
# 2 Mobile, Alabama, United States -88.103184 30.701142
# 3 Phoenix, Arizona, United States -112.0733333 33.4483333
# 4 Tucson, Arizona, United States -110.970869 32.217975
# 5 Little Rock, Arkansas, United States -91.207356 33.608922
# 6 Berkeley, California, United States -122.29673 37.860576
# 7 Duarte, California, United States -118.298662 33.786594
# 8 Encinitas, California, United States -116.846046 33.016928
# 9 La Jolla, California, United States -117.876447 33.857515
# 10 Los Angeles, California, United States -117.885359 35.187133
# 11 Orange, California, United States -117.853112 33.787795
# 12 Redwood City, California, United States -117.885359 35.187133
# 13 Sacramento, California, United States -121.555406 38.380456
# 14 San Francisco, California, United States -117.885359 35.187133
# 15 Stanford, California, United States -122.1675 37.42509
# 16 Hartford, Connecticut, United States -72.763564 41.78516
# 17 New Haven, Connecticut, United States -72.927507 41.365709

Pass Data for Querying an API in R

You need to reformat your addresses so they can be read by the API.

Looks like they were replacing the commas with %2c+, and the spaces with +.

library(httr)
library(tidyverse)

base_url <- "http://www.datasciencetoolkit.org/street2coordinates/"
look_up <- c("4600+Vegas+Dr%2c+Las+Vegas%2c+NV+89108", "3600+Vegas+Dr%2c+Las+Vegas%2c+NV+89108", "2543+Graystone+Place%2c+Simi+Valley%2c+CA+93065")

full_url <- map2_chr(base_url, look_up, paste0)

out <- map(full_url, GET) %>%
map_dfr(., ~ {
content(., as = 'parsed') %>%
unlist() %>%
enframe() %>%
extract(col = name, into = "key", regex = "([^.]*$)", remove = FALSE)
})

Hope this helps.

How to geocode using Google API Key in R ggmap as of 2019

Have you tried this link here?

Note: it is written by SmartyStreets, a geocoding API, so it is biased. But it does compare and contrast some alternatives with their tradeoffs:

•Google

•TAMU

•Loqate

•Geocodio

•SmartyStreets

Also worth noting (link)

How to Geocode address from multiple models?

In your model you need to have the association to your countries and states tables
I am assuming that you are saving the state id and country_id

class User < ActiveRecord::Base
belongs_to :state
belongs_to :country

geocoded_by :address
def address
[local_address, state.name, country.name].compact.join(', ')
end
end

RDSTK: Reverse geocode lat/lon to city (using coordinates2politics)

Sound cool. I've had some trouble with RDSTK lately... I'm assuming the stock server is no longer working for you, as the author's blog describes. Too bad.

Here are two workarounds. You might be able to take the original lat/lon pairs, using the city places file from tigerfile, and use %over% in the sp package and then pull the name from the returned shape. That should be faster than repeated calls to an API.

However, I've got the same need for an open geocoder in R, and there are a few options. Check out ggmap, referenced in LukeA's answer - can use DSTK (now defunct) and is a simple interface to the google API for just a few calls. Also see this fantstic post describing how to use the census bureau's geocoder API. Write a little wrapper function to handle the JSON and you're good to go. That code worked for me as of 1/1/2016.

Geocoding Address name from Database using leaflet esri plugin

Here is an example using ES6:

import L from "leaflet";
// import library as ELG
import * as ELG from "esri-leaflet-geocoder";

// here is an example address in the US - use the one from your DB
const address = "380 New York St, Redlands, California, 92373";

// call geocode method of the library, no need to call L.esri.Geocoding.geocode() as in vanilla js
ELG.geocode()
// pass the address
.text(address)
.run((err, results, response) => {
console.log(results.results[0].latlng);
// retrieve latitude, longitude from related response
const { lat, lng } = results.results[0].latlng;
// build a marker using the retrieved address
L.marker([lat, lng])
.addTo(mymap)
.bindPopup(address)
.openPopup();
});

Demo



Related Topics



Leave a reply



Submit