[GIS] Extract Raster Information using Python

cell-sizecoordinate systempythonraster

I would like to extract raster information (i.e. coordinate system, cell size) to excel file using python, how can I do that?

Best Answer

Here is one approach that uses Rasterio to extract the raster info and Pandas to write the data to a CSV file.

import rasterio
import pandas as pd

def spatial_resolution(raster):
    """extracts the XY Pixel Size"""
    t = raster.transform
    x = t[0]
    y =-t[4]
    return x, y

def get_crs(raster):
    """extracts the EPSG spatial reference"""
    return raster.crs.to_epsg()

def write_to_csv(x_res, y_res, sr, out_csv):
    """writes the data to a CSV file"""
    d = {'x_resolution':[x_res], 'y_resolution':[y_res], 'epsg':[sr]}
    df = pd.DataFrame(data = d)
    df.to_csv(out_csv)

if __name__ == "__main__":
    raster = rasterio.open('/path/to/your/rasterdata.tif')
    out_csv = '/path/to/your/csvfile.csv'
    x_res,y_res = spatial_resolution(raster)
    sr = get_crs(raster)
    write_to_csv(x_res,y_res, sr, out_csv)

enter image description here

Related Question