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

**URL:** https://discourse.julialang.org/t/how-to-extract-the-x-y-coordinated-otf-the-center-of-pixel-200-300-of-a-rasters-raster-object/131936
**Category:** Geo
**Created:** [August 29, 2025, 9:50am UTC](https://discourse.julialang.org/t/how-to-extract-the-x-y-coordinated-otf-the-center-of-pixel-200-300-of-a-rasters-raster-object/131936 "2025-08-29T09:50:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 29, 2025, 9:50am UTC](https://discourse.julialang.org/t/how-to-extract-the-x-y-coordinated-otf-the-center-of-pixel-200-300-of-a-rasters-raster-object/131936/1 "2025-08-29T09:50:38Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [August 29, 2025, 11:02am UTC](https://discourse.julialang.org/t/how-to-extract-the-x-y-coordinated-otf-the-center-of-pixel-200-300-of-a-rasters-raster-object/131936/2 "2025-08-29T11:02:16Z")

</div>

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:

```julia
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):

```julia
centerpoint = DimPoints(maybeshiftlocus(Center(), rast))[(size(rast) .÷ 2)...]

```

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

---

<div class="post-metadata">

### Author: ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)
#### Post date: [August 29, 2025, 5:14pm UTC](https://discourse.julialang.org/t/how-to-extract-the-x-y-coordinated-otf-the-center-of-pixel-200-300-of-a-rasters-raster-object/131936/3 "2025-08-29T17:14:33Z")

</div>

@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?

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [August 30, 2025, 1:25am UTC](https://discourse.julialang.org/t/how-to-extract-the-x-y-coordinated-otf-the-center-of-pixel-200-300-of-a-rasters-raster-object/131936/4 "2025-08-30T01:25:59Z")

</div>

Do other similar packages have a function like that?

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

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

```

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

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 30, 2025, 8:32am UTC](https://discourse.julialang.org/t/how-to-extract-the-x-y-coordinated-otf-the-center-of-pixel-200-300-of-a-rasters-raster-object/131936/5 "2025-08-30T08:32:13Z")

</div>

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](https://discourse.julialang.org/t/how-to-get-value-and-matrix-index-at-a-given-x-y-point/129692/9)). Perhaps an API that facilitates these passages would be useful…

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [August 30, 2025, 10:03am UTC](https://discourse.julialang.org/t/how-to-extract-the-x-y-coordinated-otf-the-center-of-pixel-200-300-of-a-rasters-raster-object/131936/6 "2025-08-30T10:03:42Z")

</div>

I think you are talking about the Matlab’s [axes2pix](https://www.mathworks.com/help/images/ref/axes2pix.html) and pix2axes pair. GMT has them as well, though I never needed them as a user. Only for code developing.
