Spatial join using shapefiles with R

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Spatial join using shapefiles with R

Jose Bustos Melo
Hi everyone,

Its have been very difficult to do an spatial join with R and I couldn't find any good manual about it. So, I will explain it if someone is willing to help.

I have one Shapefile with poligons (with its number) and another Shapefile with xy points. My idea is to obtain the number of the polygons where the points are found, in order to obtain their social data of those polygons.

How can I do it? Is there any good way to do it? Where can I find informationa bout it? Any function?

Thanks in advance
José

        [[alternative HTML version deleted]]


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Spatial join using shapefiles with R

Lyndon Estes-2
Hi Jose,

This function might help with what you want to do.

Cheers, Lyndon

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)
}




On Tue, Jan 10, 2012 at 1:06 PM, Jose Bustos Melo <[hidden email]>wrote:

> Hi everyone,
>
> Its have been very difficult to do an spatial join with R and I couldn't
> find any good manual about it. So, I will explain it if someone is willing
> to help.
>
> I have one Shapefile with poligons (with its number) and another Shapefile
> with xy points. My idea is to obtain the number of the polygons where the
> points are found, in order to obtain their social data of those polygons.
>
> How can I do it? Is there any good way to do it? Where can I find
> informationa bout it? Any function?
>
> Thanks in advance
> José
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
        [[alternative HTML version deleted]]


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Spatial join using shapefiles with R

christiaan pauw-2
In reply to this post by Jose Bustos Melo
Hallo Jose

Make sure you have:
library(rgdal)
library(sp)

read:
help(readOGR)

on reading Shapefiles into R

and;

help(overlay)

for the point-in-polygon operation

regards
Christiaan

P.S Please post repoducible code if you want more advice. See the posting guide

On 10 January 2012 20:06, Jose Bustos Melo <[hidden email]> wrote:

> Hi everyone,
>
> Its have been very difficult to do an spatial join with R and I couldn't find any good manual about it. So, I will explain it if someone is willing to help.
>
> I have one Shapefile with poligons (with its number) and another Shapefile with xy points. My idea is to obtain the number of the polygons where the points are found, in order to obtain their social data of those polygons.
>
> How can I do it? Is there any good way to do it? Where can I find informationa bout it? Any function?
>
> Thanks in advance
> José
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



--
Christiaan Pauw
Nova Institute
www.nova.org.za

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Spatial join using shapefiles with R

Jose Bustos Melo
In reply to this post by Lyndon Estes-2
Hi Lyndon,

I really apreciate you help. Your function was very helpfull. However, in this case polygons are block (block where many homes are). The avenue and street aren't assigned at any polygon, so if a point is out of a polygon is there any way to get it inside the nearest polygon?. For example, any point near (50 meters) the polygon should correspond to that polygon. So, it this way I could change the precision of the estimation, don't you?

Than you in advance!
José



________________________________
 De: Lyndon Estes <[hidden email]>

CC: "[hidden email]" <[hidden email]>
Enviado: martes 10 de enero de 2012 15:21
Asunto: Re: [R-sig-Geo] Spatial join using shapefiles with R


Hi Jose, 

This function might help with what you want to do.  

Cheers, Lyndon 

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)
}





ote:

Hi everyone,

>
>Its have been very difficult to do an spatial join with R and I couldn't find any good manual about it. So, I will explain it if someone is willing to help.
>
>I have one Shapefile with poligons (with its number) and another Shapefile with xy points. My idea is to obtain the number of the polygons where the points are found, in order to obtain their social data of those polygons.
>
>How can I do it? Is there any good way to do it? Where can I find informationa bout it? Any function?
>
>Thanks in advance
>José
>
>       [[alternative HTML version deleted]]
>
>
>_______________________________________________
>R-sig-Geo mailing list
>[hidden email]
>https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
        [[alternative HTML version deleted]]


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Spatial join using shapefiles with R

Lyndon Estes-2
Hi Jose,

If I understand correctly, some of the points lie outside of the polygons,
but you want to pick up the information for the nearest associated
polygon?

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.

I haven't done this type of operation in R, but I suspect you could find
your answer with the following functions:

library(sp)
?over

library(rgeos)
?gBuffer
?gIntersects

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.

Cheers, Lyndon



