Skip to content

Generate a coordinate (latitude) image?

5 messages · Jonathan Greenberg, Manuel Schneider, Michael Denslow +2 more

#
Folks:

Is there any way to take a raster and generate two outputs where
each pixel is the center coordinate of that pixel (e.g. you will have
one x- image and one y-image)?  I'm trying to produce a "latitude map"
for solar calculations.

--j
#
Jonathan Greenberg <greenberg <at> ucdavis.edu> writes:
You could use coordinates(as(obj, "SpatialPixels")) but only for relatively
small rasters. And why not calculate by hand?
#
On Tue, May 17, 2011 at 7:25 PM, Jonathan Greenberg
<greenberg at ucdavis.edu> wrote:
If you are using the spgrass6 library you should be able to use the
y() argument in the r.mapcalc command from GRASS. I say should because
I have only done this in GRASS directly not using spgrass6.

Hope this helps,
Michael

  
    
#
On 18/05/2011 1:25, Jonathan Greenberg wrote:
If you mean "each pixel value  is the center coordinate of that pixel" 
you could proceed like this:

# (first, I built a raster for my toy example)
library(maptools)
data(meuse.grid) # only the non-missing valued cells
coordinates(meuse.grid) = c("x", "y") # promote to SpatialPointsDataFrame
gridded(meuse.grid) <- TRUE # promote to SpatialPixelsDataFrame

# now, assign the coordinates as values to each pixel
meuse.grid[["xcoord"]] = coordinates(meuse.grid)[,"x"]
meuse.grid[["ycoord"]] = coordinates(meuse.grid)[,"y"]

image(meuse.grid["xcoord"]) # note the single [
image(meuse.grid["ycoord"])

Cheers,
  Marcelino

  
    
#
Jonathan, 

If your data are in lon/lat

r <- raster()
x <- init(r, v='x')
y <- init(r, v='y')


Or "by hand", for lon/lat do

lat <- yFromRow(r, 1:nrow(r))

if the raster is not lon/lat

xy <- SpatialPoints(xyFromCell(r, cellFromRowCol(1:nrow(r), 1)))
lat <- coordinates( spTransform(xy, CRS( +proj=longlat +datum=WGS84 )))[,2] 

In cases like this, I stop here. Rather than making a latitude raster with
lots of repeated values, I would work with the lat vector. But you could do
something like this:

x <- t((t(r) * lat))
plot(x)


Best, Robert
 

--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Generate-a-coordinate-latitude-image-tp6375697p6378473.html
Sent from the R-sig-geo mailing list archive at Nabble.com.