Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
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 Apr 19 '12 at 16:33
How have you made the generated raster image? Is it coming from another application, or made within R? –  Simbamangu Apr 20 '12 at 6:47
It's generated within R using the image() function. –  Mimo Apr 20 '12 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 Sep 19 '13 at 17:11
add comment

migrated from stats.stackexchange.com Apr 19 '12 at 22:44

This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.

2 Answers

up vote 6 down vote accepted

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")

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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