有人可以解释一下R中的addGeoJSON()功能如何工作,我无法理解该文档。
?addGeoJSON =>( map ,geojson,layerId = NULL)
什么是geojson和layerId?
我能够使用GDAL导入我的GeoJSON:
a1
如何使用传单addGeoJSON()访问列以绘制x,y?
谢谢
最佳答案
addGeoJSON
的第一个参数是传单对象,它是由对leaflet()
的调用创建的。例如。,
url <- "https://raw.githubusercontent.com/glynnbird/usstatesgeojson/master/california.geojson"
geojson <- jsonlite::fromJSON(url)
library("leaflet")
leaflet() %>%
addTiles() %>%
setView(lng = -98.583, lat = 39.833, zoom = 3) %>%
addGeoJSON(geojson)
您可以将通过
readOGR
读入的geojson替换为我创建的geojson
对象用
readOGR()
library("leaflet")
library("rgdal")
url <- "https://raw.githubusercontent.com/glynnbird/usstatesgeojson/master/california.geojson"
res <- readOGR(dsn = url, layer = "OGRGeoJSON")
leaflet() %>%
addTiles() %>%
setView(lng = -98.583, lat = 39.833, zoom = 3) %>%
addPolygons(data = res)
您应该将
addPolygons(data = res)
替换为addPolygons(data = res, lng = "feature.properties.long", lat = "feature.properties.lat")
应该适用于您上面的示例。两者都可能都返回一个
SpatialPolygonsDataFrame
类,您需要将其传递给data
或leaflet()
中的addPolygons()
参数。好吧,如果您要从磁盘上读取带有点的geojson文件,则例如
geojson <- '{
"type": "FeatureCollection",
"features" :
[
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -123, 49 ]
},
"properties": {
"a_property": "foo",
"some_object": {
"a_property": 1,
"another_property": 2
}
}
}
]
}'
writeLines(geojson, "file.geojson")
res <- readOGR(dsn = "file.geojson", layer = "OGRGeoJSON")
leaflet() %>%
addTiles() %>%
setView(lng = -123, lat = 49, zoom = 6) %>%
addMarkers(data = res)
关于r - 如何在R中为Leaflet使用addGeoJSON()功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31798360/