Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getindex fails trying to retrieve 2D coordinates of a Raster #342

Closed
luraess opened this issue Nov 30, 2022 · 5 comments
Closed

getindex fails trying to retrieve 2D coordinates of a Raster #342

luraess opened this issue Nov 30, 2022 · 5 comments

Comments

@luraess
Copy link

luraess commented Nov 30, 2022

Extracting 2D x and y coordinate arrays of a Raster fails when using getindex. This MWE was previously (Summer 2022) working:

using Rasters
url = "https://www.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc";
filename = download(url, "tos_O1_2001-2002.nc");
A = Raster(filename)
coords = reverse(A, dims=2)
(x,y)  = (getindex.(coords,1), getindex.(coords,2))

Maybe there is a new API for this?

@rafaqz
Copy link
Owner

rafaqz commented Nov 30, 2022

Your coords are just the values of the array with the second dimension reversed?

How can you index with that?

Best to clearly state what you're trying to do...

@luraess
Copy link
Author

luraess commented Dec 1, 2022

I would like to retrieve the 2D arrays of x and y coordinates corresponding to the A raster data (a separate one for x-coords and y-coords).

One way to do it would be as follow but I guess there is certainly more optimal way of doing so

using Rasters
url = "https://www.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc"
filename = download(url)
A = Raster(filename)
x = A[1].val.data
y = A[2].val.data
x2d = x +. 0 .* y'
y2d = y +. 0 .* x'

EDIT: This MWE is not working. See one post below for the intended MWE (and not MNWE).

@rafaqz
Copy link
Owner

rafaqz commented Dec 1, 2022

Your example above doesn't actually work. It is important when making issues to run your code and provide a working MWE, otherwise I don't really know what result you want.

But I think DimensionalData.DimPoints might be it:

