如何从shapefile访问多边形信息

问题描述 投票:0回答:2

我使用 https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london 以及闪亮的 R 和传单制作了伦敦地图。我向 shapefile 添加了一个属性,现在希望能够在用户单击特定多边形时突出显示 shapefile 并打印信息。

我查看了shiny leaflet ploygon click事件R leaflet中的Marker mouse click事件用于shiny,以及如何正确实现input$map_marker_click?,并且知道我需要使用

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)
  })

无济于事。

我想要的是,当用户突出显示特定形状时,会弹出文本并显示形状文件中相应的

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
r shiny r-leaflet
2个回答
2
投票

你已经很接近了,但是有一些语法问题,我认为这些问题让你绊倒了。

首先,您没有正确从 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)

其产量: enter image description here


0
投票

@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) 
© www.soinside.com 2019 - 2024. All rights reserved.