传单(R)添加弹出窗口:坐标/属性
一般来说,我对处理空间数据和传单相当陌生。我很难为我的地图创建弹出窗口。基本上,我希望在弹出窗口中显示多边形的坐标和类类型的属性之一。下面是我的geoJSON文件的测试示例:传单(R)添加弹出窗口:坐标/属性,r,leaflet,geospatial,geojson,R,Leaflet,Geospatial,Geojson,一般来说,我对处理空间数据和传单相当陌生。我很难为我的地图创建弹出窗口。基本上,我希望在弹出窗口中显示多边形的坐标和类类型的属性之一。下面是我的geoJSON文件的测试示例: { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {"class": "blah"}, "geometry": { "type": "Polygo
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"class": "blah"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-81.7987060546875,
32.74570253945518
],
[
-81.6229248046875,
32.16631295696736
],
[
-80.958251953125,
32.4263401615464
],
[
-81.2713623046875,
32.791892438123696
],
[
-81.7437744140625,
32.97180377635759
],
[
-81.7987060546875,
32.74570253945518
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blah2"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-82.056884765625,
33.55512901742288
],
[
-81.4471435546875,
33.247875947924385
],
[
-81.40869140625,
33.80653802509606
],
[
-82.078857421875,
33.88865750124075
],
[
-82.40295410156249,
33.58716733904656
],
[
-82.056884765625,
33.55512901742288
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blahh3"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-83.485107421875,
32.930318199070534
],
[
-83.07861328125,
31.863562548378965
],
[
-82.21618652343749,
32.11049589629439
],
[
-82.97973632812499,
33.22030778968541
],
[
-83.726806640625,
33.211116472416855
],
[
-83.485107421875,
32.930318199070534
]
]
]
}
}
]
}
以下是我迄今为止创建地图的代码,但我正在努力/甚至不知道从何处开始创建包含坐标和属性的弹出窗口:
blahTest <- geojson_read("/file/path/...", what = "sp")
fpal <- colorFactor("viridis", blahTest$class)
leaflet(blahTest) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
color = ~fpal(class)) %>%
addLegend(colors = ~fpal, opacity = 1.0, labels = ~fpal) %>%
addPopups()
提前谢谢 概述
根据@patL的建议,我在函数中使用了popup参数来添加标签,包括定义blahTest中每个多边形的类和坐标
我将您的示例数据保存为.gejson文件,并使用导入,读取后使用包在弹出窗口中生成HTML表
请尝试在addPolygons中将popup=column\u设置为\u popup。谢谢,我已使用属性class,但没有使用多边形的坐标。您希望显示多边形的什么坐标?所有节点?我想包括所有节点
# load necessary package
library( htmlTable )
library( leaflet )
library( sf )
# load necessary data
blahTest <-
read_sf(
dsn = "test.geojson"
, layer = "OGRGeoJSON"
)
# map data values to colors
fpal <- colorFactor( palette = "viridis", domain = blahTest$class)
# create map
my.map <-
leaflet( data = blahTest ) %>%
addTiles() %>%
addPolygons( stroke = FALSE
, smoothFactor = 0.3
, fillOpacity = 1
, color = ~fpal( class )
, popup = paste0(
"<b>Class: </b>"
, blahTest$class
, "<br>"
, "<b>Coordinates: </b>"
, "<br>"
, lapply(
X = blahTest$geometry
, FUN = function( i )
htmlTable(
x = i[[1]]
, header = c( "Longitude", "Latitude" )
)
)
) ) %>%
addLegend( pal = fpal
, values = ~class
, labels = ~class
, title = "Legend"
, position = "topright"
, opacity = 1.0 )
# view map
my.map
# end of script #