我有一个多边形(zones
)和一组坐标(points
)。我想为整个多边形创建一个空间内核密度栅格,并按区域提取密度总和。多边形外的点应丢弃。
library(raster)
library(tidyverse)
library(sf)
library(spatstat)
library(maptools)
load(url("https://www.dropbox.com/s/iv1s5butsx2v01r/example.RData?dl=1"))
# alternatively, links to gists for each object
# https://gist.github.com/ericpgreen/d80665d22dfa1c05607e75b8d2163b84
# https://gist.github.com/ericpgreen/7f4d3cee3eb5efed5486f7f713306e96
ggplot() +
geom_sf(data = zones) +
geom_sf(data = points) +
theme_minimal()
我尝试使用{ppp
转换为spatstat
,然后使用density()
,但是结果中的单位让我感到困惑。我相信问题与地图的单位有关,但我不确定如何进行。
更新
以下是用于重现我创建的密度图的代码:
zones_owin <- as.owin(as_Spatial(zones))
pts <- st_coordinates(points)
p <- ppp(pts[,1], pts[,2], window=zones_owin, unitname=c("metre","metres"))
ds <- density(p)
r <- raster(ds)
plot(r)
答案 0 :(得分:1)
我不确定这是否能回答您的所有问题,但应该是一个好的开始。如果您需要其他类型的输出,请在评论或问题中进行澄清。
它将删除不在“区域”多边形之一内的所有点,按区域对它们进行计数,并绘制由落入该点内的点数着色的区域。
library(raster)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
library(spatstat)
library(maptools)
#> Checking rgeos availability: TRUE
load(url("https://www.dropbox.com/s/iv1s5butsx2v01r/example.RData?dl=1"))
# alternatively, links to gists for each object
# https://gist.github.com/ericpgreen/d80665d22dfa1c05607e75b8d2163b84
# https://gist.github.com/ericpgreen/7f4d3cee3eb5efed5486f7f713306e96
p1 <- ggplot() +
geom_sf(data = zones) +
geom_sf(data = points) +
theme_minimal()
#Remove points outside of zones
points_inside <- st_intersection(points, zones)
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
nrow(points)
#> [1] 308
nrow(points_inside)
#> [1] 201
p2 <- ggplot() +
geom_sf(data = zones) +
geom_sf(data = points_inside)
points_per_zone <- st_join(zones, points_inside) %>%
count(LocationID.x)
#> although coordinates are longitude/latitude, st_intersects assumes that they are planar
p3 <- ggplot() +
geom_sf(data = points_per_zone,
aes(fill = n)) +
scale_fill_viridis_c(option = 'C')
points_per_zone
#> Simple feature collection with 4 features and 2 fields
#> geometry type: POLYGON
#> dimension: XY
#> bbox: xmin: 34.0401 ymin: -1.076718 xmax: 34.17818 ymax: -0.9755066
#> epsg (SRID): 4326
#> proj4string: +proj=longlat +ellps=WGS84 +no_defs
#> # A tibble: 4 x 3
#> LocationID.x n geometry
#> * <dbl> <int> <POLYGON [°]>
#> 1 10 129 ((34.08018 -0.9755066, 34.0803 -0.9757393, 34.08046 -0.975…
#> 2 20 19 ((34.05622 -0.9959458, 34.05642 -0.9960835, 34.05665 -0.99…
#> 3 30 29 ((34.12994 -1.026372, 34.12994 -1.026512, 34.12988 -1.0266…
#> 4 40 24 ((34.11962 -1.001829, 34.11956 -1.002018, 34.11966 -1.0020…
cowplot::plot_grid(p1, p2, p3, nrow = 2, ncol = 2)
看来我低估了您的问题的难度。您在寻找下面的图(及底层数据)吗?
它使用的栅格为〜50x50,栅格:: focal的窗口为9x9,均值用于插值数据。
答案 1 :(得分:1)
直接使用地理坐标(lon,lat)时,单位很难。如果可能,应转换为平面坐标(这是spatstat
的要求),然后从那里开始。平面坐标通常以米为单位,但是我想这取决于特定的投影和下面的椭球等。您可以看到this answer,了解如何使用sf
投影到平面坐标并导出到{使用spatstat
的{1}}格式。 注意:您必须手动选择一个明智的投影(可以使用http://epsg.io找到一个投影),并且必须同时投影多边形和点。
一旦所有内容均为maptools
格式,则可以使用spatstat
进行内核平滑。所得的栅格值(类别density.ppp
的对象)是点的强度,即每平方单位(例如平方米)的点数。如果要在某个区域上进行汇总,可以使用im
获取具有给定强度的点过程模型在该区域中的预期点数。