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

GDAL.gdalwarpappoptionsnew missing "-te"? and "-cutline" issue #115

Closed
Rapsodia86 opened this issue Mar 1, 2021 · 5 comments
Closed

GDAL.gdalwarpappoptionsnew missing "-te"? and "-cutline" issue #115

Rapsodia86 opened this issue Mar 1, 2021 · 5 comments

Comments

@Rapsodia86
Copy link

Hello,
I have been exploring reprojection in Julia.
The simple approach of raster reprojection based on the other raster, it works:
mask= GDAL.gdalopen(mask_dir, GDAL.GA_ReadOnly)
pr_m = GDAL.gdalgetprojectionref(mask)

options = GDAL.gdalwarpappoptionsnew(["-t_srs",pr_m, "-of","GTiff","-tr","1000","1000"], C_NULL)
ds_warped = GDAL.gdalwarp("U:/test5.tif", Ptr{GDAL.GDALDatasetH}(C_NULL), 1, [tif], options, C_NULL)
GDAL.gdalwarpappoptionsfree(options)
GDAL.gdalclose(ds_warped)

However, when I added "-te" and "-te_srs" (https://gdal.org/programs/gdalwarp.html) to have the extent of the smaller raster (from each I took the output projection):
options = GDAL.gdalwarpappoptionsnew(["-t_srs",pr_m, "-of","GTiff","-tr","1000","1000","-te","606520.000 4575750.000 653485.000 4639545.000", "-te_srs",pr_m], C_NULL)
I got an info that :
GDALError (CE_Failure, code 6):
Unknown option name '-te'

Any plans to implement that option?
I have also tried to just use the source raster (that is smaller) to cut the extent:
options = GDAL.gdalwarpappoptionsnew(["-t_srs",pr_m, "-of","GTiff","-tr","1000","1000","-cl",mask,"-crop_to_cutline"], C_NULL)

But I am getting just the original extent of the reprojected raster.

I would appreciate some advice.

Best,
Monika

@visr
Copy link
Member

visr commented Mar 2, 2021

Hmm, I don't fully understand. We automatically support all options that the GDAL build we provide supports. If you run the latest GDAL.jl, that should be GDAL 3.2.1.

I believe the options that you are passing in the vector should all be strings. In the latest example you pass mask to the cutline layer (cl). mask here is a pointer to a dataset. But it expects the name of the layer as a string.

Note by the way that you may find it more convenient to use this: https://yeesian.com/ArchGDAL.jl/latest/reference/#ArchGDAL.unsafe_gdalwarp
(note, you can leave out unsafe_ here, also works.

Or if you want to run the command line utility gdalwarp instead of the function, you could use this:

GDAL.gdalwarp_path() do gdalwarp
    run(`$gdalwarp arg1 arg2`)
end

Then it will be exactly like https://gdal.org/programs/gdalwarp.html

@Rapsodia86
Copy link
Author

Rapsodia86 commented Mar 2, 2021

Ok, I got this.

options = GDAL.gdalwarpappoptionsnew(["-t_srs",pr_m, "-of","GTiff","-tr","1000","1000","-te","606520.000", "4575750.000", "653485.000", "4639545.000", "-te_srs",pr_m], C_NULL)

worked!
So the error [ GDALError (CE_Failure, code 6): Unknown option name '-te'] was misleading (at least for me).

In the second example:

options = GDAL.gdalwarpappoptionsnew(["-t_srs",pr_m, "-of","GTiff","-tr","1000","1000","-cl",mask,"-crop_to_cutline"], C_NULL)

instead of mask, I used a full name with a directory and extension but it did not work (no error, just the original extent).

Btw. I looked thru options in GDAL.jl but I could not find a function that would give me only georeferenced extent [xmin ymin xmax ymax]. So, I have used: gdalinfo to print the band information and took the extent from the text . Any suggestion for a better approach using GDAL.jl (not ArchGDAL)? Unless I have missed appropriate function or option?

Ok, next time I will first check ArchGDAL.jl before asking about GDAL.jl ;)

Thank you very much!

@yeesian
Copy link
Member

yeesian commented Mar 10, 2021

Btw. I looked thru options in GDAL.jl but I could not find a function that would give me only georeferenced extent [xmin ymin xmax ymax].

If you're looking to use ArchGDAL, you can follow yeesian/ArchGDAL.jl#129 (comment). Otherwise, something like the following might work:

ds = GDAL.gdalopenex(filename, GDAL.GDAL_OF_READONLY, C_NULL, C_NULL, C_NULL)

transform = Array{Cdouble}(undef, 6)
result = GDAL.gdalgetgeotransform(dataset, pointer(transform))
xmin = transform[1]
ymax = transform[4]
xmax = xmin + transform[2] * GDAL.gdalgetrasterxsize(ds)
ymin = ymax + transform[6] * GDAL.gdalgetrasterysize(ds)

// [...]
GDAL.gdalclose(ds)

Let us know how it worked out for you?

@Rapsodia86
Copy link
Author

Yes, that worked well! Thank you very much!

@visr
Copy link
Member

visr commented Mar 10, 2021

Great! I'll close the issue then :)

@visr visr closed this as completed Mar 10, 2021
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

3 participants