gpt4 book ai didi

r - 将 sf 转换为未标记的 ppp

转载 作者:行者123 更新时间:2023-12-04 13:09:59 25 4
gpt4 key购买 nike

我想将一个sf 对象转换成一个未标记的ppp。 (根据 this post,现在支持从 sfppp 的转换。)

library(sf)

#Initialise sf object
pp <- structure(list(X = c(959207.877070254, 959660.734838225, 951483.685462513, 951527.767554883, 958310.673042469, 950492.05212104, 959660.734838225, 959207.877070254, 960500.020456073, 959660.734838225), Y = c(1944457.42827898, 1955543.76027363, 1939982.16629396, 1940216.55143212, 1954704.68186897, 1951434.68524296, 1955543.76027363, 1944457.42827898, 1955292.64874361, 1955543.76027363), geometry = structure(list(structure(c(959207.877070254, 1944457.42827898), class = c("XY", "POINT", "sfg")), structure(c(959660.734838225, 1955543.76027363), class = c("XY", "POINT", "sfg")), structure(c(951483.685462513, 1939982.16629396), class = c("XY", "POINT", "sfg")), structure(c(951527.767554883, 1940216.55143212), class = c("XY", "POINT", "sfg")), structure(c(958310.673042469, 1954704.68186897), class = c("XY", "POINT", "sfg")), structure(c(950492.05212104, 1951434.68524296), class = c("XY", "POINT", "sfg")), structure(c(959660.734838225, 1955543.76027363), class = c("XY", "POINT", "sfg")), structure(c(959207.877070254, 1944457.42827898), class = c("XY", "POINT", "sfg")), structure(c(960500.020456073, 1955292.64874361), class = c("XY", "POINT", "sfg")), structure(c(959660.734838225, 1955543.76027363), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = 950492.05212104, ymin = 1939982.16629396, xmax = 960500.020456073, ymax = 1955543.76027363 ), class = "bbox"), crs = structure(list(input = "EPSG:5179", wkt = "PROJCRS[\"Korea 2000 / Unified CS\",\n BASEGEOGCRS[\"Korea 2000\",\n DATUM[\"Geocentric datum of Korea\",\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4737]],\n CONVERSION[\"Korea Unified Belt\",\n METHOD[\"Transverse Mercator\",\n ID[\"EPSG\",9807]],\n PARAMETER[\"Latitude of natural origin\",38,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8801]],\n PARAMETER[\"Longitude of natural origin\",127.5,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8802]],\n PARAMETER[\"Scale factor at natural origin\",0.9996,\n SCALEUNIT[\"unity\",1],\n ID[\"EPSG\",8805]],\n PARAMETER[\"False easting\",1000000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8806]],\n PARAMETER[\"False northing\",2000000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8807]]],\n CS[Cartesian,2],\n AXIS[\"northing (X)\",north,\n ORDER[1],\n LENGTHUNIT[\"metre\",1]],\n AXIS[\"easting (Y)\",east,\n ORDER[2],\n LENGTHUNIT[\"metre\",1]],\n USAGE[\n SCOPE[\"unknown\"],\n AREA[\"Korea, Republic of (South Korea)\"],\n BBOX[28.6,122.71,40.27,134.28]],\n ID[\"EPSG\",5179]]"), class = "crs"), n_empty = 0L)), row.names = c(4177L, 15721L, 21365L, 21973L, 24836L, 59359L, 66313L, 70379L, 83277L, 90828L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(c(X = NA_integer_, Y = NA_integer_), .Label = c("constant", "aggregate", "identity" ), class = "factor"))

library(spatstat)

#Convert to ppp format: method 1
pp1 <- as.ppp(pp)
#Warning message:
#In as.ppp.sf(pp) : only first attribute column is used for marks
pp1 <- unmark(pp1)

#Convert to ppp format: method 2
pp2 <- as.ppp(st_coordinates(pp), st_convex_hull(st_union(pp)))
#Warning message:
#data contain duplicated points

identical(pp1, pp2)
[1] FALSE

第一种方法需要标记,但我无法找到如何关闭它(指定 marks=NULL 不起作用)。之后我删除了标记,但这是一种解决方法。

考虑到警告,第二种方法可能是错误的,尽管我基于 this solution 。 2 个输出不相同。

这些方法是否正确?为什么方法 2 会产生重复的点?是否有更直接的转换方法,没有变通办法?

最佳答案

我不确定以下答案是否适用于每个版本的 spatstat,但我认为如果你想生成一个未标记的 ppp 对象,你应该运行

# packages
suppressPackageStartupMessages({
library(sf)
library(spatstat)
})

# Initialise sf object
pp <- structure(list(X = c(959207.877070254, 959660.734838225, 951483.685462513, 951527.767554883, 958310.673042469, 950492.05212104, 959660.734838225, 959207.877070254, 960500.020456073, 959660.734838225), Y = c(1944457.42827898, 1955543.76027363, 1939982.16629396, 1940216.55143212, 1954704.68186897, 1951434.68524296, 1955543.76027363, 1944457.42827898, 1955292.64874361, 1955543.76027363), geometry = structure(list(structure(c(959207.877070254, 1944457.42827898), class = c("XY", "POINT", "sfg")), structure(c(959660.734838225, 1955543.76027363), class = c("XY", "POINT", "sfg")), structure(c(951483.685462513, 1939982.16629396), class = c("XY", "POINT", "sfg")), structure(c(951527.767554883, 1940216.55143212), class = c("XY", "POINT", "sfg")), structure(c(958310.673042469, 1954704.68186897), class = c("XY", "POINT", "sfg")), structure(c(950492.05212104, 1951434.68524296), class = c("XY", "POINT", "sfg")), structure(c(959660.734838225, 1955543.76027363), class = c("XY", "POINT", "sfg")), structure(c(959207.877070254, 1944457.42827898), class = c("XY", "POINT", "sfg")), structure(c(960500.020456073, 1955292.64874361), class = c("XY", "POINT", "sfg")), structure(c(959660.734838225, 1955543.76027363), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = 950492.05212104, ymin = 1939982.16629396, xmax = 960500.020456073, ymax = 1955543.76027363 ), class = "bbox"), crs = structure(list(input = "EPSG:5179", wkt = "PROJCRS[\"Korea 2000 / Unified CS\",\n BASEGEOGCRS[\"Korea 2000\",\n DATUM[\"Geocentric datum of Korea\",\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4737]],\n CONVERSION[\"Korea Unified Belt\",\n METHOD[\"Transverse Mercator\",\n ID[\"EPSG\",9807]],\n PARAMETER[\"Latitude of natural origin\",38,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8801]],\n PARAMETER[\"Longitude of natural origin\",127.5,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8802]],\n PARAMETER[\"Scale factor at natural origin\",0.9996,\n SCALEUNIT[\"unity\",1],\n ID[\"EPSG\",8805]],\n PARAMETER[\"False easting\",1000000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8806]],\n PARAMETER[\"False northing\",2000000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8807]]],\n CS[Cartesian,2],\n AXIS[\"northing (X)\",north,\n ORDER[1],\n LENGTHUNIT[\"metre\",1]],\n AXIS[\"easting (Y)\",east,\n ORDER[2],\n LENGTHUNIT[\"metre\",1]],\n USAGE[\n SCOPE[\"unknown\"],\n AREA[\"Korea, Republic of (South Korea)\"],\n BBOX[28.6,122.71,40.27,134.28]],\n ID[\"EPSG\",5179]]"), class = "crs"), n_empty = 0L)), row.names = c(4177L, 15721L, 21365L, 21973L, 24836L, 59359L, 66313L, 70379L, 83277L, 90828L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(c(X = NA_integer_, Y = NA_integer_), .Label = c("constant", "aggregate", "identity" ), class = "factor"))

# Convert to ppp format: method 1
(pp1 <- as.ppp(st_geometry(pp)))
#> Planar point pattern: 10 points
#> window: rectangle = [950492.1, 960500] x [1939982.2, 1955543.8] units

reprex package (v0.3.0) 创建于 2021-03-01

您提供的两种方法并不等效,因为 sf 包中定义的 as.ppp.sfc 函数使用矩形 owin对象:

# packages
suppressPackageStartupMessages({
library(sf)
library(spatstat)
})

#Initialise sf object
pp <- structure(list(X = c(959207.877070254, 959660.734838225, 951483.685462513, 951527.767554883, 958310.673042469, 950492.05212104, 959660.734838225, 959207.877070254, 960500.020456073, 959660.734838225), Y = c(1944457.42827898, 1955543.76027363, 1939982.16629396, 1940216.55143212, 1954704.68186897, 1951434.68524296, 1955543.76027363, 1944457.42827898, 1955292.64874361, 1955543.76027363), geometry = structure(list(structure(c(959207.877070254, 1944457.42827898), class = c("XY", "POINT", "sfg")), structure(c(959660.734838225, 1955543.76027363), class = c("XY", "POINT", "sfg")), structure(c(951483.685462513, 1939982.16629396), class = c("XY", "POINT", "sfg")), structure(c(951527.767554883, 1940216.55143212), class = c("XY", "POINT", "sfg")), structure(c(958310.673042469, 1954704.68186897), class = c("XY", "POINT", "sfg")), structure(c(950492.05212104, 1951434.68524296), class = c("XY", "POINT", "sfg")), structure(c(959660.734838225, 1955543.76027363), class = c("XY", "POINT", "sfg")), structure(c(959207.877070254, 1944457.42827898), class = c("XY", "POINT", "sfg")), structure(c(960500.020456073, 1955292.64874361), class = c("XY", "POINT", "sfg")), structure(c(959660.734838225, 1955543.76027363), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = 950492.05212104, ymin = 1939982.16629396, xmax = 960500.020456073, ymax = 1955543.76027363 ), class = "bbox"), crs = structure(list(input = "EPSG:5179", wkt = "PROJCRS[\"Korea 2000 / Unified CS\",\n BASEGEOGCRS[\"Korea 2000\",\n DATUM[\"Geocentric datum of Korea\",\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4737]],\n CONVERSION[\"Korea Unified Belt\",\n METHOD[\"Transverse Mercator\",\n ID[\"EPSG\",9807]],\n PARAMETER[\"Latitude of natural origin\",38,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8801]],\n PARAMETER[\"Longitude of natural origin\",127.5,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8802]],\n PARAMETER[\"Scale factor at natural origin\",0.9996,\n SCALEUNIT[\"unity\",1],\n ID[\"EPSG\",8805]],\n PARAMETER[\"False easting\",1000000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8806]],\n PARAMETER[\"False northing\",2000000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8807]]],\n CS[Cartesian,2],\n AXIS[\"northing (X)\",north,\n ORDER[1],\n LENGTHUNIT[\"metre\",1]],\n AXIS[\"easting (Y)\",east,\n ORDER[2],\n LENGTHUNIT[\"metre\",1]],\n USAGE[\n SCOPE[\"unknown\"],\n AREA[\"Korea, Republic of (South Korea)\"],\n BBOX[28.6,122.71,40.27,134.28]],\n ID[\"EPSG\",5179]]"), class = "crs"), n_empty = 0L)), row.names = c(4177L, 15721L, 21365L, 21973L, 24836L, 59359L, 66313L, 70379L, 83277L, 90828L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(c(X = NA_integer_, Y = NA_integer_), .Label = c("constant", "aggregate", "identity" ), class = "factor"))

# Convert to ppp format: method 1
(pp1 <- as.ppp(st_geometry(pp)))
#> Planar point pattern: 10 points
#> window: rectangle = [950492.1, 960500] x [1939982.2, 1955543.8] units

# Method 2
(pp2 <- as.ppp(
X = st_coordinates(pp),
W = as.owin(st_bbox(pp))
))
#> Warning: data contain duplicated points
#> Planar point pattern: 10 points
#> window: rectangle = [950492.1, 960500] x [1939982.2, 1955543.8] units

identical(pp1, pp2)
#> [1] TRUE

此外,由于作者设置了 check = FALSE,第一个方法没有返回任何警告消息。

anyDuplicated(pp1)
#> [1] TRUE

reprex package (v0.3.0) 创建于 2021-03-01

有关详细信息,请参阅 here

关于r - 将 sf 转换为未标记的 ppp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66418104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com