How can I rasterize the polygons of a Shapefile in Julia?

To follow-up from @visr’s example, the following should work if you have GDAL.jl and ArchGDAL.jl installed:

import GDAL
using ArchGDAL; const AG = ArchGDAL

AG.registerdrivers() do
    AG.read("/vsizip/gadm36_ARG_shp.zip/gadm36_ARG_1.shp") do source_ds
        GDAL.close(GDAL.rasterize(
            "gadm36_ARG_1.tif",
            Ptr{GDAL.GDALDatasetH}(C_NULL),
            source_ds.ptr,
            GDAL.rasterizeoptionsnew([
                # "-of", "GTiff",         # The format/driver. Inferred from filename here.
                # "-l", "gadm36_ARG_1",   # The layer(s) used for input features.
                "-ts", "250", "250",      # target size: "-ts xsize ysize". Set it to whatever you want.
                "-ot", "Byte"
            ], C_NULL), C_NULL))
    end
end

I have left some of the optional arguments as comments in the example above. Feel free to explore the other options that you can pass to GDAL.rasterizeoptionsnew() in the format above.

I have also opened an issue to provide better support (and eventually documentation) for it in ArchGDAL.jl.

3 Likes