100% found this document useful (1 vote)
31 views5 pages

Chapter 1 Exploring Rasterio

Uploaded by

Ha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
100% found this document useful (1 vote)
31 views5 pages

Chapter 1 Exploring Rasterio

Uploaded by

Ha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Patrick Gray ([Link] at duke) - [Link] Chapter 1: Exploring rasterio Introduction GDAL - the Geospatial Data Abstraction Library isa software library for reading and writing raster and vector geospatial data formats and forms the basis of most software for processing geospatial data. There are many formats for using GDAL ranging from graphical tools like ArcGIS or QGIS te command line GDAL tools but here we're using the fantastic rasterio python package which provides a pythonic wrapping around GDAL. Basically it reads and writes geospatial formats anc provides a Python API based on numpy N-dimensional arrays and GeoJSON f you're coming from another language and want an overview of object oriented programming in Python, see the python like you meant it short online course Module import in Python Before we can get started, we need to tell Python that we will be using functions, classes, and variables from some packages. The technical wording for this is that we need to import these modules into our namespace (see Python's documentation on the module system here). We will do this using some import statements import rasterio # import the main rasterio function import matplotlib # matplotlib is the primary python plotting and viz Library # this bit of magic allows matplotlib to plot inline in a jupyter notebook Xmatplotlib inline "_version_" # We can check which version we're running by printing the variable print("rasterio's version is: " + rasterio.__version_) print(rasterio) rasterio's version is: 1.2.3 Once we import these packages Python will know where to look on our system for the code that implements them, When we want to access classes, variables, or functions within these packages, we will need to reference the full path (e.g, [Link]() } Examples Open an image When we open an image in rasterio we create a Dataset object. As the name would suggest, we car open an image with the “open’ function within rasterio . We will use an example image provided in the data directory for this chapter. This image is a subset ofa Landsat 7 image containing the 8 bands on this sensor rearranged in order of wavelength (e.g, Landsat 7's second SWIR channel comes before thermal channel in our stack). The last band in this image is a cloud and cloud shadow mask from Fmask # filepath to our image img_fp = ‘../data/LE7@220492002106EDC00_stack.gtif # Open a geospatial dataset dataset = [Link](img_ fp) print (dataset) Now that we have this dataset open, let's explore some of its capabilities Image attributes # what is the nane of this image img_nane = dataset .nane print(‘Inage filename: {n}\n' .fornat(n=ing_name)) # How many bands does this image have? nun_bands = [Link] print(‘Nunber of bands in image: {n}\n‘ -format(nenum_bands)) # How many rows and columns? rows, cols = [Link] print("Image size is: {r} rows x {c} colunns\n'.format(r=rows, c=cols)) # Does the raster have a description or metadata? desc = [Link] metadata = [Link] print(*Raster description: {desc}\n' . format (desc=desc)) # What driver was used to open the raster? driver = [Link] print(‘Raster driver: {d)\n" .format (d=driver)) # hat is the raster's projection? proj = [Link] print('Inage projection print(proj, ‘\n') # what is the raster's "geo-transform" gt = [Link] print('Inage geo-transform:\n(gt}\n' .fornat(gt=gt)) print(‘All raster metadata:') print (metadata) print(*\n") Image filename: ... /data/LE70220492¢02106EDCa0_stack.gtif Number of bands in image: 8 Image size is: 25@ rows x 25@ columns Raster description: (‘band 1 reflectance’, ‘band 2 reflectance’, ‘band 3 reflectance’, "band 4 reflectance’, ‘band 5 reflectance’, ‘band 7 reflectance’, ‘band 6 temperature’, “Band 8") Raster driver: Gift Image projection: £PSG:32615 Image geo-transform: | 3.08, 0.08, 462405.00 | @.00,-30.00, 1741815.00] | @.00, 0.08, 1.00 All raster metadata: {‘driver’: ‘Giff, ‘dtype’: ‘int16', ‘nodata': None, ‘width': 258, ‘height’: 258, ‘coun t': 8, ‘ers*: CRS.from_epsg(32615), ‘transform’: AfFine(30.0, 0.0, 462405.0, 0.0, -30., 1741815.0)} The first few pieces of information we obtained are fairly straightforward - image name, the raster size, the number of bands, a description, some metadata, and the rasters file format. The image's projection is formatted in what's known as “Well Known Text". For more information or specific projections and for format conversions among projection description formats (e.g, proja string, WKT, ESRI WKT, JSON, etc.) see Spatial Reference, The last piece of information we accessed is something called a "geotransform'. This set of 6 umbers provides all the information required to and from transform pixel and projected coordinates. In this example, the first number (462405) and the fourth number (1741815) are the top left of the upper left pixel of the raster. The pixel size in x and y dimensions of the raster is listed as the second (30) and the sixth (-30) numbers, Since our raster is north up oriented, the third and fifth numbers are 0. For more information on the GDAL data model, visit this web page. Image raster bands now for the fun part, actually visualizing and working the data The rasterio Dataset object we created contains a lot of useful information but itis not directly usec to read in the raster image. Instead we will need to access the raster’s bands using the read() method: # Open the fourth band in our image - NIR here nir = [Link](4) [Link] # check out the dimensions of the image (25, 25@) When we load our raster band into memory we will read it into a NumPy 2 dimensional array. Numby is, "the fundamental package for scientific computing with Python", because it allows us to represent our data in a very memory efficient way. NumPy arrays are the comerstone or building block of the rest of the Scientific Python suite of software. Get familiar with them: © NumPy for MATLAB users © NumPy tutorial © NumPy API reference manual Just as we made the routines and data types from rasterio available to us using import , we loadec up NumPy. When we import NumPy, we also gave it an alias so that we don't have to type numpy every time we want to use it # No alias import numpy print(numpy.__version_) # Alias or rename to "np" -- a very common practice import numpy as np print(np.__version__) 1.20.2 2 # What are the band's datatypes? datatype = [Link] print (‘Band datatypes: {dt}' .fornat(dt=datatype)) # How about some band statistics? band_mean = [Link](nir) band_nin = [Link](nir) band_max = [Link](nir) band_stddev = [Link](nir) print ("Band range: {minimum} - {naximun}’.format(maximun=band_max, ‘minimun=band_min)) print(‘Band mean, stddev: {m}, {s}\n'.format(msband_mean, s=band_stddev)) Band datatypes: (‘inti6", ‘inti6’, ‘inti6", ‘inti’, ‘inti6', ‘inti6", ‘intt6’, ‘inti6") Band range: 1007 - 8178 Band mean, stddev: 2700.18344, 710.129602141585S The method sead() takes arguments that allow us to specify a subset of the raster bands, specific X and Y offsets and sizes of the bands and much more. Remember this ability when you want to process large images or are working with a limited amount of memory. In these circumstances, you will run out of memory if you read the entire dataset in at once. Instead, read in a block of some umber of columns and rows at one time, perform your computation and store your output, anc then chunk through the rest of the image Read more here [Link] io/en/latest/api/[Link] [Link] For now, because this image is small, well ust read in and display the entire image: full_ing = [Link]() full_img.shape # bands, rows, cols (8, 25, 250) With our data read into a NumPy array, we can print it to console and even perform statistics on it in addition to helping us store massive amounts of data efficiently, NumPy will help us with some basic linear algebra, numerical operations, and summary statistics. For now let's plot that near infrared band we read in earlier. from [Link] import show # import the show function which allows us to display th print("Inage dimensions: ", full_img.shape) show(nir, transform=[Link], cnap="gray') Image dimensions: (8, 258, 25@) 464000 466000 468000 The next chapter (link to webpage or Notebook) puts these lessons to use in order to calculate the Normalized Difference Vegetation Index (NOVI).

You might also like