在 sf 中重叠 shp 地图

Overlapping shp maps in sf

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

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

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

可在此处访问意大利国家统计局的文件: 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)

这会让你入门....

我使用了 leafgl 库,因为你正在绘制很多 polylines/plygons...这执行(相当)快...

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)

结果

考虑通过 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()