aruizga
aruizga

Reputation: 644

R rMaps geoJson function not plotting

I have a JSON route coming from ESRI service. I try to plot the route using geoJson using rMaps. The only output I get is the map but not the plot from the polygon.

This is the code:

require(rMaps)
json = "{\"messages\":[],\"routes\":{\"fieldAliases\":{\"ObjectID\":\"ObjectID\",\"Name\":\"Name\",\"FirstStopID\":\"FirstStopID\",\"LastStopID\":\"LastStopID\",\"StopCount\":\"StopCount\",\"Total_TravelTime\":\"Total_TravelTime\",\"Total_Kilometers\":\"Total_Kilometers\",\"Total_Miles\":\"Total_Miles\",\"Shape_Length\":\"Shape_Length\"},\"geometryType\":\"esriGeometryPolyline\",\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326},\"features\":[{\"attributes\":{\"ObjectID\":1,\"Name\":\"Location 1 - Location 2\",\"FirstStopID\":1,\"LastStopID\":2,\"StopCount\":2,\"Total_TravelTime\":2.8403004080455228,\"Total_Kilometers\":0.79761507718052971,\"Total_Miles\":0.49561503145413888,\"Shape_Length\":0.0082585488982182334},\"geometry\":{\"paths\":[[[-122.40774845199996,37.783745569000075],[-122.40745999999996,37.783510000000035],[-122.40722999999997,37.783340000000067],[-122.40718999999996,37.783310000000029],[-122.40648999999996,37.782740000000047],[-122.40591999999998,37.782300000000077],[-122.40520999999995,37.782870000000059],[-122.40442999999999,37.78350000000006],[-122.40432999999996,37.783580000000029],[-122.40372999999994,37.784070000000042],[-122.40329999999994,37.78374000000008],[-122.40269999999998,37.783270000000073],[-122.40411865599998,37.782150343000069]]]}}]},\"directions\":[{\"routeId\":1,\"routeName\":\"Location 1 - Location 2\",\"summary\":{\"totalLength\":0.49561503145413888,\"totalTime\":2.8403004049323499,\"totalDriveTime\":2.8403004080455228,\"envelope\":{\"xmin\":-122.40789999999998,\"ymin\":37.782000000000039,\"xmax\":-122.40269999999998,\"ymax\":37.784070000000042,\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326}}},\"features\":[{\"attributes\":{\"length\":0,\"time\":0,\"text\":\"Start at Location 1\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTDepart\"},\"compressedGeometry\":\"+1m91-6fki2+202vh+0+0\"},{\"attributes\":{\"length\":0.13919863031093585,\"time\":0.8808930174425843,\"text\":\"Go southeast on 5th St toward Stevenson St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTStraight\"},\"compressedGeometry\":\"+1m91-6fki2+202vh+36-2g\"},{\"attributes\":{\"length\":0.16963433548079213,\"time\":0.96497624999999998,\"text\":\"Turn left on Minna St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTTurnLeft\"},\"compressedGeometry\":\"+1m91-6fkes+202t1+3q+33\"},{\"attributes\":{\"length\":0.07799196861297697,\"time\":0.51913014739269459,\"text\":\"Turn right on 4th St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTTurnRight\"},\"compressedGeometry\":\"+1m91-6fkb2+20304+1p-1d\"},{\"attributes\":{\"length\":0.10879009704943393,\"time\":0.47530099321024388,\"text\":\"Turn right on Howard St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTTurnRight\"},\"compressedGeometry\":\"+1m91-6fk99+202un-2f-1u\"},{\"attributes\":{\"length\":0,\"time\":0,\"text\":\"Finish at Location 2, on the left\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTStop\"},\"compressedGeometry\":\"+1m91-6fkbo+202sp+0+0\"}]}]}"
regions=RJSONIO::fromJSON(json)
lmap <- Leaflet$new()
lmap$tileLayer(provide='Stamen.TonerLite')
lmap$setView(c(37.78356, -122.4079), 13)
lmap$geoJson(
  regions)
legend_vec = c('red'='high', 'blue'='medium', 'green'='low')
lmap$legend(position = 'bottomright', 
            colors   =  names(legend_vec), 
            labels   =  as.vector(legend_vec))
lmap

Anyone knows why this JSON is not displayed in the map?

Thanks!

Upvotes: 1

Views: 221

Answers (1)

iH8
iH8

Reputation: 28688

That's not a valid GeoJSON object. L.GeoJSON (Leaflet's GeoJSON layer) only accepts a valid GeoJSON featurecollection object, or an array with valid GeoJSON feature objects. This is a valid GeoJSON featurecollection object:

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "myProperty": "myValue"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [0,0]
        }
    },{
        "type": "Feature",
        "properties": {
            "myProperty": "myValue"
        },
        "geometry": {
            "type": "LineString",
            "coordinates": [
                [-45, -45],
                [45, 45]
            ]
        }
    }]
}

You can learn more about GeoJSON here: http://geojson.org/ and you can validate GeoJSON objects here: http://geojsonlint.com/ For good measure here is also the reference for Leaflet's L.GeoJSON layer: http://leafletjs.com/reference.html#geojson and a tutorial from Leaflet on how to use L.GeoJSON layers: http://leafletjs.com/examples/geojson.html

Upvotes: 1

Related Questions