I'm looking to create a raster given just a geopackage (or shapefile, or other vector format). I don't have a template raster, and all I wish to do is burn the shapes into a raster using rasterio. Without the template raster, I'm uncertain how to generate the required inputs (transform, mainly). I'd rather not construct it by hand.
[GIS] How to convert vector format (geopackage, shapefile) to raster format (geotiff) using rasterio without template raster
geocubepythonrasterrasteriovector
Related Solutions
I've been playing with GDALRasterizeLayers this week and have a pretty good idea of what it is doing. By default, it will rasterise a pixel if the pixel centre is within the polygon. If there is nothing in the centre, it won't be rasterised, even if there are parts of a polygon within the pixel limits. To allow the rasterising to work the way you intend, try the "ALL_TOUCHED" option:
gdal.RasterizeLayer(raster_dataset, [1], shape_layer, None, None, [1], ['ALL_TOUCHED=TRUE'])
If you're comfortable building software from source and are on a Unix flavor or OSX, you can go from .las to DEM using PDAL and points2grid. If you're on Windows, I have no idea if you'll be able to get points2grid to build, but you could try.
Both PDAL and points2grid have C++ APIs that you could integrate into your own C++ application. You can also use PDAL's command line executable to create DEMs.
The short instructions for a Unix/OSX system to get a points2grid-enabled PDAL command-line executable are:
- Make sure you have cmake; use your favorite package manager to install
- Download points2grid from Github:
git clone https://github.com/CRREL/points2grid.git
Build and install points2grid:
cd points2grid && cmake . && make && make install && cd ..
If you don't have write access to
/usr/local
, change that last bit tosudo make install
Download PDAL from Github:
git clone https://github.com/PDAL/PDAL.git
Build and install PDAL with points2grid support:
cd PDAL && cmake . -DBUILD_PLUGIN_P2G=ON && make && make install && cd ..
Same caveat to
make install
as aboveWrite a pipeline xml file to describe your gridding process (run
points2grid --help
for more information about the points2grid options):<?xml version="1.0" encoding="utf-8"?> <Pipeline version="1.0"> <Writer type="writers.p2g"> <Option name="grid_dist_x">6.0</Option> <Option name="grid_dist_y">6.0</Option> <Option name="filename">outfile</Option> <Option name="output_type">mean</Option> <Option name="output_type">idw</Option> <Option name="output_format">grid</Option> <Reader type="readers.las"> <Option name="filename">mylasfile.las</Option> </Reader>
Use
pdal pipeline
to create your DEM:pdal pipeline mypipeline.xml
That should create some outfile.xxx
raster files.
Best Answer
Rasterio provides a
from_bounds
method that generates an affine transform for you.