Search code examples
pythongisgdalgeopandasrasterio

Raster from XYZ csv format


I have a large csv file of 3 columns of x y and z values. I want to do the following: 1. How to convert that csv into vector shape file (points). 2. How to convert the obtained points in step 2 into geo raster. 3. Lets say a set of separate point file in "shp" format how to extract the values from the raster obtained in step 2 into new the csv file so I can perform so statistics on it.

The issue is that I'm new to geo-processing using python, for example for step 2 I used to do that easily in R using "raster" package and for step 3 also using "extract" function in "raster" package. However, in python I can do step 2 in geopandas but step 2 and 3 no simple answer is available.


Solution

  • This exact case exists in the GDAL documentation.

    Assuming your data, dem.csv, is like:

    Easting,Northing,Elevation
    86943.4,891957,139.13
    87124.3,892075,135.01
    86962.4,892321,182.04
    87077.6,891995,135.01
    ...
    

    You create a VRT to describe it:

    <OGRVRTDataSource>
        <OGRVRTLayer name="dem">
            <SrcDataSource>dem.csv</SrcDataSource>
            <GeometryType>wkbPoint</GeometryType>
            <GeometryField encoding="PointFromColumns" x="Easting" y="Northing" z="Elevation"/>
        </OGRVRTLayer>
    </OGRVRTDataSource>
    

    Or, if the CSV lacks columns, you can specify your GeometryField like this:

    <GeometryField encoding="PointFromColumns" x="field_1" y="field_2" z="field_3"/>
    

    Save that as something likedem.vrt for use in later commands.

    Then to interpolate with gdal_grid and produce a GeoTIFF output:

    gdal_grid -a invdist:power=2.0:smoothing=1.0 -txe 85000 89000 -tye 894000 890000 -outsize 400 400 -of GTiff -ot Float64 -l dem dem.vrt dem.tiff