I am an absolute beginner in analyzing geographic data with R.

I have a database with longitude and latitude of around a million buildings in a country. I would like to find the average density of those buildings. For that I have divided the whole country into several cells using Raster and applied projection with the appropriate proj4 profile. Now I want to check which buildings are coming under which cell to find the density.

> metersCoordinates <- totalMeterDatabase[,c("longitude","latitude")]
> coordinates(metersCoordinates) <- ~longitude+latitude
> r <- raster(ncols=6000, nrows=2000)
> r[] <- 0
> crs(r) <- "+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=6000 +y_0=2000 +ellps=bessel +towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs"

> projection(r)

Could you please tell me how can I find it?

  • 1
  • Thanks for the link. The link helped me to find the right answer with the help of Amadou Kone answer. I have edited the Amadou Kone answer with your example. – Vamsi Sep 18 '15 at 7:02

If I understand your question, you can use the extract() function. First, give each raster cell a unique value when you create the raster. I'd also recommend giving the raster the extent of your coordinates. So:

 metersCoordinates <- totalMeterDatabase[,c("longitude","latitude")]
 coordinates(metersCoordinates) <- ~longitude+latitude
 r <- raster(matrix(seq(1,6000*2000), ncol=6000, nrow=2000), 
 totalMeterDatabase$cellnumber <- extract(r, metersCoordinates)

And use that to do your aggregation/analysis.

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.