Referring to my previous question, how do I "geocode"/convert - using R - a generated raster image to a format that can be shown as a layer on a map? In short, I'm aiming to recreate the results of the commercial SpatialKey (screenshot) software.

I'm using a GeoServer instance to host the data for the maps, but how can I incorporate the required projection and coordinate info into the image so it will align correctly with its true geographical position. The density heatmap was produced using spatial data.

  • As this is solely a programming question this isn't really on topic. Have you looked at the spatstat package information and the short course pdf to see if it is covered there? I'm sure this has come up before, and a quick google search brings up this relevant R-help answer. I wouldn't be surprised if more direct solutions exist as well with some more searching.
    – Andy W
    Commented Apr 19, 2012 at 16:33
  • How have you made the generated raster image? Is it coming from another application, or made within R?
    – Simbamangu
    Commented Apr 20, 2012 at 6:47
  • It's generated within R using the image() function.
    – Mimo
    Commented Apr 20, 2012 at 6:57
  • x has unequal horizontal and vertical resolutions. Such data cannot be stored in arc-ascii format My x has 4684, 1950 dimension, it complains about the resolution. I made it the same though. Buggy
    – user22146
    Commented Sep 19, 2013 at 17:11

2 Answers 2


The raster package lets you create arbitrary rasters (size, projection) or use existing objects, like matrices; you can then assign projections & extents . Your image() function takes a matrix argument which can be used directly:

## Create a matrix with random data & use image()
xy <- matrix(rnorm(400),20,20)

# Turn the matrix into a raster
rast <- raster(xy)
# Give it lat/lon coords for 36-37°E, 3-2°S
extent(rast) <- c(36,37,-3,-2)
# ... and assign a projection
projection(rast) <- CRS("+proj=longlat +datum=WGS84")

You can then use writeRaster() with any number of formats, e.g.

writeRaster(rast, "~/myraster.asc", format = "ascii")

There are a number of different ways, but taking a stab that you want to use open-source tools on Windows I can suggest 3 options:

  1. Use gdal_translate tool in GDAL (open-source geo-raster manipulation library). You probably need the -gcp pixel line easting northing elevation parameter where pixel and line is x / y on the raster (http://www.gdal.org/gdal_datamodel.html) and easting / northing the geographic coordinates. You'll also have to specify a projection with -a_srs srs_def
  2. Use the r bindings to GDAL and do the same thing
  3. An easier "GUI" way might be to follow this tutorial and use QGIS with GDAL. The disadvantage of this is you have to set up both QGIS and GDAL if you haven't already.

I would look carefully at the dependencies of each bit of software you need before going down whichever route you choose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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