Use sf polygon object as window in spatstat

后端 未结 2 1128
隐瞒了意图╮
隐瞒了意图╮ 2021-01-20 03:15

Hello all potential helpers,

I have a SpatialPolygonDataFrame object obtained from the tigris package and I would like to use it as a polyg

2条回答
  •  没有蜡笔的小新
    2021-01-20 04:00

    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. Use something like as_Spatial in sf and then load maptools and use as.owin or as(x, "owin") on the Spatial object.

    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 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 random points in the county:

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

提交回复
热议问题