R rMaps geoJson函数没有绘图

时间:2015-02-12 19:06:24

标签: r leaflet geojson

我有来自ESRI服务的JSON路由。我尝试使用rMaps使用geoJson绘制路线。我得到的唯一输出是地图,但不是多边形的绘图。

这是代码:

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

任何人都知道为什么这个JSON没有显示在地图中?

谢谢!

1 个答案:

答案 0 :(得分:1)

这不是有效的GeoJSON对象。 L.GeoJSON(Leaflet的GeoJSON图层)仅接受有效的GeoJSON featurecollection对象或具有有效GeoJSON特征对象的数组。这是一个有效的GeoJSON featurecollection对象:

{
    "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]
            ]
        }
    }]
}

您可以在此处了解有关GeoJSON的更多信息:http://geojson.org/您可以在此处验证GeoJSON对象:http://geojsonlint.com/为了更好地衡量,这里也是Leaflet的L.GeoJSON图层的参考:{{ 3}}以及Leaflet关于如何使用L.GeoJSON图层的教程:http://leafletjs.com/reference.html#geojson