【问题标题】:Overlapping shp maps in sf在 sf 中重叠 shp 地图
【发布时间】:2020-01-07 08:14:10
【问题描述】:

我有两个 shapefile,我用 sf 读入 R。

  • 第一个 shp 文件涵盖区域
  • 第二个 shp 文件涵盖行政区
  • 选区嵌套在区域中。

我想叠加两张地图,然后用相同颜色的阴影为每个选区着色,每个区域使用一种颜色。

我可以绘制两者并使用颜色,但不能叠加和着色。

可以从意大利国家统计局访问文件: Reg1991_WGS84.shp: http://www.istat.it/storage/cartografia/confini_amministrativi/non_generalizzati/Limiti1991.zip CAMERA_PLURI_2017.shp:https://www.istat.it/storage/COLLEGI_ELETTORALI_2017.zip

library(sf)

italia_regions_1991<- read_sf("Limiti1991/Reg1991/Reg1991_WGS84.shp")  %>% select(geometry)
italia_camera_pluri <- read_sf("COLLEGI_ELETTORALI_2017/CAMERA_PLURI_2017.shp") %>% select(geometry)

【问题讨论】:

    标签: r ggplot2 shapefile sf


    【解决方案1】:

    这会让你开始......

    我使用了leafgl 库,因为您要绘制很多折线/多边形...这执行(相当)快...

    library(sf)
    library(devtools)
    library(leaflet)
    #install leaflet with gl-suport
    devtools::install_github("r-spatial/leafgl")
    library(leafgl)
    library(colourvalues)
    
    
    #read shapefile regions and cast to polygons
    sf1 <- st_read( "e:/two_shapes/Limiti1991/Reg1991/Reg1991_WGS84.shp" ) %>% st_cast( "POLYGON", warn = FALSE )
    #read shapefile and cast to POLYGON and then to LINESTRING
    sf2 <- st_read( "e:/two_shapes/COLLEGI_ELETTORALI_2017/COLLEGI_ELETTORALI_2017.shp") %>%
      st_cast( "POLYGON", warn = FALSE ) %>%
      st_cast( "LINESTRING", warn = FALSE )
    
    #creaae color matrix for the regions( depending om DEN_REG), and for the polylines (=black)
    col_region <- colour_values_rgb(sf1$DEN_REG, include_alpha = FALSE) / 255
    col_lines  <- matrix(data = c(0,0,0), nrow = 1 )
    
    #plot leaflet (takes some time)
    leaflet() %>% addTiles() %>%
      addGlPolygons(data = sf1, color = col_region) %>%
      addGlPolylines( data = sf2, color = col_lines)
    

    结果

    【讨论】:

      【解决方案2】:

      考虑通过sf::st_intersection 与地区和地区相交 - 但请注意,似乎存在一些重叠,因为地区和地区并不完美对齐(它们大部分都对齐,但不完全......)

      我还将 CRS 转换为 WGS84;也许没有必要,但与传单等一起使用效果更好......

      library(sf)
      library(dplyr)
      library(ggplot2)
      
      italia_regions_1991<- read_sf("Reg1991_WGS84.shp") %>% 
        select(region = DEN_REG) %>% # this, and geometry by default
        st_transform(4326)
      
      italia_camera_pluri <- read_sf("CAMERA_PLURI_2017.shp") %>% 
        select(geometry) %>% # only geometry...
        st_transform(4326) 
      
      result <- italia_camera_pluri %>% 
        st_intersection(italia_regions_1991) 
      
      ggplot(data = result, aes(fill = region)) +
        geom_sf()
      

      【讨论】:

      • 这非常接近我的预期。但是,我收到以下错误消息Error: 'st_normalize' is not an exported object from 'namespace:sf'。我想弄清楚这是否是由于包冲突。
      • 我设法使用plot(result) 进行绘图,但这让我对绘图的控制较少
      • @MCS 看来您的 sf 包已过时 - 尝试更新它。 st_normalize 是 sf 的一部分,因为 v0.7-3 = 2019 年 2 月左右......
      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 2020-07-28
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多