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

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

point.in.poly function does not work because it is deprecated?

问题

我已经使用R从Open Street Map中提取了某个地址的坐标,并且为了确保地理编码正确,我想要检查我找到的坐标是否位于城市边界内。

  1. cities <- readOGR("cities")
  2. crs_project <- ("+proj=longlat +datum=WGS84 +no_defs")
  3. coordinates(extracted_gps) <- ~lon+lat
  4. proj4string(extracted_gps) <- CRS("+proj=longlat +datum=WGS84 +no_defs")
  5. extracted_gps <- spTransform(extracted_gps, CRS(crs_project))
  6. extracted_gps <- spatialEco::point.in.poly(extracted_gps, cities)

当我尝试这样做时,我收到以下错误:

  1. Warning message:
  2. Function is deprecated because sf::st_intersection
  3. intersections points an polygons and returns associated
  4. attributes

尝试运行sf::st_intersection时,我得到了这个错误:

  1. extracted_gps <- sf::st_intersection(extracted_gps, cities)
  2. Error in UseMethod("st_intersection") :
  3. 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.

  1. cities &lt;- readOGR(&quot;cities&quot;)
  2. crs_project&lt;-(&quot;+proj=longlat +datum=WGS84 +no_defs&quot;)
  3. coordinates(extracted_gps)&lt;-~lon+lat
  4. proj4string(extracted_gps)&lt;-CRS(&quot;+proj=longlat +datum=WGS84 +no_defs&quot;)
  5. extracted_gps&lt;-spTransform(extracted_gps, CRS(crs_project))
  6. extracted_gps &lt;- spatialEco::point.in.poly(extracted_gps, cities)

When I try to do this, I receive the following error:

  1. Warning message:
  2. Function is deprecated because sf::st_intersection
  3. intersections points an polygons and returns associated
  4. attributes

Trying to run sf::st_intersection, I get this:

  1. extracted_gps &lt;- sf::st_intersection(extracted_gps, cities)
  2. Error in UseMethod(&quot;st_intersection&quot;) :
  3. 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() 函数来实现这个目标。

在这里你可以这样做,

  1. # 将 sp 对象转换为 sf
  2. cities_sf <- sf::st_as_sf(cities)
  3. extracted_gps_sf <- sf::st_as_sf(extracted_gps)
  4. # 使用 sf 函数
  5. 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,

  1. # Convert sp objects to sf
  2. cities_sf &lt;- sf::st_as_sf(cities)
  3. extracted_gps_sf &lt;- sf::st_as_sf(extracted_gps)
  4. # use sf functions
  5. 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:

确定