1

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.

3
  • 2
    The only temporal descriptors you have used are "slow" and "FOREVER". These are not empirical values. Please Edit the question so that an actual performance rate can be calculated. – Vince Jan 16 at 12:27
  • Thank you for your advice. I do need to try to make things clearer so that it would be more straightforward for others. – abelxu73 Jan 16 at 12:36
  • 2
    Use the raster package, coerce the SpatialPixelsDataFrame object to a raster or stack and use raster::extract. – Jeffrey Evans Jan 17 at 3:19
1

I think I know the reason for it. Because when I use the over command, this will collapse each pixel into central point. This might be time consuming and also make point-to-point over operation an impossible task. But I am still curious about how to generate this kind of grid and locate spatial point on it correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.