This question already has an answer here:

I have a CSV points file, with NDVI values. My goal is to create a GeoTiff with 10 meters pixel, where each pixel value is calculated as the mean of points values in this square.

I read the CSV with Pandas and convert it to a geodataframe with GeoPandas.

How can build an empty raster and fill it with mean of NDVI value ? Maybe with GDAL?

Here is my code:

import sys, os, math, csv
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point

csv_file = (sys.argv[1]) # Path fichier csv

os.chdir = os.path.dirname(csv_file)
csv_name = os.path.basename(csv_file)
print(os.chdir, '-->', csv_name)

pixel = 10 # Taille du pixel en mètre

# CSV parcellaire en GeoDataFrame
csv_df = pd.read_csv(csv_file, header = 0) # Lecture du CSV
csv_gdf = gpd.GeoDataFrame(csv_df) # Conversion en GeoDataFrame

csv_gdf['geometry'] = 'geom' # Création de la colonne geometry

for i, row in csv_gdf.iterrows():
    csv_gdf.geometry[i] = Point([(csv_gdf.X[i], csv_gdf.Y[i])]) # Remplissage de la geometry avec les points de coords X-Y

#Résolution du raster
xmin, ymin, xmax, ymax = csv_gdf.total_bounds # coordonées de la bounding box du csv

xres = math.ceil((xmax - xmin) / pixel)
yres = math.ceil((ymax - ymin) / pixel)

marked as duplicate by nickves, MaryBeth, whyzar, Jochen Schwarze, Dan C Dec 13 '18 at 16:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Use gdal_grid (http://www.gdal.org/gdal_grid.html).

Your use case is the reason it was made for.

Ofcource uses can access the gdal grid functionality from within Python, as its python binding provide an API gateway

(for more information one can look at : Python - gdal.Grid() correct use)

  • 1
    It would be more helpful if you gave a snippet of code, showing how gdal_grid could be used, in terms of the poster's use-case – Shawn Dec 13 '18 at 9:38
  • Thank you for the feedback @Shawn. The answer suggests an alternative, of instead doing it programmatically, to do it using an external command. At the bottom of the linked page, there is a working example of what you are asking. The reason I would advocate doing that would be that creating a raster from points would require to extrapolate their values, and that would be quite tricky implementing it. – nickves Dec 13 '18 at 12:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.