Reputation: 52810
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.
How can I convert geotiff file into pandas dataframe or geopandas dataframe?
Upvotes: 11
Views: 22137
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)
Upvotes: 1
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
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.
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)
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
Polygonisation of Geotiff into pandas dataframe here
First convert geotiff into CSV and then CSV into pandas dataframe, related question here
Upvotes: 5