GDAL Datasets

GDAL Datasets

The following code demonstrates the general workflow for reading in a dataset:

ArchGDAL.registerdrivers() do
    ArchGDAL.read(filename) do dataset
        # work with dataset
    end
end

We defer the discussions on ArchGDAL.registerdrivers() and ArchGDAL.read(filename) to the sections on Driver Management and Working with Files respectively.

Note

In this case, a handle to the dataset is obtained, and no further data was requested. It is only when we run print(dataset) that calls will be made through GDAL's C API to obtain information about dataset for display.

Vector Datasets

In this section, we work with the data/point.geojson dataset via

ArchGDAL.registerdrivers() do
    ArchGDAL.read(filepath) do dataset
        print(dataset)
    end
end

The display indicates

You can also programmatically retrieve them using

For more on working with features and vector data, see the Section on Feature Data.

Raster Datasets

In this section, we work with the gdalworkshop/world.tif dataset:

ArchGDAL.registerdrivers() do
    ArchGDAL.read(filepath) do dataset
        print(dataset)
    end
end

The display indicates

You can also programmatically retrieve them using

For more on working with raster data, see the Section on Raster Data.

Driver Management

Before opening a GDAL supported datastore it is necessary to register drivers. Normally this is accomplished with the GDAL.allregister() function which registers all known drivers. However, the user will then need to remember to de-register the drivers using GDAL.destroydrivermanager() when they're done.

In ArchGDAL, we provide a registerdrivers() block, such that

ArchGDAL.registerdrivers() do
    # your code here
end

is equivalent to

GDAL.allregister()
# your code here
GDAL.destroydrivermanager()

Working with Files

We provide the following methods for working with files:

For each one of them, we will call ArchGDAL.destroy at the end of the do-block which will dispatch to the corresponding GDAL method. For example,

ArchGDAL.read(filename) do dataset
    # work with dataset
end

will correspond to

dataset = ArchGDAL.unsafe_read(filename)
# work with dataset
ArchGDAL.destroy(dataset) # the equivalent of GDAL.close(dataset.ptr)
Note

In GDAL, datasets are closed by calling GDAL.close(). This will result in proper cleanup, and flushing of any pending writes. Forgetting to call GDAL.close() on a dataset opened in update mode in a popular format like GTiff will likely result in being unable to open it afterwards.

Note

This pattern of using do-blocks to manage context plays a big way into the way we handle memory in this package. For details, see the section on Memory Management.