point.in.poly 函数不起作用,因为它已被弃用?

问题描述 投票:0回答:1

我已经使用 R 中的开放街道地图提取了某些地址的坐标,为了确保地理编码正确,我想检查我找到的坐标是否落在城市边界内。

cities <- readOGR("cities")
crs_project<-("+proj=longlat +datum=WGS84 +no_defs")

coordinates(extracted_gps)<-~lon+lat
proj4string(extracted_gps)<-CRS("+proj=longlat +datum=WGS84 +no_defs")
extracted_gps<-spTransform(extracted_gps, CRS(crs_project))

extracted_gps <- spatialEco::point.in.poly(extracted_gps, cities)

当我尝试执行此操作时,收到以下错误:

Warning message:
Function is deprecated because sf::st_intersection
    intersections points an polygons and returns associated
         attributes

尝试运行 sf::st_intersection,我得到这个:

extracted_gps <- sf::st_intersection(extracted_gps, cities)

Error in UseMethod("st_intersection") : 
  no applicable method for 'st_intersection' applied to an object of class "character"

城市是一个

SpatialPolygonsDataFrame
,extracted_gps 是一个正式类
SpatialPointsDataFrame

r geospatial sf rgeo
1个回答
0
投票

您需要将

sp
对象转换为
sf
对象。您可以使用
st_as_sf()
包中的
sf
函数来完成此操作。

在这里你可以做到这一点,

# Convert sp objects to sf
cities_sf <- sf::st_as_sf(cities)
extracted_gps_sf <- sf::st_as_sf(extracted_gps)

# use sf functions
extracted_gps_sf <- sf::st_intersection(extracted_gps_sf, cities_sf)

st_as_sf()
函数将
sp
对象转换为
sf
对象,允许您在它们上使用较新的sf函数。然后可以毫无问题地在这些 sf 对象上使用
st_intersection()
函数。

© www.soinside.com 2019 - 2024. All rights reserved.