请问有人能解释一下R语言中的addGeoJSON()功能是如何工作的吗?我看不懂文档。
?addGeoJSON => (map, geojson, layerId = NULL)
geojson和layerId是什么意思?
我已经通过GDAL导入了我的GeoJSON文件:
a1 <- readOGR(dsn = "myData.geojson", layer = "OGRGeoJSON")我该如何使用leaflet的addGeoJSON()函数访问列以绘制x,y坐标?
谢谢
addGeoJSON
的第一个参数是从调用leaflet()
创建的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)
你可以用我创建的geojson
对象替换通过readOGR
读入的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, lng = "feature.properties.long", lat = "feature.properties.lat")
替换addPolygons(data = res)
。
对于你上面的例子应该有效。两者都可能返回一个SpatialPolygonsDataFrame
类,你需要将其传递给leaflet()
或addPolygons()
中的data
参数。
如果你从磁盘读取带有点的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)
readOGR()
,稍等一下,刷新页面。 - sckottaddGeoJSON()
函数 - 只有当你有geojson数据需要传递时才需要使用。readOGR
函数应该将你读取的geojson转换为空间类对象。当你执行class(a1)
时,你会得到什么? - sckott