Rasterio: Presenters: Sushma Ghimire (13) Ashmin Sharma Pokharel (19) Asim Shrestha
Rasterio: Presenters: Sushma Ghimire (13) Ashmin Sharma Pokharel (19) Asim Shrestha
PRESENTERS:
SUSHMA GHIMIRE (13)
ASHMIN SHARMA POKHAREL (19)
ASIM SHRESTHA (23)
INTRODUCTION
• Before Rasterio, Geospatial Data Abstraction Library, GDAL was used for accessing the many
different kind of raster data files used in the GIS field.
• Python programs using GDAL tend to read and run like C programs.
• This is bad: among other considerations we’ve chosen Python instead of C to avoid problems with
pointers.
INTRODUCTION
• Rasterio’s goal is to be the kind of raster data library
– expressing GDAL’s data model using fewer non-idiomatic extension classes
– more idiomatic Python types and protocols,
– performing as fast as GDAL’s Python bindings
• High performance, lower cognitive load, cleaner and more transparent code
INSTALLATION
• Rasterio has one C-library dependency: (GDAL>=1.11)
• To install rasterio, download both binary wheels (rasterio and GDAL) and run something like this from
the downloads folder:
pip install GDAL-2.2.4-cp27-cp27m-win_amd64.whl
pip install rasterio-1.0.28-cp27-cp27m-win_amd64.whl
OPENING A DATASET IN READING MODE
• Dataset attributes
Properties of the raster data can be accessed through attributes of the opened dataset object.
>>dataset.count
>>dataset.name
>>dataset.mode
>>dataset.closed
>>dataset.width
>>dataset.height
READING RASTER DATA
• Data from a raster band can be accessed by the band’s index number.
Affine(a,b,c,d,e,f)
where:
a = width of a pixel
b = row rotation (typically zero)
c = x-coordinate of the upper-left corner of the upper-left pixel
d = column rotation (typically zero)
e = height of a pixel (typically negative)
f = y-coordinate of the of the upper-left corner of the upper-left pixel
SPATIAL INDEXING
• Datasets have an index() method for getting the array indices corresponding to points in georeferenced space.
>>>dataset.xy(dataset.height//2,dataset.width//2)
OPENING A DATASET IN WRITING MODE
• Call rasterio.open() with a path to the new file to be created, 'w' to specify writing mode, and several
keyword arguments.
driver: the name of the desired format driver
width: the number of columns of the dataset
height: the number of rows of the dataset
count: a count of the dataset bands
dtype: the data type of the dataset
• while image processing software like scikit-image, pillow and matplotlib are generally ordered:
(rows, columns, bands)
• The number of rows defines the dataset’s height, the columns are the dataset’s width.
• Numpy provides a way to efficiently swap the axis order and you can use the following reshape
functions to convert between raster and image axis order:
INTEROPERABILITY
PLOTTING
• Rasterio provides rasterio.plot.show() to perform common tasks such as displaying multi-band images
as RGB and labeling the axes with proper geo-referenced extents.
>>>from rasterio.plot import show
>>> show ( (dataset,1) ,title='map',with_bounds=True,cmap='Reds',ax=None)
PLOTTING
PLOTTING
• Subplots
PLOTTING
PLOTTING
• Histogram
Rasterio also provides a show_hist() function for generating histograms of single or multiband rasters.
MASKING A RASTER USING A SHAPEFILE
• Using rasterio with fiona, it is simple to open a shapefile, read geometries, and mask out regions of a
raster that are outside the polygons defined in the shapefile.
• Applying the features in the shapefile as a mask on the raster sets all pixels outside of the features to be
zero.
• crop=True in given example, the extent of the raster is also set to be the extent of the features in the
shapefile.
• We can then use the updated spatial transform and raster height and width to write the masked raster
to a new file.
MASKING A RASTER USING A SHAPEFILE
MASKING A RASTER USING A SHAPEFILE
SOME COMMANDS USED IN RASTERIO
Rasterio’s command line interface (CLI) is a program named “rio”
• Bounds:
>>>rio bounds input.tif –indent 2
• Info
The info command prints structured information about a dataset
>>>rio info tests/data/RGB.byte.tif --indent 2
More information, such as band statistics, can be had using the --verbose option.
>>>rio info tests/data/RGB.byte.tif --indent 2 –verbose
SOME COMMANDS USED IN RASTERIO
• Clip
The clip command clips a raster using bounds input directly or from a template raster.
>>>rio clip input.tif output.tif –like template.tif
• Mask
The mask command masks in pixels from all bands of a raster using features (masking out all areas not
covered by features) and optionally crops the output raster to the extent of the features.
>>>rio mask input.tif output.tif --geojson-mask input.geojson
>>>rio mask input.tif output.tif --invert --geojson-mask input.geojson