简体   繁体   English

ArcGIS Dissolve 等效于 R

[英]ArcGIS Dissolve equivalent in R

I am trying to convert some ArcGIS work into an R-script.我正在尝试将一些 ArcGIS 工作转换为 R 脚本。 I have a shapefile that has 2 variables, one is the FRU(an ID field representing different region names 1 through 60) and a geometry variable (I assume this is showing where these features are located spatially).我有一个包含 2 个变量的 shapefile,一个是 FRU(代表不同区域名称 1 到 60 的 ID 字段)和一个几何变量(我假设这是显示这些特征在空间上的位置)。

What I want is to create a new shapefile that combines the geometry for FRU=o, FRU=1, FRU=2...FRU=60.我想要的是创建一个新的 shapefile,它结合了 FRU=o、FRU=1、FRU=2...FRU=60 的几何形状。

So instead of having 26, 967 rows, I have 61 rows.因此,我有 61 行,而不是 26、967 行。

It is generally good practice to include a reproducible code example with your questions so it is easier for others to help solve the problem.在您的问题中包含一个可重现的代码示例通常是一种很好的做法,这样其他人就可以更轻松地帮助解决问题。 You can pass your data into the dput function in R to get a copy and pasteable version of your data.您可以将数据传递到dput中的 dput function 以获得数据的复制和粘贴版本。

For this problem, I would suggest using the sf package:对于这个问题,我建议使用sf package:

# Read example data from sf package
nc <-  sf::st_read(system.file("shape/nc.shp", package="sf"))

# add a dummy column do dissolve with
nc <- dplyr::mutate(nc, example = c(rep('a', 50), rep('b', 50)))

# group the data by the new column
nc <- dplyr::group_by(nc, example)

# summarize the data
nc <- dplyr::summarise(nc)

Using your example, you would instead say my_data <- dplyr::group_by(my_data, FRU) and then use the same summarise function dplyr::summarise(my_data) .使用您的示例,您会说my_data <- dplyr::group_by(my_data, FRU)然后使用相同的 summarize function dplyr::summarise(my_data)

In my opinion, the sf package is the premiere tool for working with spatial data in R. However, there is no dedicated sf::st_dissolve() .在我看来, sf package 是 R 中处理空间数据的首要工具。但是,没有专用的sf::st_dissolve()

Instead you can simply define a grouping variable in your data and use dplyr::group_by and dplyr::summarize .相反,您可以简单地在数据中定义一个分组变量并使用dplyr::group_bydplyr::summarize The example is simplified version of examples here ( https://github.com/r-spatial/sf/issues/290 ).该示例是此处示例的简化版本 ( https://github.com/r-spatial/sf/issues/290 )。

library(sf)
library(ggplot2)

# Example data
nc <- st_read(system.file("shape/nc.shp", package="sf"))

# Dummy grouping variable
nc$group <- factor(rep(1:3, length.out = nrow(nc)))

# Dissolve
nc_dissolve <- nc %>% group_by(group) %>% summarize()

# Plot results
ggplot(data = nc_dissolve) +
  geom_sf(aes(fill = group))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM