Getting coordinates of cities through an API

130 views Asked by At

I have a list of cities to which I'm missing a latitude and longitude data.

I found an API which after entering a city returns latitude and longitude information: https://rapidapi.com/apininjas/api/geocoding-by-api-ninjas

I´d now like to automate the process (either in Python or R), where I would input the list of cities and it would give me latitude and longitude for every city in a table.

I have this code in R to get a result from an API, but I need to get it to work automatically with the whole list of cities:

req <- request("https://geocoding-by-api-ninjas.p.rapidapi.com/") %>%
 req_url_path("v1/geocoding") %>%
 req_url_query(name = "New York") %>%
 req_headers('X-RapidAPI-Key'='xxxxx',
              'X-RapidAPI-Host'='xxx')

req %>% req_dry_run()

resp <- req %>% 
  req_perform() 

resp %>%
  resp_body_string()

Could you please help with code examples or maybe some smarter way to achieve the result?

2

There are 2 answers

0
IkonoDim On BEST ANSWER

In python it's really easy. You just need to install the library 'requests'. The code would look something like this:

import requests

cities = ["London", "Brooklyn"]
url = "https://geocoding-by-api-ninjas.p.rapidapi.com/v1/geocoding"
headers = {
    "X-RapidAPI-Key": "YOU-API-KEY",
    "X-RapidAPI-Host": "geocoding-by-api-ninjas.p.rapidapi.com"
}

for city in cities:
    response = requests.request("GET", f"{url}?name={city}", headers=headers)
    print(repsonse.text)

2
HoelR On

Depending on what you wish as an output; edit the function. Now it returns a data frame of possible matches.

library(tidyverse)
library(httr2)

cities <- c("New York", "Seattle", "Los Angeles", "New Orleans")

fetch_geo <- function(city_string) {
  request("https://geocoding-by-api-ninjas.p.rapidapi.com/") %>%
    req_url_path("v1/geocoding") %>%
    req_url_query(city = city_string, .multi = "explode") %>%
    req_headers('X-RapidAPI-Key'='YOU-API-KEY',
                'X-RapidAPI-Host'='geocoding-by-api-ninjas.p.rapidapi.com') %>% 
    req_perform() %>% 
    resp_body_json(simplifyVector = TRUE)
}

map(cities, fetch_geo)

[[1]]
             name  latitude  longitude country       state
1 New York County 40.712728  -74.00602      US    New York
2        New York 55.025300   -1.48695      GB     England
3        New York 39.685287  -93.92688      US    Missouri
4        New York  7.963112  -11.76369      SL Bo District
5     Lake Oswego 45.420675 -122.67065      US      Oregon

[[2]]
         name latitude longitude country      state
1     Seattle 47.60383 -122.3301      US Washington
2     Seattle 20.71997 -103.3763      MX    Jalisco
3 Laurelhurst 45.52651 -122.6245      US     Oregon

[[3]]
         name   latitude  longitude country         state
1 Los Angeles  34.053691 -118.24277      US    California
2 Los Angeles   9.390064  -75.23892      CO         Sucre
3 Los Ángeles -37.470745  -72.35169      CL Biobío Region
4 Los Angeles   8.524167  -82.19419      PA      Chiriquí
5 Los Angeles   4.800889  -75.69150      CO     Risaralda

[[4]]
         name   latitude longitude country        state
1 New Orleans  29.975998 -90.07821      US    Louisiana
2 New Orleans  13.104117 -59.61902      BB         <NA>
3 New Orleans -33.715531  18.98695      ZA Western Cape
4 New Orleans   9.638393 -74.61233      CO    Magdalena
5 New Orleans  13.665107 -85.39754      NI     Jinotega

Best match per city name:

fetch_geo <- function(city_string) {
  request("https://geocoding-by-api-ninjas.p.rapidapi.com/") %>%
    req_url_path("v1/geocoding") %>%
    req_url_query(city = city_string, .multi = "explode") %>%
    req_headers('X-RapidAPI-Key'='YOU-API-KEY',
                'X-RapidAPI-Host'='geocoding-by-api-ninjas.p.rapidapi.com') %>% 
    req_perform() %>% 
    resp_body_json(simplifyVector = TRUE) %>% 
    slice_head(n = 1)
}

map_dfr(cities, fetch_geo)

             name latitude  longitude country      state
1 New York County 40.71273  -74.00602      US   New York
2         Seattle 47.60383 -122.33006      US Washington
3     Los Angeles 34.05369 -118.24277      US California
4     New Orleans 29.97600  -90.07821      US  Louisiana