Skip to main content

Reading and writing GeoTIFFs made easy. Proces satellite data directly from TAR files.

Project description

Python-for-Remote-Sensing-and-GIS

PyRSGIS is a powerful module to read, manipulate and export geo-rasters. The module is built on the GDAL library, and is very efficient for various geospatial analysis. Please do not use this package for commercial purpose without my explicit permission. Researchers/ academicians are welcomed for feedback and technical support.

Official Python repository
Python

Cite : DOI

The module is capable of processing Landsat data from the downloaded TAR files itself.

Please find few example below:

Let's import the module by using the below code
import pyrsgis as rg

We will first start with reading raster and perform some basic operations. Make sure your current working directory is the same where the raster files are located, if not use the following command:
import os
os.chdir("d:/yourDirectory")

Please skip the above two lines of code if the directory is already set to the files location.

To read bands of a stacked satellite image,
(1) For all bands:
dataSource, yourArray = raster.read(file, bands='all')
Which returns the data source containing projection information, and a NumPy array.

To read a list of bands from stacked images:
(2) For list of bands:
dataSource, yourArray = raster.read(file, bands=[2, 3, 4])
Which returns bands 2, 3 & 4 as three-dimensional NumPy array.

To read a specific band from stacked image:
(3) For a specific band:
dataSource, yourArray = raster.read(file, bands=2)
Which returns band number 2 as two-dimensional NumPy array.

To export the bands from the above read data,
(1) For all bands:
raster.export(yourArray, dataSource, "sample_extracted.tif", dtype='int', bands='all')
By default, dtype = 'int', to export float type array (eg. NDVI), use dtype = 'float'
Other options are: 'byte', 'cfloat32', 'cfloat64', 'cint16', 'cint32', 'float32', 'float64', 'int16', 'int32', 'uint8', 'uint16', 'uint32'

(2) For list of bands:
raster.export(yourArray, dataSource, "sample_extracted.tif", bands=[2, 3, 4])

(3) For a specific band:
raster.export(yourArray, dataSource, "sample_extracted.tif", bands=3)

To read the TAR file directly:
yourData = rg.readtar("yourFilename.tar.gz")

Similarly, stacked TIFF file can be read:
yourData = rg.readtif("yourFilename.tif")

After the above code, various properties of the raster can be assessed.
print(yourData.rows)
print(yourData.cols)
will give you the number of rows and columns.

The number of bands can be checked using:
print(yourData.nbands)

The satellite sensor can also be determined.
print(yourRaster.satellite)

If the above code shows the correct satellite sensor correctly, then this getting this should be easy:
print(yourRaster.bandIndex)
This will show correctly the band number for available bands.

Any particular band can be extarcted using:
yourBand = yourData.getband(bandNumber)

The above code returns the band as array which can be visualised using:
display(yourBand)
or display(yourData.getband(bandNumber))
The map can directly be saved as an image.

Map title can also be assigned:
display(yourBand, maptitle='Your Map Title')

The extracted band can be exported using:
yourData.export(yourBand, "yourOutputFilename.tif")
This saves the extracted band to the same directory.
If exporting a float data type (raster with decimal values), please define the datatype explicitly, default is 'int':
yourData.export(yourBand, "yourOutputFilename.tif", datatype='float')

The NDVI (Normalised Difference Vegetaton Index) can be computed easily.
yourndvi = yourData.ndvi()
This returns the NDVI array,which can be exported using the same command used for the band above.

Any normalised difference indev can be computed using:
yourIndex = yourData.nordif(bandNumber2, bandNumber1)
Which performs (band2-band1)/(band2+band1) in the back end.

yourRaster.export(yourndvi, 'yourNDVI.tif', datatype='float')
Be careful that the NDVI is of float datatype, whereas the raw bands are integer datatype. Float data export uses more space on hard drive, so the default has been set to integer. Therefore, to export any float datatype, the argument should be passed explicitly.

Raster files can also be easily converted into CSV files which is mainly required for statistical analysis.
from pyrsgis.convert import rastertocsv

Assign the directory where raster files are located
yourDir = "D:\\yourRasterFolder"
rastertocsv(yourDir, filename='yourFilename.csv')

Generally the NoData or NULL values in the raster become random negative values, negatives can be removed using:
rastertocsv(yourDir, filename='yourFilename.csv', negative=False)

At times the NoData or NULL values in raster become '127' or '65536', they can also be removed by declaring explicitly
rastertocsv(yourDir, filename='yourFilename.csv', remove=[127, 65536])
This is a trial and check process, please check the generated CSV file for such issues

Bad rows in the CSV file represents the cell that has zero values in all the rasters and takes a lot of storage space, it can be eliminated using:
rastertocsv(yourDir, filename='yourFilename.csv', badrows=False)

pyrsgis also allows the user to quickly create the northing and easting coordinates using a reference raster
The flip option can be use to flip the resulting rasters.

from pyrsgis.raster import northing, easting

referenceRaster = 'E:/Example/landcover.tif'
northing(referenceFile, outFile='pyrsgis_northing.tif', flip=True)
easting(referenceFile, outFile='pyrsgis_easting.tif', flip=False)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrsgis-0.3.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

pyrsgis-0.3.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file pyrsgis-0.3.0.tar.gz.

File metadata

  • Download URL: pyrsgis-0.3.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for pyrsgis-0.3.0.tar.gz
Algorithm Hash digest
SHA256 baaccb7077dd1ddc23422e7c9f045a24e3140388ef50e7c3641af8c2addda440
MD5 9bd0d83de53822c8d1a9576c3c9edecf
BLAKE2b-256 a8612b4b63879feeb0c74f3506f8135a0b70037160b6a7ff0175876f27a5a0d2

See more details on using hashes here.

File details

Details for the file pyrsgis-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pyrsgis-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for pyrsgis-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ed5521b675377dcc60d64c820bc8fd2fc1efe12986af6471f14c6bd4b7be4d1
MD5 5fdf546de6902f7dee21fb58aa56d5d6
BLAKE2b-256 92fc11a29275f685cdb7a381c15fb719a388f25a38a750cb429242ffe3262dd0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page