1

I am new to API's and R, and I was wondering how to use this GraphHopper API.(https://graphhopper.com/api/1/docs/isochrone/https://graphhopper.com/api/1/docs/isochrone/) At the page above there is this:

curl "https://graphhopper.com/api/1/isochrone?point=51.131108,12.414551&key=[YOUR_KEY]"

Is there a way to make to convert the response to a polygon object?

So far i got here, but I don't know how to convert the request to a Polygon:

library(httr)
library(jsonlite)

a = GET("https://graphhopper.com/api/1/isochrone?point=51.131108,12.414551&key=KEY")

class(a)

a$status_code
Claudio Paladini
  • 1,000
  • 1
  • 10
  • 20
  • You can see [This](https://www.r-bloggers.com/accessing-apis-from-r-and-a-little-r-programming/) – MrSmithGoesToWashington Jan 25 '18 at 09:48
  • I don't think there is anything on APIs. I am looking for a function like: polygon <- parseAsPolygon(GET("http://...")) – Claudio Paladini Jan 25 '18 at 10:00
  • The title is **Accessing APIs from R (and a little R programming)** .. why do you say there is nothing on API ? Anyway, you allready made the request. If I understand well, your matter is to understand the content of you object a. So, may be you can add to your question the result of `dput(a)` .. and someone can help you to explore this object ? – MrSmithGoesToWashington Jan 25 '18 at 10:05
  • Jesus, sorry. I was clicking on your name. Thanks for the help – Claudio Paladini Jan 25 '18 at 10:07

1 Answers1

1

This worked..

library(RJSONIO)
library(sp)
library(leaflet)

## GraphHopper API
# https://graphhopper.com/api/1/docs/isochrone/

#Request
a <-fromJSON("https://graphhopper.com/api/1/isochrone?point=51.131108,12.414551&key=[GET YOUR OWN KEY]")

#Response
x = a$polygons$geometry$coordinates

#Response Manipulation
x = data.frame(unlist(x))
m = nrow(x)/2
x1 = x[1:m,1]
x2 = x[(1+m):nrow(x),1]
x0 = data.frame(cbind(x1,x2))

#Polygon plotting on Leaflet
p = Polygon(coords = x0)

leaflet()%>%
  addTiles()%>%
  addPolygons(data = p)
Claudio Paladini
  • 1,000
  • 1
  • 10
  • 20