如何从Shape文件传单R接入多边形信息(How to access polygon informa

2019-10-30 04:54发布

我提出用伦敦的地图https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london和光泽,R和传单。 我加入到shape文件的属性,现在希望能够当用户点击一个特定的多边形突出shape文件和打印信息。

我看着闪亮的传单ploygon点击事件 , 标记鼠标点击事件中的R单张光泽 ,和如何正确地贯彻执行输入$ 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从shape文件。

市镇的前几行:

> 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

Answer 1:

你是相当接近,但有我认为是你绊倒了几个语法问题。

首先你是不是正确选择什么STWARDNAME您从您的SPDF想的那么有没有办法对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)

这将产生:



Answer 2:

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


文章来源: How to access polygon information from shapefile leaflet R
标签: r shiny leaflet