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

huangapple go评论70阅读模式
英文:

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 &lt;- readOGR(&quot;cities&quot;)
crs_project&lt;-(&quot;+proj=longlat +datum=WGS84 +no_defs&quot;)

coordinates(extracted_gps)&lt;-~lon+lat
proj4string(extracted_gps)&lt;-CRS(&quot;+proj=longlat +datum=WGS84 +no_defs&quot;)
extracted_gps&lt;-spTransform(extracted_gps, CRS(crs_project))

extracted_gps &lt;- 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 &lt;- sf::st_intersection(extracted_gps, cities)

Error in UseMethod(&quot;st_intersection&quot;) : 
  no applicable method for &#39;st_intersection&#39; applied to an object of class &quot;character&quot;

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 &lt;- sf::st_as_sf(cities)
extracted_gps_sf &lt;- sf::st_as_sf(extracted_gps)

# use sf functions
extracted_gps_sf &lt;- 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.

huangapple
  • 本文由 发表于 2023年7月28日 05:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定