# (help request) using ArchGDAL.gdalwarp() for resampling a raster dataset

**URL:** <https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039>\
**Category:** Geo\
**Tags:** question, geo\
**Created:** [September 21, 2020, 8:51pm UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039 "2020-09-21T20:51:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vlandau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlandau/32/8986_2.png) [@vlandau](https://discourse.julialang.org/u/vlandau)\
**Post date:** [September 21, 2020, 8:51pm UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/1 "2020-09-21T20:51:05Z")

</div>

Hi all, I’m struggling to figure out how to use ArchGDAL.gdalwarp (currently only unsafe\_gdalwarp is documented). ArchGDAL.gdalwarp’s first parameter being a function is a bit confusing as well.

Here’s the GDAL CLI version of what I’m trying to implement in ArchGDAL (params I already have in Julia are represented by the all caps strings):

```julia
gdalwarp -t_srs WKT_STRING -te LEFT_EXTENT LOWER_EXTENT RIGHT_EXTENT \
    UPPER_EXTENT -te_srs WKT_STRING -tr X_RES Y_RES -r cubic \
    GDAL_DATASET output_dataset

```

I’m also hoping to return an ArchGDAL.Dataset if possible (without writing anything to disk).

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![vlandau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlandau/32/8986_2.png) [@vlandau](https://discourse.julialang.org/u/vlandau)\
**Post date:** [September 21, 2020, 9:28pm UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/2 "2020-09-21T21:28:17Z")

</div>

Okay! I think I figured it out…?

```julia
ds = ArchGDAL.read(path) do source
    ArchGDAL.gdalwarp([source], ["-t_srs", "$(wkt)",
                               "-te", "$(west_bound)", "$(south_bound)",
                               "$(east_bound)", "$(north_bound)",
                               "-te_srs", "$(wkt)",
                               "-tr", "$(step(x_dim))", "$(step(y_dim))",
                               "-r", "cubic"]) do warped
        band = ArchGDAL.getband(warped, 1)

        ArchGDAL.read(band)
    end
end

```

---

<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:** [October 1, 2020, 3:07am UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/3 "2020-10-01T03:07:35Z")

</div>

We may as well wrap this in GeoData.jl, that could be a one liner

---

<div class="post-metadata">

**Author:** ![vlandau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlandau/32/8986_2.png) [@vlandau](https://discourse.julialang.org/u/vlandau)\
**Post date:** [October 1, 2020, 3:33am UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/4 "2020-10-01T03:33:15Z")

</div>

I can do a PR if you’d like. Feel free to create an issue in GeoData.jl and tag me! Can probably get to that next week. Also feel free to copy/paste the above if you want to implement it.

---

<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:** [October 1, 2020, 5:17am UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/5 "2020-10-01T05:17:28Z")

</div>

Done: [https://github.com/rafaqz/GeoData.jl/issues/68](https://github.com/rafaqz/GeoData.jl/issues/68)

---

<div class="post-metadata">

**Author:** ![stefanomoro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefanomoro/32/47473_2.png) [@stefanomoro](https://discourse.julialang.org/u/stefanomoro)\
**Post date:** [March 9, 2023, 6:55pm UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/6 "2023-03-09T18:55:54Z")

</div>

I have a similar issue trying to resample with `gdalwarp().` I want to write a function to save both the warped dataset and the Matrix of the band for processing. I need the dataset to use the `getgeotransform(dataset)` and `getproj(dataset)` methods to retrieve the x/y from the row/col of the pixel.

From my understanding, it seems like `gdalwarp()` calls the `unsafe_gdalwarp()` method and then destroys the warped dataset. This is what I tried, but the returned dataset is `NULL`

```julia
function reproject_dem(param, output_bounds)
    dst_cs = AG.importEPSG(point2epsg(param.lon, param.lat)) # UTM projected coordinate system
    AG.read(param.dem_file) do source 
        AG.gdalwarp([source], ["-t_srs", "$(AG.toWKT(dst_cs))",
                        "-te",string.(output_bounds)...,
                        "-te_srs", "$(AG.toWKT(dst_cs))",
                        "-tr","$(param.pixel_spacing_in_m)","$(param.pixel_spacing_in_m)",
                        "-r", "cubic"]) do warped
            warped,
            AG.read(AG.getband(warped, 1))
        end
    end
end

```

Thanks for any help!

---

<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:** [March 9, 2023, 7:17pm UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/7 "2023-03-09T19:17:55Z")

</div>

Rasters.jl wraps AG.gdalwarp for easy warping and resampling. The `resample` method is pretty straightforward compared to this.

---

<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:** [March 9, 2023, 7:31pm UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/8 "2023-03-09T19:31:30Z")

</div>

And with GMT you can do things like

```julia
using GMT

julia> G = GMT.peaks();

julia> grdinfo(G)
11-element Vector{String}:
 ": Title: "
 ": Command: "
 ": Remark: "
 ": Gridline node registration used [Cartesian grid]"
 ": Grid file format: nf = GMT netCDF format (32-bit float), CF-1.7"
 ": x_min: -3 x_max: 3 x_inc: 0.125 name: x n_columns: 49"
 ": y_min: -3 y_max: 3 y_inc: 0.125 name: y n_rows: 49"
 ": v_min: -6.54166173935 v_max: 8.08391857147 name: z"
 ": scale_factor: 1 add_offset: 0"
 ": format: classic"
 ": Default CPT: "

# Reinterpolate to 0.1
julia> G2 = gdalwarp(G, I=0.1);

julia> grdinfo(G2)
11-element Vector{String}:
 ": Title: "
 ": Command: "
 ": Remark: "
 ": Gridline node registration used [Cartesian grid]"
 ": Grid file format: nf = GMT netCDF format (32-bit float), CF-1.7"
 ": x_min: -3.0125 x_max: 2.9875 x_inc: 0.1 name: x n_columns: 61"
 ": y_min: -2.9875 y_max: 3.0125 y_inc: 0.1 name: y n_rows: 61"
 ": v_min: -6.54166173935 v_max: 8.08391857147 name: z"
 ": scale_factor: 1 add_offset: 0"
 ": format: classic"
 ": Default CPT: "

```

---

<div class="post-metadata">

**Author:** ![stefanomoro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefanomoro/32/47473_2.png) [@stefanomoro](https://discourse.julialang.org/u/stefanomoro)\
**Post date:** [March 10, 2023, 7:24pm UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/9 "2023-03-10T19:24:21Z")

</div>

Thank both of you for the replies!

I ended up using `Rasters.warp()` because I have to limit the output with a set of bounds defined in the destination CRS, and it seems not possible with `Rasters.resample()`

---

<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:** [March 10, 2023, 9:00pm UTC](https://discourse.julialang.org/t/help-request-using-archgdal-gdalwarp-for-resampling-a-raster-dataset/47039/10 "2023-03-10T21:00:08Z")

</div>

If you have time please make a github issue spelling out what should happen and why it doesn’t and I’ll fix it. `resample` should be able to handle this.
