英文:
point.in.poly function does not work because it is deprecated?
问题
我已经使用R从Open Street Map中提取了某个地址的坐标,并且为了确保地理编码正确,我想要检查我找到的坐标是否位于城市边界内。
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"
cities
是一个SpatialPolygonsDataFrame
,而extracted_gps
是一个SpatialPointsDataFrame
的正式类。
英文:
I've extracted coordinates for some address using Open Street Map in R and to ensure geocoding is correct, I want to check if the coordinates I found fall within the citys' boundaries.
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)
When I try to do this, I receive the following error:
Warning message:
Function is deprecated because sf::st_intersection
intersections points an polygons and returns associated
attributes
Trying to run sf::st_intersection, I get this:
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"
The cities is a SpatialPolygonsDataFrame
and extracted_gps is a Formal class SpatialPointsDataFrame
.
答案1
得分: 2
你需要将你的 sp
对象转换为 sf
对象。你可以使用 sf
包中的 st_as_sf()
函数来实现这个目标。
在这里你可以这样做,
# 将 sp 对象转换为 sf
cities_sf <- sf::st_as_sf(cities)
extracted_gps_sf <- sf::st_as_sf(extracted_gps)
# 使用 sf 函数
extracted_gps_sf <- sf::st_intersection(extracted_gps_sf, cities_sf)
st_as_sf()
函数将 sp
对象转换为 sf
对象,使你能够在它们上使用更新的 sf 函数。然后,可以在这些 sf 对象上使用 st_intersection()
函数而不会出现问题。
英文:
You need to convert your sp
objects to sf
objects. You can do this using the st_as_sf()
function from the sf
package.
Here you can do this,
# 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)
The st_as_sf()
function converts sp
objects to sf
objects, allowing you to use the newer sf functions on them. The st_intersection()
function can then be used on these sf objects without issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论