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

e1xvtsh3  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(178)

你好,所有潜在的帮助者,
我有一个从tigris包中获得的SpatialPolygonDataFrame对象,我想在创建ppp对象时将其用作多边形窗口。下面是我的尝试:

# 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)

感谢你的帮助.

wtlkbnrh

wtlkbnrh1#

**注意:我已经编辑了这个答案,以包含完整的细节。

正如@TimSalabim提到的,这是在sf中进行的,但在此之前,你必须遍历旧的sp类,如SpatialPolygons。在sf中使用类似as_Spatial的东西,然后加载maptools,并在Spatial对象上使用as.owinas(x, "owin")
此外,spatstat只能使用平面(投影)空间中的坐标,而不能使用地球曲面上的坐标。您必须投影到相关的平面坐标系。epsg.io/6345在这种情况下,< www.example.com >可能可用。要投影到该坐标系,请使用sf::st_transform(county_one, crs = 6345)。然后转换为Spatial,然后转换为owin。* 注意:选择相关的投影是一门科学,我对它了解不多,所以如果你想确保你不会得到太扭曲的结果,就做一些研究。
具体来说,在原始示例中,您可以执行以下操作:

# 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个随机点:

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

oxcyiej7

oxcyiej72#

这里只想指出sf类的强制方法现在由sf包注册(如果这个词正确的话)。我不完全理解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

字符串
所以,假设你已经正确地投影了你的数据(正如@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)

相关问题