如何从shapefile传单R访问多边形信息 [英] How to access polygon information from shapefile leaflet R

查看:77
本文介绍了如何从shapefile传单R访问多边形信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a map of London using https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london and shiny and R and leaflet. I added an attribute to the shapefile, and now want to be able to highlight the shapefile and print information when the user clicks on a specific polygon.

我查看了发光的传单ploygon点击事件如何正确实施input $ map_marker_click?,并且知道我需要使用ObserveEvent,但无法正确实现它.

I looked at shiny leaflet ploygon click event, Marker mouse click event in R leaflet for shiny, and How to implement input$map_marker_click correctly?, and know I need to use ObserveEvent, but have not been able to implement it correctly.

我的代码是:

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.

What I want is that when a user highlights a specific shape, text pops up and shows the corresponding STWARDNAME from the shapefile.

自治市镇的前几行是:

> 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

推荐答案

您非常接近,但是有一些语法问题,我认为这使您感到困惑.

You are pretty close but have a few syntax issues which I think are tripping you up.

首先,您没有从SPDF中正确选择所需的STWARDNAME,因此R无法知道要显示哪个.我添加了这一行

First you are not properly selecting what STWARDNAME you want from your SPDF so there is no way for R to know which one to show. I added this line

popup = subset(shapeData, LA_NAME == input$LANAME )$STWARDNAME

还应该将highlightOptions传递给highlight

最后,由于您对多边形的鼠标悬停更加感兴趣,因此我删除了弹出标记,并更改为highlight,以便在将鼠标悬停在多边形上时显示名称:

Finally since you were more interested in mouseover for a polygon, I removed the popup marker and changed to highlight so that the name is shown when you mouseover the polygon:

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)

哪个产量:

这篇关于如何从shapefile传单R访问多边形信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