R语言 如何创建一个热图,遵循Map上的路线使用传单?

gt0wga4j  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(112)

我有一个geojson文件,它是路线的形状。在csv文件中,我有utm_lat,utm_lng,pasengers的数据。使用R中的传单,我如何才能创建一个只遵循路线的热图?我需要其他类型的数据文件吗?
我得到类似的东西:enter link description here
但我希望它看起来像这样:enter link description here
我在R shiny app中的一部分代码:heatway1被过滤,其中方向被过滤为way1,heatway2被过滤为way2

heatway1 <- data.frame(heatway1$utm_lng, heatway1$utm_lat, heatway1$passengers)
  colnames(heatway1) <- c("lng", "lat", "value")
  heatway2 <- data.frame(heatway2$utm_lng, heatway2$utm_lat, heatway2$passengers)
  colnames(heatway2) <- c("lng", "lat", "value")

map <- leaflet(filtered_data()) %>%
      addTiles() %>%
      setView(lng = mean(filtered_data()$utm_lng), 
              lat = mean(filtered_data()$utm_lat), 
              zoom = 13)
    
    map <- map %>%
      addHeatmap(data = heatway1, intensity = ~value, blur = 20, max = 20, radius = 25, group = "way1")
    
    map <- map %>%
      addHeatmap(data = heatway2, intensity = ~value, blur = 20, max = 20, radius = 25, group = "way2")
    
    map <- map %>%
      addLegend(
        position = "bottomleft",
        pal = pal,
        values = ~passengers,
        title = "Passengers",
        opacity = 1
      )

    map <- map %>% addLayersControl(overlayGroups = c("way1", "way2"), options = layersControlOptions(collapsed = FALSE)) %>%
      addGeodesicPolylines(data = geojsonroutedata, color = "#03F", opacity = 0.5) %>%
      addResetMapButton() %>% addSearchOSM() %>% addScaleBar(position = "bottomleft") # %>% addFullscreenControl()
ie3xauqp

ie3xauqp1#


我得到类似的东西:your link 1但我希望它看起来像这样:your link 2
和你对数据的描述,在制作热图之前捕捉对应于路线的点可能是有用的。你可以用maptools将点捕捉到最近的线:https://maptools.r-forge.r-project.org/reference/snapPointsToLines.html

编辑:

你将不得不通过考虑两种“方式”来改进,但我所描绘的逻辑如下

library(sf)
library(spData)
library(leaflet)

#### converting polygon dataset to lines for tiny example
nz_as_lines <- nz %>%
  st_cast(x = ., to = "MULTILINESTRING")

# plotting
plot(nz_as_lines$geom)
plot(nz_height$geometry, add=TRUE, col = 'blue')

#### snapping operation
nz_height_relocated <- maptools::snapPointsToLines(points = as(nz_height, Class = "Spatial"), 
                            lines = as(nz_as_lines, Class = "Spatial"), maxDist = 20000) %>%
  st_as_sf()

# difference:
plot(nz_height_relocated$geometry, add=TRUE, col = 'red')

# converting points as WGS84 for leaflet
nz_height_relocated <- nz_height_relocated %>%
  st_transform(x = ., crs = 4326)

map <- leaflet(nz_height_relocated) %>%
  addTiles()

map <- map %>%
  leaflet.extras::addHeatmap(data = nz_height_relocated, intensity = ~snap_dist, blur = 20, max = 20, radius = 25, group = "way1")

map

如果你放大中心,几个初始点与区域的边界很好地对齐。我希望这对你有帮助!

相关问题