hhh
hhh

Reputation: 52810

Python Georasters: geotiff image into geopandas dataframe or pandas dataframe?

I want to convert geotiff file into geopandas dataframe or pandas dataframe. I found this solution here but

#!pip3 install georasters

import georasters as gr
import pandas
myRaster = 'demo.tif'
elevation = gr.from_file(myRaster)
    
#elevation #<georasters.georasters.GeoRaster at 0x7f38507aff90>


#elevation = elevation.to_pandas()
#elevation = elevation.to_geopandas()

but I am getting the error ValueError: Must pass 2-d input. shape=(3, 1500, 3000).

Example geotiff below that I found from here.

enter image description here

How can I convert geotiff file into pandas dataframe or geopandas dataframe?

Upvotes: 11

Views: 22137

Answers (3)

Franck Theeten
Franck Theeten

Reputation: 140

The rioxarray and earthpy packages can work. See this interresting tutorial on https://www.earthdatascience.org/courses/scientists-guide-to-plotting-data-in-python/plot-spatial-data/customize-raster-plots/plotting-extents/

Here is an example of code superimposing a GeoTif (elevation background) and three vectorial layers (boundaries, lakes and rivers of Burundi).

import matplotlib.pyplot as plt

import geopandas
import rioxarray as rxr
from rasterio.plot import plotting_extent
import earthpy.plot as ep


fp = "couleur_elevation_burundi.tif"
bdi="BDI_adm0.gpkg"
lakes="osm_lakes.gpkg"
rivers="osm_rivers_fusionne.gpkg"
output_map="_test_color.png"
raster_data = rxr.open_rasterio(fp, masked=True)

raster_data_extent = plotting_extent(raster_data[0], raster_data.rio.transform())

print(raster_data_extent)                                   
f, ax = plt.subplots()
ax.set_xlim(29, 31)
ax.set_ylim(-4.5, -2.3)

ep.plot_rgb(raster_data.values,
            rgb=[0, 1, 2],
            ax=ax,
            title="test Burundi",
            extent=raster_data_extent)

    
lakes_shp=geopandas.read_file(lakes)
lakes_shp.plot(ax=ax)
lakes_shp.set_crs(epsg=4326, inplace=True)

rivers_shp=geopandas.read_file(rivers)
rivers_shp.plot(ax=ax, linewidth=0.2, color="blue")
rivers_shp.set_crs(epsg=4326, inplace=True)


prov_shp=geopandas.read_file(bdi)
prov_shp.boundary.plot(ax=ax,color="black", linewidth=0.3)
prov_shp.set_crs(epsg=4326, inplace=True)
ax.set_xticks([29,29.5,30, 30.5,31])
ax.set_yticks([-4.5, -4, -3.5, -3, -2.5])    

plt.savefig(output_map)

enter image description here

Upvotes: 1

Luckasino
Luckasino

Reputation: 424

You can also do it by xarray or rioxarray library.

import rioxarray as rxr
import xarray as xr

dataarray = rxr.open_rasterio('file.tif')
dataarray = xr.open_rasterio('file.tif')

df = dataarray[0].to_pandas()

Upvotes: 3

hhh
hhh

Reputation: 52810

There are multiple techniques to convert Geotiff into Dataframe. I list below methods that I have so far got working. I am unable to resolve the Georasters issue.

Options

  1. First convert tiff file into CSV and then to dataframe, commandline option:

    $ pip3 install raster2xyz
    $ raster2xyz demo.tif out.csv        (marketed as faster alternative to gdal2xyz.py)
    
  2. In Python,

    from raster2xyz.raster2xyz import Raster2xyz
    
    input_raster = "demo.tif"
    out_csv = "demo_out_xyz.csv"
    
    rtxyz = Raster2xyz()
    rtxyz.translate(input_raster, out_csv)
    
    myRasterDF = pd.read_csv(out_csv)
    
    myRasterDF
    

Other interesting questions

  1. Polygonisation of Geotiff into pandas dataframe here

  2. First convert geotiff into CSV and then CSV into pandas dataframe, related question here

Upvotes: 5

Related Questions