On Tue, Jan 10, 2012 at 3:33 PM, Jose Bustos Melo <[hidden email]>wrote:

> Hi Lyndon,
>
> I really apreciate you help. Your function was very helpfull. However, in
> this case polygons are block (block where many homes are). The avenue and
> street aren't assigned at any polygon, so if a point is out of a polygon is
> there any way to get it inside the nearest polygon?. For example, any point
> near (50 meters) the polygon should correspond to that polygon. So, it this
> way I could change the precision of the estimation, don't you?
>
> Than you in advance!
> José
>
>   ------------------------------
> *De:* Lyndon Estes <[hidden email]>
> *Para:* Jose Bustos Melo <[hidden email]>
> *CC:* "[hidden email]" <[hidden email]>
> *Enviado:* martes 10 de enero de 2012 15:21
> *Asunto:* Re: [R-sig-Geo] Spatial join using shapefiles with R
>
> Hi Jose,
>
> This function might help with what you want to do.
>
> Cheers, Lyndon
>
> 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)
> }
>
>
>
>
> On Tue, Jan 10, 2012 at 1:06 PM, Jose Bustos Melo <[hidden email]>wrote:
>
> Hi everyone,
>
> Its have been very difficult to do an spatial join with R and I couldn't
> find any good manual about it. So, I will explain it if someone is willing
> to help.
>
> I have one Shapefile with poligons (with its number) and another Shapefile
> with xy points. My idea is to obtain the number of the polygons where the
> points are found, in order to obtain their social data of those polygons.
>
> How can I do it? Is there any good way to do it? Where can I find
> informationa bout it? Any function?
>
> Thanks in advance
> José
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
>
>
        [[alternative HTML version deleted]]


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Spatial join using shapefiles with R

Jose Bustos Melo
Hi Lyndon,

You are right, I need to pick up the information for the nearest associated polygon. I will  try  increasing the radius of the points and then I think could work.

However, I have found it in ArcGIS. They have named as CLOSEST match_option, but I don't have access to that software. So, I will try your advice.

Thank you so much, you have been a very good helper. This topic is very dark for me.


Thank again!
José




________________________________
 De: Lyndon Estes <[hidden email]>

CC: "[hidden email]" <[hidden email]>
Enviado: miércoles 11 de enero de 2012 11:25
Asunto: Re: [R-sig-Geo] Spatial join using shapefiles with R


Hi Jose,

If I understand correctly, some of the points lie outside of the polygons, but you want to pick up the information for the nearest associated polygon? 

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. 

I haven't done this type of operation in R, but I suspect you could find your answer with the following functions:

library(sp)
?over

library(rgeos)
?gBuffer
?gIntersects

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.

Cheers, Lyndon  





ote:

Hi Lyndon,
>
>I really apreciate you help. Your function was very helpfull. However, in this case polygons are block (block where many homes are). The avenue and street aren't assigned at any polygon, so if a point is out of a polygon is there any way to get it inside the nearest polygon?. For example, any point near (50 meters) the polygon should correspond to that polygon. So, it this way I could change the precision of the estimation, don't you?
>
[[elided Yahoo spam]]
>José
>
>
>
>
>________________________________
> De: Lyndon Estes <[hidden email]>

>CC: "[hidden email]" <[hidden email]>
>Enviado: martes 10 de enero de 2012 15:21
>Asunto: Re: [R-sig-Geo] Spatial join using shapefiles with R
>
>
>
>Hi Jose, 
>
>
>This function might help with what you want to do.  
>
>
>Cheers, Lyndon 
>
>
>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)
>}
>
>
>
>
>
>
>
rote:

>
>Hi everyone,
>>
>>Its have been very difficult to do an spatial join with R and I couldn't find any good manual about it. So, I will explain it if someone is willing to help.
>>
>>I have one Shapefile with poligons (with its number) and another Shapefile with xy points. My idea is to obtain the number of the polygons where the points are found, in order to obtain their social data of those polygons.
>>
>>How can I do it? Is there any good way to do it? Where can I find informationa bout it? Any function?
>>
>>Thanks in advance
>>José
>>
>>       [[alternative HTML version deleted]]
>>
>>
>>_______________________________________________
>>R-sig-Geo mailing list
>>[hidden email]
>>https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>
>
>
        [[alternative HTML version deleted]]


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo