title: How to pick up the information for the nearest associated polygon to points using R?
body: <p>I'm figuring out how to do a Intersection (Spatial Join) between point and polygons from shapefiles. My idea is to get the closest points and those points that match completely inside the polygons. In ARGIS there's a function for match option named CLOSEST and they have defined by: "The feature in the join features that is closest to a target feature is matched. It is possible that two or more join features are the same distance away from the target feature. When this situation occurs, one of the join features is randomly selected as the matching feature."</p>
  
  <p>I have a function to intersect points into polygons, it was kindly contributed by Lyndon Estes at the r-sig-geo list and the code works very well when all the polygons have filled all the area. The second case is known as a Spatial join distance and in ArcGIS is know as  INTERSECT when match_option is CLOSEST, as ArcGIS does. So, you can modify the minimal distance between the point and the polygon when the area is not filled by all polygons.</p>
  
  <p>Here's the data and the function of the first INTERSECT:</p>
  
  <pre><code>library(rgeos)
  library(sp) 
  library(maptools)
  library(rgdal)
  library(sp)
  xy.map &lt;- readShapeSpatial("http://www.udec.cl/~jbustosm/points.shp")
  manzana.map &lt;- readShapeSpatial("http://www.udec.cl/~jbustosm/manzanas_from.shp" )
  
  IntersectPtWithPoly &lt;- 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 &lt;- overlay(y, x)
  
    # Bind captured data to points dataframe
    x2 &lt;- cbind(x, z)
  
    # Make it back into a SpatialPointsDataFrame 
    # Account for different coordinate variable names 
    if(("coords.x1" %in% colnames(x2)) &amp; ("coords.x2" %in% colnames(x2))) {
      coordinates(x2) &lt;- ~coords.x1 + coords.x2  
    } else if(("x" %in% colnames(x2)) &amp; ("x" %in% colnames(x2))) {
      coordinates(x2) &lt;- ~x + y 
    }
  
    # Reassign its projection if it has one
    if(is.na(CRSargs(x@proj4string)) == "FALSE") {
      x2@proj4string &lt;- x@proj4string  
    }
    return(x2)
  }
  
  
  test&lt;-IntersectPtWithPoly (xy.map,manzana.map)
  </code></pre>
  
  <p>Sharing some ideas with Lyndon, he told me this:</p>
  
  <hr>
  
  <p>I think the easiest thing to do would be to put a buffer around each of the points (you could specify 50 m if it is in projected coordinates), converting them to polygons, and then your task becomes an intersection of two different polygon objects. </p>
  
  <p>I haven't done this type of operation in R, but I suspect you could find your answer with the following functions:</p>
  
  <pre><code>library(sp)
  ?over
  
  library(rgeos)
  ?gBuffer
  ?gIntersects
  </code></pre>
  
  <p>I suggest putting up a subset of your data illustrating the problem, and then maybe someone else who has a better idea on polygon to polygon intersects/overlays could suggest the method. </p>
  
  <p>should be made in the points radius which are in the shapefile in order to make them get into the nearest polygon. </p>
  
  <p>I know that this functions could help to achive it. </p>
  
  <pre><code>library(sp)
  ?over
  
  library(rgeos)
  ?gBuffer
  ?gIntersects
  </code></pre>
  
  <hr>
  
  <p>I'm working on it, so any comment or help, would be very apreciated!</p>