Python – Convert Raster to CSV with Latitude, Longitude, and Value Columns Using GDAL

coordinatesdatagdalpythonrioxarray

I'm looking for a process to convert ASCII gridded data (in this case 60min/1 degree gridded ASCII population data (GPW) from SEDAC: https://sedac.ciesin.columbia.edu/data/set/gpw-v4-population-count-adjusted-to-2015-unwpp-country-totals-rev11/data-download) to a CSV with three columns: lon, lat, and value using open source tools.

This is the data header:

ncols         360
nrows         180
xllcorner     -180
yllcorner     -90
cellsize      1.0000000000001
NODATA_value  -9999

so the data is structured as space-separated values, with each value occupying a position within 360 columns and 180 rows that represents its coordinates via that position:

-9999 -9999 -9999 -9999 -9999 -9999 0.000936705 0.002529013 0.001377391 0.001723122 0.0004472999 ... 

I only want to include positive values in the lon/lat CSV.

I've been using GDAL for other tasks and have been looking in the documentation, but I'm not seeing this type of data manipulation in its scope.

Best Answer

You can do this with rioxarray:

Note: I used data for the column name as values is a pandas specific attribute.

import rioxarray
import pandas

rds = rioxarray.open_rasterio(
    "gpw_v4_population_count_adjusted_to_2015_unwpp_country_totals_rev11_2020_1_deg.tif",
)
rds = rds.squeeze().drop("spatial_ref").drop("band")
rds.name = "data"
df = rds.to_dataframe().reset_index()
df[df.data>=0.0].to_csv("out.csv", index=False)