如何从shapefile传单R访问多边形信息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从shapefile传单R访问多边形信息相关的知识,希望对你有一定的参考价值。
我用https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london和闪亮的R和传单制作了伦敦地图。我在shapefile中添加了一个属性,现在希望能够在用户单击特定多边形时突出显示shapefile并打印信息。
我看了shiny leaflet ploygon click event,Marker mouse click event in R leaflet for shiny和How to implement input$map_marker_click correctly?,知道我需要使用ObserveEvent
,但是无法正确实现它。
我的代码是:
library(shiny)
library("rgdal")
library(leaflet)
shapeData <- readOGR('statistical-gis-boundaries-london/ESRI/LSOA_2004_London_Low_Resolution.shp')
shapeData <- spTransform(shapeData, CRS("+proj=longlat +ellps=GRS80"))
shapeData$col=sample(c('red','yellow','green'),nrow(shapeData),1) #add some value you want to map
borough=read.csv('BoroughCentres.csv')
ui=fluidPage(
fluidPage(
leafletOutput('LSOAMap'),
p(),
selectInput('LANAME','Borough',
choices = unique(shapeData$LA_NAME))
)
)
server=function(input, output) {
output$LSOAMap <- renderLeaflet({
llong=borough[borough$Borough==input$LANAME,3]
llat=borough[borough$Borough==input$LANAME,4]
bor=subset(shapeData,shapeData$LA_NAME %in% input$LANAME)
leaflet() %>% addTiles() %>%
setView(lng = llong, lat=llat,zoom=13) %>%
addPolygons(data=bor,weight=2,col = 'black',fillOpacity = 0.2,fillColor = bor$col,
highlightOptions = highlightOptions(color='white',weight=1,
bringToFront = TRUE)) %>%
addMarkers(lng = llong,lat=llat,popup=input$LANAME)
})
}
shinyApp(ui, server)
我尝试添加,以及session
作为参数:
observe({
click <- input$map_marker_click
if (is.null(click))
return()
print(click)
text <-
paste("Lattitude ",
click$lat,
"Longtitude ",
click$lng)
leafletProxy(mapId = "LSOAMap") %>%
clearPopups() %>%
addPopups(dat = click, lat = ~lat, lng = ~lng, popup = text)
# map$clearPopups()
# map$showPopup(click$latitude, click$longtitude, text)
})
无济于事。
我想要的是当用户突出显示特定形状时,弹出文本并从shapefile中显示相应的STWARDNAME
。
自治市镇的前几行是:
> head(borough)
Borough LA_CODE long lat
1 City of London E09000001 -0.09194991 51.51814
2 Barking and Dagenham E09000002 0.13064556 51.54764
3 Barnet E09000003 -0.20416711 51.61086
4 Bexley E09000004 0.13459320 51.45981
5 Brent E09000005 -0.26187070 51.55697
6 Bromley E09000006 0.03734663 51.38836
你非常接近,但有一些语法问题,我认为这些问题正在绊倒你。
首先,你没有从你的SPDF中正确选择你想要的STWARDNAME
,所以R无法知道要显示哪一个。我添加了这一行
popup = subset(shapeData, LA_NAME == input$LANAME )$STWARDNAME
你也可以通过highlightOptions
作为选项,它应该只是highlight
最后,因为您对多边形的鼠标悬停更感兴趣,我删除了弹出标记并更改为highlight
,以便在鼠标悬停多边形时显示名称:
leaflet() %>% addTiles() %>%
setView(lng = llong, lat=llat,zoom=13) %>%
addPolygons(data=bor,weight=2,col = 'black',fillOpacity = 0.2,fillColor = bor$col,
highlight = highlightOptions(color='white',weight=1,
bringToFront = TRUE), label= popup)
@Stedy回答很棒,而且更好,我只是采取了不同的方式。我添加了一个图层id矢量,并弹出一个决定来查找STWARDNAME
lid=as.vector(row.names(bor@data))
pu=bor@data[row.names(bor@data)==lid,'STWARDNAME']
,然后在制作地图layerId = lid,popup = pu,
时加入两个参数。当用户点击某个区域时弹出窗口显示
bor=subset(shapeData,shapeData$LA_NAME %in% input$LANAME)
lid=as.vector(row.names(bor@data))
pu=bor@data[row.names(bor@data)==lid,'STWARDNAME']
leaflet() %>% addTiles() %>%
setView(lng = llong, lat=llat,zoom=13) %>%
addPolygons(data=bor,weight=2,col = 'black',fillOpacity = 0.2,fillColor = bor$col,
layerId = lid,popup = pu,
highlightOptions = highlightOptions(color='white',weight=1,
bringToFront = TRUE)) %>%
addMarkers(lng = llong,lat=llat,popup=input$LANAME)
以上是关于如何从shapefile传单R访问多边形信息的主要内容,如果未能解决你的问题,请参考以下文章
R:将Shapefile 1中的多边形与shapefile 2中的区域代码匹配