Grow your YouTube views, likes and subscribers for free
Get Free YouTube Subscribers, Views and Likes

Convert between CSV and GeoTIFF with GDAL in Python

Follow
Making Sense Remotely

Recently I got a couple of questions regarding the conversion of raster data from CSV to GeoTIFF and vice versa, so I decided to make a tutorial on this topic. In this video, I explain different methods to convert a GeoTIFF file into a threecolumn structure with X + Y coordinates and raster value and to convert CSV data into GeoTiff using GDAL and Python.

GDAL/OGR Python API: https://gdal.org/python/

Chapters:
0:00 Introduction
0:44 GeoTIFF to CSV by converting to XYZ with gdal.Translate
4:04 GeoTIFF to CSV by flattening the raster array and calculating coordinates
13:26 CSV to GeoTIFF by creating a XYZ file and translating it
22:00 CSV to GeoTIFF with a VRT file, gdal.Rasterize or gdal.Grid

Code:

from osgeo import gdal
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt

ds = gdal.Open("dem.tif")

TIFF to CSV
first option
xyz = gdal.Translate("dem.xyz", ds)
xyz = None

df = pd.read_csv("dem.xyz", sep = " ", header = None)
df.columns = ["x","y", "value"]
df.to_csv("dem.csv", index = False)

second option
ar = ds.GetRasterBand(1).ReadAsArray()
flat = ar.flatten()
gt = ds.GetGeoTransform()
res = gt[1]
xmin = gt[0]
ymax = gt[3]
xsize = ds.RasterXSize
ysize = ds.RasterYSize
xstart = xmin +res/2
ystart = ymax res/2
ds = None

x = np.arange(xstart, xstart+xsize*res, res)
y = np.arange(ystart, ystartysize*res, res)
x = np.tile(x, ysize)
y = np.repeat(y, xsize)

dfn = pd.DataFrame({"x":x, "y":y, "value":flat})

CSV to TIFF
first option
dfn.to_csv("dfn.xyz", index = False, header = None, sep = " ")
demn = gdal.Translate("demn.tif", "dfn.xyz", outputSRS = "EPSG:32719")
demn = None

def toTIFF(dfn, name):
dfn.to_csv(name+".xyz", index = False, header = None, sep = " ")
demn = gdal.Translate(name+".tif", name+".xyz", outputSRS = "EPSG:32719",
xRes = res, yRes = res)
demn = None

shuffle = dfn.sample(frac = 1)
shuffle = shuffle.sort_values(by = ["y", "x"], ascending = [False, True])
toTIFF(shuffle, "shuffle")

sample = dfn.sample(frac = 0.1)
sample = sample.sort_values(by = ["y", "x"], ascending = [False, True])
toTIFF(sample, "sample")

uneven = sample.copy()
uneven.x = uneven.x + np.random.randint(6, size = len(uneven))
uneven.y = uneven.y + np.random.randint(6, size = len(uneven))
toTIFF(uneven, "uneven") # not working

second option
uneven.to_csv("uneven.csv", index = False)
if os.path.exists("uneven.vrt"):
os.remove("uneven.vrt")

f = open("uneven.vrt", "w")
f.write() # !!! here you have to put the information that should be written to the VRT file. YouTube doesn't allow angled brackets in the description, so I can't put that here, but you can copy the necessary lines from the GDAL webpage: https://gdal.org/programs/gdal_grid.h...
f.close()

r = gdal.Rasterize("uneven.tif", "uneven.vrt", outputSRS = "EPSG:32719",
xRes = res, yRes = res, attribute = "value", noData = np.nan)
r = None

g = gdal.Grid("unevenInt.tif", "uneven.vrt", outputSRS = "EPSG:32719")
g = None

posted by Wirtbergys