简体   繁体   English

在 spatstat 中使用 sf 多边形对象作为窗口

[英]Use sf polygon object as window in spatstat

Hello all potential helpers,你好所有潜在的帮助者,

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.我有一个从tigris包获得的SpatialPolygonDataFrame对象,我想在创建ppp对象时将它用作多边形窗口。 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)

ppp对象

Thank you for your help.感谢您的帮助。

Note: I have edited this answer to contain full details.注意:我已编辑此答案以包含完整的详细信息。

As @TimSalabim mentions this is under way in sf , but until then you have to go through the old sp classes such as SpatialPolygons .正如@TimSalabim 提到的,这是在sf ,但在那之前你必须通过旧的sp类,如SpatialPolygons Use something like as_Spatial in sf and then load maptools and use as.owin or as(x, "owin") on the Spatial object.sf使用类似as_Spatial东西,然后加载maptools并在Spatial对象上使用as.owinas(x, "owin") as.owin as(x, "owin")

Furthermore, you can only use coordinates in planar (projected) space with spatstat and not coordinates on the curved surface of the earth.此外,您只能使用带有spatstat平面(投影)空间中的坐标,而不能使用地球曲面上的坐标。 You have to project to a relevant planar coordinates system.您必须投影到相关的平面坐标系。 Maybe <epsg.io/6345> is usable in this case.也许 <epsg.io/6345> 在这种情况下可用。 To project to this coordinate system use sf::st_transform(county_one, crs = 6345) .要投影到此坐标系,请使用sf::st_transform(county_one, crs = 6345) Afterwards you convert to Spatial and then owin .之后,您转换为Spatial ,然后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 random points in the county:县内100个随机点:

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

just want to note here that coercion methods for sf classes are now registered (if that's the right word) by the sf package.只是想在这里注意 sf 类的强制方法现在由 sf 包注册(如果这是正确的词)。 I don't fully understand the R magic that finds methods, but it does work.我并不完全理解 R 寻找方法的魔法,但它确实有效。

library(sf)
library(spatstat)

> methods(as.owin)
 [1] as.owin.boxx                    as.owin.data.frame              as.owin.default                 as.owin.distfun                
 [5] as.owin.dppm                    as.owin.funxy                   as.owin.im                      as.owin.influence.ppm          
 [9] as.owin.kppm                    as.owin.layered                 as.owin.leverage.ppm            as.owin.linfun                 
[13] as.owin.linnet                  as.owin.lintess                 as.owin.lpp                     as.owin.lppm                   
[17] as.owin.msr                     as.owin.MULTIPOLYGON*           as.owin.nnfun                   as.owin.owin                   
[21] as.owin.POLYGON*                as.owin.ppm                     as.owin.ppp                     as.owin.psp                    
[25] as.owin.quad                    as.owin.quadratcount            as.owin.quadrattest             as.owin.rmhmodel               
[29] as.owin.sf*                     as.owin.sfc*                    as.owin.sfc_MULTIPOLYGON*       as.owin.sfc_POLYGON*           
[33] as.owin.SpatialGridDataFrame*   as.owin.SpatialPixelsDataFrame* as.owin.SpatialPolygons*        as.owin.tess                   
see '?methods' for accessing help and source code

So, assuming you have properly projected your data (as noted by @Ege Rubak), calling as.owin directly should work:因此,假设您已正确投影数据(如@Ege Rubak 所述),直接调用as.owin应该可以工作:

library(tigris)

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

county <- st_union(county)

county <- st_transform(county, crs = 6345)

window <- as.owin(county)

plot(window)

在此处输入图片说明

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

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