julia> DimPoints(dims(A, (X, Y)))
180×170 DimPoints{Tuple{Float64, Float64}, 2, Tuple{X{Projected{Float64, LinRange{Float64, Int64}, ForwardOrdered, Regular{Float64}, Intervals{Start}, Metadata{Rasters.GDALfile, Dic
t{String, Any}}, WellKnownText{GeoFormatTypes.CRS}, Nothing, X{Colon}}}, Y{Projected{Float64, LinRange{Float64, Int64}, ReverseOrdered, Regular{Float64}, Intervals{Start}, Metadata{
Rasters.GDALfile, Dict{String, Any}}, WellKnownText{GeoFormatTypes.CRS}, Nothing, Y{Colon}}}}, Tuple{X{Colon}, Y{Colon}}}:
 (0.0, 89.0)    (0.0, 88.0)    (0.0, 87.0)    (0.0, 86.0)    (0.0, 85.0)    (0.0, 84.0)      (0.0, -76.0)    (0.0, -77.0)    (0.0, -78.0)    (0.0, -79.0)    (0.0, -80.0)
 (2.0, 89.0)    (2.0, 88.0)    (2.0, 87.0)    (2.0, 86.0)    (2.0, 85.0)    (2.0, 84.0)       (2.0, -76.0)    (2.0, -77.0)    (2.0, -78.0)    (2.0, -79.0)    (2.0, -80.0)
 (4.0, 89.0)    (4.0, 88.0)    (4.0, 87.0)    (4.0, 86.0)    (4.0, 85.0)    (4.0, 84.0)       (4.0, -76.0)    (4.0, -77.0)    (4.0, -78.0)    (4.0, -79.0)    (4.0, -80.0)
 (6.0, 89.0)    (6.0, 88.0)    (6.0, 87.0)    (6.0, 86.0)    (6.0, 85.0)    (6.0, 84.0)       (6.0, -76.0)    (6.0, -77.0)    (6.0, -78.0)    (6.0, -79.0)    (6.0, -80.0)
 (8.0, 89.0)    (8.0, 88.0)    (8.0, 87.0)    (8.0, 86.0)    (8.0, 85.0)    (8.0, 84.0)       (8.0, -76.0)    (8.0, -77.0)    (8.0, -78.0)    (8.0, -79.0)    (8.0, -80.0)
 (10.0, 89.0)   (10.0, 88.0)   (10.0, 87.0)   (10.0, 86.0)   (10.0, 85.0)   (10.0, 84.0)     (10.0, -76.0)   (10.0, -77.0)   (10.0, -78.0)   (10.0, -79.0)   (10.0, -80.0)
 (12.0, 89.0)   (12.0, 88.0)   (12.0, 87.0)   (12.0, 86.0)   (12.0, 85.0)   (12.0, 84.0)      (12.0, -76.0)   (12.0, -77.0)   (12.0, -78.0)   (12.0, -79.0)   (12.0, -80.0)
 (14.0, 89.0)   (14.0, 88.0)   (14.0, 87.0)   (14.0, 86.0)   (14.0, 85.0)   (14.0, 84.0)      (14.0, -76.0)   (14.0, -77.0)   (14.0, -78.0)   (14.0, -79.0)   (14.0, -80.0)
 (16.0, 89.0)   (16.0, 88.0)   (16.0, 87.0)   (16.0, 86.0)   (16.0, 85.0)   (16.0, 84.0)      (16.0, -76.0)   (16.0, -77.0)   (16.0, -78.0)   (16.0, -79.0)   (16.0, -80.0)
 (18.0, 89.0)   (18.0, 88.0)   (18.0, 87.0)   (18.0, 86.0)   (18.0, 85.0)   (18.0, 84.0)      (18.0, -76.0)   (18.0, -77.0)   (18.0, -78.0)   (18.0, -79.0)   (18.0, -80.0)
 (20.0, 89.0)   (20.0, 88.0)   (20.0, 87.0)   (20.0, 86.0)   (20.0, 85.0)   (20.0, 84.0)     (20.0, -76.0)   (20.0, -77.0)   (20.0, -78.0)   (20.0, -79.0)   (20.0, -80.0)
                                                                                           
 (340.0, 89.0)  (340.0, 88.0)  (340.0, 87.0)  (340.0, 86.0)  (340.0, 85.0)  (340.0, 84.0)    (340.0, -76.0)  (340.0, -77.0)  (340.0, -78.0)  (340.0, -79.0)  (340.0, -80.0)
 (342.0, 89.0)  (342.0, 88.0)  (342.0, 87.0)  (342.0, 86.0)  (342.0, 85.0)  (342.0, 84.0)     (342.0, -76.0)  (342.0, -77.0)  (342.0, -78.0)  (342.0, -79.0)  (342.0, -80.0)
 (344.0, 89.0)  (344.0, 88.0)  (344.0, 87.0)  (344.0, 86.0)  (344.0, 85.0)  (344.0, 84.0)     (344.0, -76.0)  (344.0, -77.0)  (344.0, -78.0)  (344.0, -79.0)  (344.0, -80.0)
 (346.0, 89.0)  (346.0, 88.0)  (346.0, 87.0)  (346.0, 86.0)  (346.0, 85.0)  (346.0, 84.0)     (346.0, -76.0)  (346.0, -77.0)  (346.0, -78.0)  (346.0, -79.0)  (346.0, -80.0)
 (348.0, 89.0)  (348.0, 88.0)  (348.0, 87.0)  (348.0, 86.0)  (348.0, 85.0)  (348.0, 84.0)     (348.0, -76.0)  (348.0, -77.0)  (348.0, -78.0)  (348.0, -79.0)  (348.0, -80.0)
 (350.0, 89.0)  (350.0, 88.0)  (350.0, 87.0)  (350.0, 86.0)  (350.0, 85.0)  (350.0, 84.0)    (350.0, -76.0)  (350.0, -77.0)  (350.0, -78.0)  (350.0, -79.0)  (350.0, -80.0)
 (352.0, 89.0)  (352.0, 88.0)  (352.0, 87.0)  (352.0, 86.0)  (352.0, 85.0)  (352.0, 84.0)     (352.0, -76.0)  (352.0, -77.0)  (352.0, -78.0)  (352.0, -79.0)  (352.0, -80.0)
 (354.0, 89.0)  (354.0, 88.0)  (354.0, 87.0)  (354.0, 86.0)  (354.0, 85.0)  (354.0, 84.0)     (354.0, -76.0)  (354.0, -77.0)  (354.0, -78.0)  (354.0, -79.0)  (354.0, -80.0)
 (356.0, 89.0)  (356.0, 88.0)  (356.0, 87.0)  (356.0, 86.0)  (356.0, 85.0)  (356.0, 84.0)     (356.0, -76.0)  (356.0, -77.0)  (356.0, -78.0)  (356.0, -79.0)  (356.0, -80.0)
 (358.0, 89.0)  (358.0, 88.0)  (358.0, 87.0)  (358.0, 86.0)  (358.0, 85.0)  (358.0, 84.0)     (358.0, -76.0)  (358.0, -77.0)  (358.0, -78.0)  (358.0, -79.0)  (358.0, -80.0)

If you want separate matrices for each you can just extract xs = first.(x) and ys = last.(x)

@luraess
Copy link
Author

luraess commented Dec 1, 2022

Thanks for your feedback.

Your example above doesn't actually work

Indeed, I somehow posted the not working test. Here what was meant to be the MWE:

using Rasters
url = "https://www.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc"
filename = download(url)
A = Raster(filename)
B = A[1:10,1:10,1]
x = B.dims[1].val.data
y = B.dims[2].val.data
x2d = x .+ 0 .* y'
y2d = y .+ 0 .* x'

If you want separate matrices for each you can just extract xs = first.(x) and ys = last.(x)

Thanks, this does the trick

coord = DimPoints(dims(A, (X, Y)))
x2 = first.(coord)
y2 = last.(coord)

@luraess
Copy link
Author

luraess commented Dec 1, 2022

All works - thanks for your help!

@luraess luraess closed this as completed Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants