将sf多边形对象用作spatstat中的窗口 [英] Use sf polygon object as window in spatstat

查看:90
本文介绍了将sf多边形对象用作spatstat中的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,所有潜在的帮助者,

Hello all potential helpers,

我有一个从tigris包获得的SpatialPolygonDataFrame对象,我想在创建ppp对象时将其用作多边形窗口.这是我尝试过的:

I have a SpatialPolygonDataFrame object obtained from the tigris package and I would like to use it as a polygonal window in the creation of a ppp object. Here is what I tried:

# Needed packages
library(spatstat)
library(sf)

# Download geospatial data for Lee county in Alabama (home of the great Auburn University by the way!)
county <- tigris::county_subdivisions(state = "Alabama", county = "Lee")

# The polygon of Lee county is subdivided, so I convert it to a single polygon after converting it to an sf object
county_sf <- st_as_sf(county)
county_one <- st_union(county_sf)

# A quick plot of the object outputs what I am expecting
plot(county_one)

# Now I create a planar point pattern and I use county_one as the window
p <- ppp(x = -85.4, y = 32.5, window = as.owin((county_one)))

# But the plot here shows that the window is just a rectangle and not the polygon :(
plot(p)

谢谢您的帮助.

推荐答案

注意:我已经编辑了此答案以包含完整的详细信息.

正如@TimSalabim提到的那样,这在sf中正在进行,但是在此之前,您必须 通过旧的sp类,例如SpatialPolygons.使用类似 sf中的as_Spatial,然后加载maptools并使用as.owinSpatial对象上的as(x, "owin").

As @TimSalabim mentions this is under way in sf, but until then you have to go through the old sp classes such as SpatialPolygons. Use something like as_Spatial in sf and then load maptools and use as.owin or as(x, "owin") on the Spatial object.

此外,您只能将平面(投影)空间中的坐标与 spatstat而不是在地球弯曲表面上的坐标.你有 投影到相关的平面坐标系.也许< epsg.io/6345>是 在这种情况下可用.要投影到此坐标系,请使用 sf::st_transform(county_one, crs = 6345).之后,您转换为 Spatial,然后再owin. 注意:选择相关的预测是 科学,而我对此知之甚少,所以如果您愿意,可以做一些研究 以确保结果不会太失真.

Furthermore, you can only use coordinates in planar (projected) space with spatstat and not coordinates on the curved surface of the earth. You have to project to a relevant planar coordinates system. Maybe <epsg.io/6345> is usable in this case. To project to this coordinate system use sf::st_transform(county_one, crs = 6345). Afterwards you convert to Spatial and then owin. Note: Choosing the relevant projection is a science, and I don’t know much about it, so do a bit of research if you want to make sure you don’t get too distorted results.

特别是在原始示例中,您可以执行以下操作:

Specifically with the original example you can do:

# Needed packages
library(spatstat)
#> Loading required package: spatstat.data
#> Loading required package: nlme
#> Loading required package: rpart
#> 
#> spatstat 1.62-2       (nickname: 'Shape-shifting lizard') 
#> For an introduction to spatstat, type 'beginner'
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.2, PROJ 6.2.1
library(maptools)
#> Loading required package: sp
#> Checking rgeos availability: TRUE
library(tigris)
#> To enable 
#> caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
#> 
#> Attaching package: 'tigris'
#> The following object is masked from 'package:graphics':
#> 
#>     plot

county <- county_subdivisions(state = "Alabama", county = "Lee", class = "sf", progress_bar = FALSE)
county_one <- st_union(county)
plot(county_one)

county_flat <- st_transform(county_one, crs = 6345)
plot(county_flat)

county_owin <- as.owin(as_Spatial(county_flat))

该县的100个随机点:

100 random points in the county:

p <- runifpoint(100, win = county_owin)
plot(p)

这篇关于将sf多边形对象用作spatstat中的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