0% found this document useful (0 votes)
212 views22 pages

Rasterio: Presenters: Sushma Ghimire (13) Ashmin Sharma Pokharel (19) Asim Shrestha

Rasterio is a Python library that provides a high-level interface to geographic raster data. It aims to improve upon GDAL by expressing its data model in more idiomatic Python types and protocols while maintaining high performance. Rasterio allows opening and reading raster files, accessing metadata and pixel values, writing raster data, masking rasters using shapefiles, and visualizing rasters. It also includes command line tools like rio for viewing metadata and clipping/masking rasters.

Uploaded by

Sarthak Regmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
212 views22 pages

Rasterio: Presenters: Sushma Ghimire (13) Ashmin Sharma Pokharel (19) Asim Shrestha

Rasterio is a Python library that provides a high-level interface to geographic raster data. It aims to improve upon GDAL by expressing its data model in more idiomatic Python types and protocols while maintaining high performance. Rasterio allows opening and reading raster files, accessing metadata and pixel values, writing raster data, masking rasters using shapefiles, and visualizing rasters. It also includes command line tools like rio for viewing metadata and clipping/masking rasters.

Uploaded by

Sarthak Regmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

RASTERIO

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.

• Requires to watch out for dangling C pointers, potential crashers of 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

Import rasterio to begin.


>>>import rasterio
Next, open the file.
>>>dataset= rasterio.open(‘example.tif’)

• 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.

• Bands are indexed from 1(GDAL convention)


>>>dataset.indexes
>>>band1=dataset.read(1)
>>>band1
>>>band1[dataset.height//3,dataset.width]
READING RASTER DATA

Coordinate reference System


>>dataset.crs
>>dataset.bounds
>>dataset.transform
>>dataset.transform*(row,col)

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.

>>> xa, y = (dataset.bounds.left + 100000, dataset.bounds.top - 50000)

>>> row, col = dataset.index(x, y)

>>> row, col

• To get spatial coordinate of pixel, use the dataset’s xy() method.

>>>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

crs: a coordinate reference system identifier or description


transform: an affine transformation matrix, and
nodata: a “nodata” value
The first 5 of these keyword arguments parametrize fixed, format-specific properties of the data file and
are required when opening a file to write. The last 3 are optional.
OPENING A DATASET IN WRITING MODE AND SAVING
INTEROPERABILITY
• Some python image processing software packages organize arrays differently than rasterio. The
interpretation of a 3-dimension array read from rasterio is:
(bands, rows, columns)

• 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy