Dear geo-list,
I'm just starting using R and the rgdal package to manage and create some shapes and I am hitting some speed issues while using the %over% function. R is significantly slower than ArcGIS.
Here is what I'm doing (I've shorten the code to make it easier to follow, I don't expect it to be reproducible).
# I first load a shape giving the perimeter of the territory of interest
terr <- readOGR("D:\\Couches GIS\\06152\\contour", "contour_carto06152_Project")
# I then want to make a grid of points. The grid is very dense and rectangular
# to do so, I first make a gridtopology
grille.plac.temp1 <- GridTopology(...)
# and get all the coordinates from the grid
grille.plac.temp2 <- coordinates(grille.plac.temp1)
# and finally transform it to a SpatialPointsDataFrame object
grille.plac <- SpatialPointsDataFrame(grille.plac.temp2,data.frame(1:nrow(grille.plac.temp2)),proj4string =CRS(proj4string(terr)))
# The grid is very big as it is a rectangle in which you can fit the territory shape
# (which is of irregular shape). I then want to reduce it's size by keeping only
# to points from the grid that are in the territory. I use the %over% function to do
# that. This is the slow part.
system.time(plac.dans.contour<-grille.plac%over%contour)
user system elapsed
1420.23 9.57 1435.17
# I then make a new shape with only the points from the grid that are in the territory
grille.plac.reduce <- grille.plac[as.vector(!is.na(plac.dans.contour)),]
This code works fine and produce what I want it to produce. However, the %over% functions takes 24 minutes to run, while ArcGIS can run it in less than 1 minute with the intersect tool. Is there a more efficient function than %over% to do what I want? Is it normal that ArcGIS is that much faster than R in that case?
Thanks for your help,
Bastien Ferland-Raymond, M.Sc. Stat., M.Sc. Biol.
Division des orientations et projets sp?ciaux
Direction des inventaires forestiers
Minist?re des Ressources naturelles et de la Faune du Qu?bec
speed problem with %over% function
3 messages · Bastien.Ferland-Raymond at mrnf.gouv.qc.ca, Edzer Pebesma, Francis Markham
Dear Bastien, the %over% method was primary written to work, and it seems that it did. It is hard to tell why ArcGIS is so much faster without access to its source code, and your data. Chances are that it uses a spatial index, and R does not. Talking about spatial indexes, you might want to try %over% in package rgeos (essentially after loading rgeos) as that seems to use a spatial index. Providing a reproducible example is always more inviting for someone on the list (possible me) to look deeper into this. If you want to obtain points on a regular grid within a polygon, sp::spsample with type "regular" may be an alternative route. Package raster may have more efficient routines for many things.
On 02/27/2012 09:54 AM, Bastien.Ferland-Raymond at mrnf.gouv.qc.ca wrote:
Dear geo-list,
I'm just starting using R and the rgdal package to manage and create some shapes and I am hitting some speed issues while using the %over% function. R is significantly slower than ArcGIS.
Here is what I'm doing (I've shorten the code to make it easier to follow, I don't expect it to be reproducible).
# I first load a shape giving the perimeter of the territory of interest
terr <- readOGR("D:\\Couches GIS\\06152\\contour", "contour_carto06152_Project")
# I then want to make a grid of points. The grid is very dense and rectangular
# to do so, I first make a gridtopology
grille.plac.temp1 <- GridTopology(...)
# and get all the coordinates from the grid
grille.plac.temp2 <- coordinates(grille.plac.temp1)
# and finally transform it to a SpatialPointsDataFrame object
grille.plac <- SpatialPointsDataFrame(grille.plac.temp2,data.frame(1:nrow(grille.plac.temp2)),proj4string =CRS(proj4string(terr)))
# The grid is very big as it is a rectangle in which you can fit the territory shape
# (which is of irregular shape). I then want to reduce it's size by keeping only
# to points from the grid that are in the territory. I use the %over% function to do
# that. This is the slow part.
system.time(plac.dans.contour<-grille.plac%over%contour)
user system elapsed
1420.23 9.57 1435.17
# I then make a new shape with only the points from the grid that are in the territory
grille.plac.reduce <- grille.plac[as.vector(!is.na(plac.dans.contour)),]
This code works fine and produce what I want it to produce. However, the %over% functions takes 24 minutes to run, while ArcGIS can run it in less than 1 minute with the intersect tool. Is there a more efficient function than %over% to do what I want? Is it normal that ArcGIS is that much faster than R in that case?
Thanks for your help,
Bastien Ferland-Raymond, M.Sc. Stat., M.Sc. Biol.
Division des orientations et projets sp?ciaux
Direction des inventaires forestiers
Minist?re des Ressources naturelles et de la Faune du Qu?bec
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Edzer Pebesma Institute for Geoinformatics (ifgi), University of M?nster Weseler Stra?e 253, 48151 M?nster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de http://www.52north.org/geostatistics e.pebesma at wwu.de
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20120228/34a1ebc3/attachment.pl>