rapigeospatialgraphhopper

How do I convert an API response to a Polygon object in R? Graphhopper API in R - Isochrones


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

Solution

  • 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)