Creating a raster (.tif) from points (geodataframe object)

Hi! I have a geodataframe with a list points that contain x, y and z values. I’d like to convert these points to a raster and export as a .tif file.

After some googling, I found that geocube has a function make_geocube that can do this. However, It does not seem to have anything built in which allows me to interpolate the data to fill the NaN values. Does anyone perhaps know another library that can also do the interpolation?

Thanks in advance!

Hi Yang! First of all, welcome to the community! Nice to see you here.

I have a question, is it really needed to have a raster? Could you use a vector image format, like .svg or .eps?

What would you like to achieve?

Hi Anande,

Thanks for the reply!

Well, the raster (downloadable output of my app) is the input for Civil3D. I have never used Civil3D but I just talked to a colleague about it. It turns out that Civil3D also supports csv inputs, which is much easier to program. I think for now we’ll try that out and see if it works! :slight_smile:

1 Like

Great, that sound as an easier solution! Good luck with everything, and let us know if you solved the problem :slight_smile:

Hi Yang,

If you want the make a raster, geocube looks like the way to go. On the make_geocube function, there is a argument ‘fill’ to fill missing data instead of NaN. Which can be used like this:

geo_grid = make_geocube(
    resolution=(-0.1, 0.00001),

More information on the ‘make_geocube’ can be found here: Core — geocube 0.4.2 documentation

The to_raster function only takes a direct Path, to use this in combination with a DownloadView, you can use something like a NamedTempFile. Let me know if you need help with that.

GeoPandas offers a wide variety of export formats using the ‘to file’ function. For example shapefile (.shp), geojson (.geojson) or GeoPackage (.GPKG). Which are much more suitable to store Geo data. I would expect that Civil3D supports on 1 or multiple of these formats.

To download a such a file from a GeoDataFrame, use the following code (GeoJson example):

output_file = File()

with output_file.open_binary() as fp:
    geo_data_frame.to_file('dataframe.geojson', driver='GeoJSON')

The `output_file’ can be used in a DownloadResult

1 Like