How to get information about the nearest connected polygon with points using R?

I figure out how to make an intersection (spatial connection) between points and polygons from shapefiles. My idea is to get the closest points and those points that completely coincide inside the polygons. ARGIS has a function for the CLOSEST matching option, and they are defined as follows: “The corresponding function in the connection functions is closest to the target function. It is possible that two or more connection functions are at the same distance from the target. When this situation arises, one from connection functions is randomly selected as a matching function. "

I have a function for intersecting points in polygons, it was kindly provided by Lindon Estes in the r-sig-geo list, and the code works very well when all the polygons filled the entire area. The second case is known as the spatial join distance, and in ArcGIS is known as INTERSECT when match_option is CLOSEST, as ArcGIS does. Thus, you can change the minimum distance between a point and a polygon when the area is not filled with all polygons.

Here is the data and function of the first INTERSECT:

library(rgeos) library(sp) library(maptools) library(rgdal) library(sp) xy.map <- readShapeSpatial("http://www.udec.cl/~jbustosm/points.shp") manzana.map <- readShapeSpatial("http://www.udec.cl/~jbustosm/manzanas_from.shp" ) IntersectPtWithPoly <- function(x, y) { # Extracts values from a SpatialPolygonDataFrame with SpatialPointsDataFrame, and appends table (similar to # ArcGIS intersect) # Args: # x: SpatialPoints*Frame # y: SpatialPolygonsDataFrame # Returns: # SpatialPointsDataFrame with appended table of polygon attributes # Set up overlay with new column of join IDs in x z <- overlay(y, x) # Bind captured data to points dataframe x2 <- cbind(x, z) # Make it back into a SpatialPointsDataFrame # Account for different coordinate variable names if(("coords.x1" %in% colnames(x2)) & ("coords.x2" %in% colnames(x2))) { coordinates(x2) <- ~coords.x1 + coords.x2 } else if(("x" %in% colnames(x2)) & ("x" %in% colnames(x2))) { coordinates(x2) <- ~x + y } # Reassign its projection if it has one if(is.na(CRSargs( x@proj4string )) == "FALSE") { x2@proj4string <- x@proj4string } return(x2) } test<-IntersectPtWithPoly (xy.map,manzana.map) 

Sharing some ideas with Lyndon, he told me this:


I think the easiest way is to make a buffer around each of the points (you can specify 50 m if it is in the projected coordinates), converting them into polygons, and then your task will become the intersection of two different polygonal objects.

I did not do this type of operation in R, but I suspect you might find your answer with the following functions:

 library(sp) ?over library(rgeos) ?gBuffer ?gIntersects 

I suggest posting a subset of your data illustrating the problem, and then maybe someone else who has the best idea of ​​polygons for intersecting / overlaying polygons can suggest a method.

should be done in the radius of the points that are in the shapefile to make them fall into the nearest polygon.

I know that these features can help achieve this.

 library(sp) ?over library(rgeos) ?gBuffer ?gIntersects 

I am working on this, so any comments or help would be greatly appreciated!

+6
source share
1 answer

It turned out that you can use polygons for polygonal overlays using sp and rgeos . You will need to load rgeos after loading sp.

 library(rgeos) over(polygon1, polygon2) 
+1
source

Source: https://habr.com/ru/post/905748/


All Articles