将空间数据框与不同列组合

时间:2019-09-03 22:32:24

标签: r leaflet geospatial raster sp

我要尝试将三个不同的空间数据帧合并为一个大数据帧。可重现的代码如下:

library(sf)
library(sp)
library(leaflet)
library(tigris)
library(rgdal)
library(raster)
library(dplyr)


# Dataset 1 ---------------------------------------------------------------

set1 <- voting_districts("nevada") 
set1 <- subset(set1,set1$COUNTYFP10 != '003')
set1 <- subset(set1,set1$COUNTYFP10 != '031')

# Dataset 2 ---------------------------------------------------------------

#Dataset Download Link:
#https://opendata.arcgis.com/datasets/3e738fdaca2b4aeba0c18a2478cae956_0.zip?outSR=%7B%22wkid%22%3A102707%2C%22latestWkid%22%3A3421%7D

dsn <- "./Precincts"
set2 = readOGR(dsn)


# Dataset 3 ---------------------------------------------------------------

#Dataset Download Link:
#https://opendata.arcgis.com/datasets/fcab7fcd0b2e4243b4f6d7292c2e4daa_7.zip?outSR=%7B%22latestWkid%22%3A3423%2C%22wkid%22%3A102709%7D

dsn <- "./Voter_Districts"
set3 = readOGR(dsn)


# Transform ---------------------------------------------------------------

set1 <- spTransform(set1, CRS("+init=epsg:4326"))
set2 <- spTransform(set2, CRS("+init=epsg:4326"))
set3 <- spTransform(set3, CRS("+init=epsg:4326"))


# Plot --------------------------------------------------------------------

leaflet() %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons( fillColor = "red",
               color = "black",
               weight = 1.0, data = set1) %>%
  addPolygons( fillColor = "blue",
               color = "black",
               weight = 1.0, data = set2) %>%
  addPolygons(fillColor = "green",
              color = "black",
              weight = 1.0, data = set3)

我想做的就是将它们全部组合成一个大的SpatialPolygonsDataFrame,这将使我能够执行与上述相同的绘图,但是不需要两个附加的addPolygons函数。像这样:

leaflet(combined_sets) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(...)

那有可能吗?鉴于它们是具有不同列的三个不同数据集,因此我不确定是否可以做到。

1 个答案:

答案 0 :(得分:0)

如评论中所述,有解决方案。 bindraster::union都为我工作

ab <- bind(set2,set3)
ab <- raster::union(set2,set3)

leaflet() %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons( fillColor = "blue",
               color = "black",
               weight = 1.0, data = ab)

enter image description here