1

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

Aaron
  • 51,658
  • 28
  • 154
  • 317
penguin
  • 21
  • 4
  • If you have esri arcpy.Describe(your_raster).spatialReference https://resources.arcgis.com/en/help/main/10.2/index.html#/Raster_Band_properties/03q30000004r000000/ and https://resources.arcgis.com/en/help/main/10.2/index.html#/Dataset_properties/03q30000008p000000/ is a good start, both apply. GetRasterProperties https://resources.arcgis.com/en/help/main/10.2/index.html#//0017000000m7000000 is also a handy tool to get statistics from the raster but the statistics must be calculated first with CalculateStatistics https://resources.arcgis.com/en/help/main/10.2/index.html#//0017000000m3000000. – Michael Stimson Feb 05 '19 at 04:23
  • 2
    That would depend on the software/libraries you wish to use. If you don't have a preference or paid licenses for one of the commercial products, I would suggest rasterio – user2856 Feb 05 '19 at 04:23

1 Answers1

4

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

Aaron
  • 51,658
  • 28
  • 154
  • 317
  • What unit is the spatial resolution calculated in??? – A Merii Nov 06 '20 at 14:29
  • 1
    @Merii, the spatial resolution would be based on your input spatial reference units. – Aaron Nov 06 '20 at 16:24
  • Sorry if my question is obvious, but is there a way to determine what my input spatial reference units are, using rasterio? – A Merii Nov 07 '20 at 19:28
  • 1
    @AMerii Please open a new question for that--I think it would be a popular question. Otherwise, you can look it up based on the epsg: https://epsg.io/?q=3857 – Aaron Nov 07 '20 at 20:10
  • 1
    Thanks for your replies, I have done as you recommended and posted the question on the link below:

    https://gis.stackexchange.com/questions/378627/determining-the-gsd-of-a-raster-image-in-python

    – A Merii Nov 08 '20 at 03:08