I am attempting the seemingly simple task of converting a .csv with lat, lon and population values into a .tif (raster).

Here's my R code:

geoName <- read.csv("/pathToData/geoName.csv")
geoName_raster <- raster(xmn=min(geoName$longitude), xmx=max(geoName$longitude), ymn=min(geoName$latitude), ymx=max(geoName$latitude), res=0.0027, crs="+proj=longlat +datum=WGS84")
geoName_rasterize <- rasterize(geoName[, c('longitude', 'latitude')], geoName_raster, geoName[, 'population'], fun=sum)
writeRaster(geoName_rasterize, "/pathToData/geoName_rasterize.tif")

The .csv is formatted like this:


The output has bands of null values which appear to be influenced by the res value: the bands are further apart as res approaches 0.

  • The .tif with narrow bands below is with no res specified no <code>res</code> option
  • The .tif with wide bands is with res = 0.0027 (which is the approximate width in degrees of a Bing tile at z17 which is the resolution of the population data set) res = 0.0027

I've read a few different R documentation pages outlining resolution, but I don't really understand its function.

My questions: what is res doing and how do I replace these bands with the data which belongs in those pixels?

Be gentle: I'm very new to R.

  • 1
    This is pretty hard to help with without some more information, ideally your CSV file so we can all replicate your maps as a starting point. If you can't release that then the range of X and Y for your data would help so we can at least replicate geoName_raster and then maybe experiment with that. – Spacedman Oct 30 '19 at 14:21
  • 1
    Also these sorts of lines sometimes appear as aliasing artefacts caused by plotting, for example, a 1000 pixel raster in a 900 pixel window. Every 100 pixels there's a shift and a pixel gets dropped. If you resize the graphics window what happens? And how are you plotting these things? Give code. – Spacedman Oct 30 '19 at 14:22
  • David, are you sure it's not a gridded data set - turned into a table in CSV? It doesn't seem so from the points you provided, but it seems odd to have population as a sparse metric like this - what is dim(geoName) and can you try rasterFromXYZ(geoName). Further, I'd plot the points so we can get a sense of them as a figure plot(geoName[1:2], pch = ".") – mdsumner Oct 30 '19 at 21:08
  • @Spacedman - What's represented in the .tif is Haiti's Nord-Ouest department. The population data is from my employer's internal database and I will see if I can share the raw .csv. The "grid" of pixels has holes where it's obvious natural features mean there is no human population (e.g., water). As for your questions about plotting, all the code I've used is in the post (I'm also using RStudio). Pretty simple, hence my frustration as to why this is happening. Normally I'd be lazy and use QGIS to do this conversion, but there's a bug in the way, so I've dipped my toes into R. – davidafuller01 Oct 30 '19 at 22:36
  • @mdsumner - To fill in some of the gaps, please see my post to @Spacedman. The population values in my sample are placeholders (not real), but at Bing zoom 17, that's roughly 611m wide at the equator. I did try rasterFromXYZ, but got a cell sizes are not regular error, so I went back to my original code. – davidafuller01 Oct 30 '19 at 22:45

Your Answer

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

Browse other questions tagged or ask your own question.