I have met this frustrating problem while using R doing some GIS operation.
The problem is, I want to locate some spatial points (SpatialPointsDataFrame) on geographical grids (SpatialPixelsDataFrame). But it seems that it would take FOREVER for me to do this.
I put my code here for you. First I read my data:
conf <- readOGR(dsn = path.expand("C:/Users/Abel/Desktop/conflict/reduce_to_short"), layer = "conflict_short") proj.crs <- proj4string(CRS( "+proj=utm +zone=33 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")) conf.newproj <- spTransform(conf, proj.crs) africa_grid <- readRDS(file = "C:/Users/Abel/Desktop/divide/grid_with_adminfo/africa_grid.rds")
The conf.newproj is conflict data saved in SpatialPointsDataFrame type, and have about 170,000 observations. And the africa_grid is saved in SpatialPixelsDataFrame, which has 5 million pixels. I wanted to know which grid/pixel does each conflict locate, so I first tried this command:
cc <- over(conf.newproj, africa_grid)
because I followed the intuition of over function that it would return the information from the later object to each point in the first object. But later I found out that for SpatialPixelDataFrame, I should, instead using:
cc <- over(africa_grid, conf.newprooj)
and this should, I quote, "returns grid values x at spatial point locations y; NA for NA grid cells or points outside the grid".
But the command ran so slow even when I tried on a small sample including 10 conflicts. It did not finish after 30 minutes. If only find grids for 10 conflicts would take more than half an hour, I have no idea how long would it take to complete my full sample which include 170 thousand conflicts.
At first I thought this is because I use a SpatialPointsDataFrame instead of SpatialPoints as the command requires. So I changed my sample into SpatialPoints. But it still runs so slow.
I want to know whether there are other function for me to do this operation, or way to accelerate this process.