I always get error to this when converting shapefile point to ppp spatstat that windowed by a polygon shapefile

# polygon that to be window
neighborhoods <- st_read("neighborhoods/neighborhoods.shp")

# convert CRS to planar projection as recommended by (https://stackoverflow.com/questions/59597078/use-sf-polygon-object-as-window-in-spatstat)
neighborhoods_3857 <- st_transform(neighborhoods, crs = 3857)

# point that to be PPP spatstat
trees <- st_read("trees/trees.shp")

# convert to planar projection
trees_3857 <- st_transform(trees, crs = 3857)

The problems, the "trees_3857" doesn't have dataframe columns that represent in EPSG3857 coordinates, so Feature column of "trees_3857" doesn't have x and y columns that respect to EPSG 3857

q <- ppp(x=?, y=?, win=neighborhoods_3857)

what I have done but error

first =using as.ppp instead of ppp to convert a vector shp to ppp spatstat

test1 <- as.ppp(trees_3857, win=neighborhoods_3857)
Error in as.ppp.sf(trees_3857, win = neighborhoods_3857): unused argument (win = neighborhoods_3857)

second = add geometries of "trees_3857" to feature columns with QGIS named "xcoord" & "ycoord"

test2 <- ppp(trees_3857$xcoord, trees_3857$ycoord, win = neighborhoods_3857)
Warning message:
"65217 points were rejected as lying outside the specified window"

You can get the coordinates of something with st_point geometry using st_coordinates.

Make a simple points data set like you would get with st_read and st_transform:

> pts = st_as_sf(data.frame(x=runif(10),y=runif(10),A=letters[1:10]), coords=c("x","y"), crs=3857)

Then use as.ppp with the coordinates and the window:

> as.ppp(st_coordinates(pts),W=unit.square())
Planar point pattern: 10 points
window: rectangle = [0, 1] x [0, 1] units

I don't know why your $xcoords and $ycoords doesn't work, I can only suggest it or the window is in the wrong coordinate system, but hard to tell unless you show us some of it!

  • 1
    Can I upvote you for three thousand times? For first time, I know "st_coordinates" function. You solve my problem after I struggled for 18 hours. – Ichsan Jun 14 '20 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.