使用R计数多边形中的点数


11

我有两个共享相同CRS的课程(纬度和经度):

  1. bolognaQuartieriMapSpatialPolygonDataFrame包含城市行政区的数据。
  2. crashPointsSpatialPointsDataFrame包含事故数据。

使用以下命令可以很好地绘制它们:

plot(bolognaQuartieriMap)
title("Crash per quartiere")
plot(crashPoints, col="red",add=TRUE)

我需要的是获取crashPoints构成多边形的每个多边形中的点数bolognaQuartieriMap。建议使用,over()但没有成功。

Answers:


21

由于您既没有提供可复制的示例,也没有错误消息,请查看以下代码片段是否可以帮助您入门:

library("raster")
library("sp")

x <- getData('GADM', country='ITA', level=1)
class(x)
# [1] "SpatialPolygonsDataFrame"
# attr(,"package")
# [1] "sp"

set.seed(1)
# sample random points
p <- spsample(x, n=300, type="random")
p <- SpatialPointsDataFrame(p, data.frame(id=1:300))

proj4string(x)
# [1] " +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"
proj4string(p)
# [1] " +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"

plot(x)
plot(p, col="red" , add=TRUE)

情节

res <- over(p, x)
table(res$NAME_1) # count points
#               Abruzzo                Apulia            Basilicata
#                    11                    20                     9
#              Calabria              Campania        Emilia-Romagna
#                    16                     8                    25
# Friuli-Venezia Giulia                 Lazio               Liguria
#                     7                    14                     7
#             Lombardia                Marche                Molise
#                    22                     4                     3
#              Piemonte              Sardegna                Sicily
#                    35                    18                    21
#               Toscana   Trentino-Alto Adige                Umbria
#                    33                    15                     6
#         Valle d'Aosta                Veneto
#                     4                    22

1
我真的非常非常感谢这个答案。请给我支持,一千个感谢。
丹尼·赫恩

2

我想留下另一个选择。您可以poly.counts()GISTools包中使用完成任务。通过rcs使用样本数据,您可以执行以下操作。如果您看一下函数,您会发现该函数写为colSums(gContains(polys, pts, byid = TRUE))。因此,您只需gContains()rgeos软件包和中使用colSums()

library(GISTools)

poly.counts(p, x) -> res
setNames(res, x@data$NAME_1)

要么

colSums(gContains(x, p, byid = TRUE)) -> res
setNames(res, x@data$NAME_1)

结果是:

#              Abruzzo                Apulia            Basilicata 
#                   11                    20                     9 
#             Calabria              Campania        Emilia-Romagna 
#                   16                     8                    25 
#Friuli-Venezia Giulia                 Lazio               Liguria 
#                    7                    14                     7 
#            Lombardia                Marche                Molise 
#                   22                     4                     3 
#             Piemonte              Sardegna                Sicily 
#                   35                    18                    21 
#              Toscana   Trentino-Alto Adige                Umbria 
#                   33                    15                     6 
#        Valle d'Aosta                Veneto 
#                    4                    22 

这确实非常有帮助。但是我在保存结果时遇到了麻烦,因为我想根据多边形中的点数绘制Choropleth
qpisqp

2

您可以使用该sf软件包实现相同的目的。检查下面的可复制和注释的代码。该包sf用于将空间对象作为简单要素对象处理。在此答案中,程序包raster仅用于下载示例多边形数据,而程序包最后dplyr用于数据转换。

# Load libraries ----------------------------------------------------------

library(raster)
library(sf)
library(dplyr)

# Get sample data ---------------------------------------------------------

# Get polygon
polygon <- getData('GADM', country='URY', level = 1)[,1] # Download polygon of country admin level 1 
polygon <- st_as_sf(polygon) # convert to sf object
colnames(polygon) <- c("id_polygons", "geometry") # change colnames
polygon$id_polygons <- paste0("poly_", LETTERS[1:19]) #  change polygon ID

# Get sample random poins from polygon bbox
set.seed(4)
bbox <- st_as_sfc(st_bbox(polygon))
points <- st_sample(x = bbox, size = 100, type = "random")
points <- st_as_sf(data.frame(id_points = as.character(1:100)), points) # add points ID

# Plot data ---------------------------------------------------------------

# Plot polygon + points
plot(polygon, graticule = st_crs(4326), key.pos = 1)
plot(points, pch = 19, col = "black", add = TRUE)

# Intersection between polygon and points ---------------------------------

intersection <- st_intersection(x = polygon, y = points)

# Plot intersection
plot(polygon, graticule = st_crs(4326), key.pos = 1)
plot(intersection[1], col = "black", pch = 19, add = TRUE)

# View result
table(intersection$id_polygons) # using table

# using dplyr
int_result <- intersection %>% 
  group_by(id_polygons) %>% 
  count()

as.data.frame(int_result)[,-3]

交叉结果

相交图

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.