How to extract the x,y coordinated otf the center of pixel 200, 300 of a Rasters.Raster object?

Provided I have a Rasters.Raster object foo, how can I extract the coordinated at the centre of a given pixel, in the raster coordinates system ?

Getting the center isn’t that straightforward as e.g. GDAL gives values in the corners. So you need to shift them.

You could manually do this:

using Rasters.Lookups 
x, y = lookup(maybeshiftlocus(Center(), rast), (X, Y))
centerpoint = x[size(rast, X) Ă· 2], x[size(rast, Y) Ă· 2]

Or use DimPoints from DimensionalData which is like a lazy array over all the points made from the lookup values (coordinates in a spatial context):

centerpoint = DimPoints(maybeshiftlocus(Center(), rast))[(size(rast) .Ă· 2)...]

(maybe adding a locus keyword to DimPoints would make this even cleaner)

1 Like

@Raf these solutions look very convoluted for what I would consider a common operation (i.e. requesting grid centroids). Is there an argument to be made for a gridcentroid function?

Do other similar packages have a function like that?

Do you mean just a wrapper that gives you a DimPoints objects?

centroids(rast::Raster) = DimPoints(maybeshiftlocus(Center(), dims(rast, (X(), Y())))

Feel free to write/document/PR that if you think so

In general, as a “casual” gis user, I find very useful in this package the “raster as a matrix” concept, it facilitates a lot the raster treatment, but I find complicated to pass from the (xid,yid) space to the coordinates space and vice versa (see also my other post). Perhaps an API that facilitates these passages would be useful…

I think you are talking about the Matlab’s axes2pix and pix2axes pair. GMT has them as well, though I never needed them as a user. Only for code developing.