Package: geo-rasterize
geo-rasterize: a 2D rasterizer for geospatial applications, written in Rustgeo-rasterizeis a Python wrapper for arust library with the same
namethat rasterizesshapelyvector shapes, just likerasterio'sfeatures.rasterize. The
difference is that whilerasteriois built on GDAL, this library has no dependencies -- you can install
it without having to worry about GDAL (or any other C library at all)!
Plusgeo-rasterize's rasterization algorithm is based on GDAL's so
it should be a drop in replacement forrasterio.features.rasterizeand it offers a very similar API.We publish wheels to PyPI Python 3.7+ for the following platforms:Operating SystemArchitectureLinuxx86-64Linuxi686Linuxaarch64Windowsx86-64Windowsi686MacOSUniversal2 (x86-64 and aarch64)ExamplesFor example, let's rasterize a singlePointlocated at(x=1, y=2)onto a raster that is 5 pixels wide and 6 pixels high. By default, the
raster pixels will start out with value zero, and we'll put a1in
every pixel the point touches:>>>fromshapely.geometryimportPoint>>>fromgeo_rasterizeimportrasterize>>>print(rasterize([Point(1,2)],[1],(5,6)))[[00000][00000][01000][00000][00000][00000]]And the result is just what we expect: a 5x6numpyarray with
exactly one pixel set to 1! Note that we had to specify a list of
shapes rather than just one shape. And the list of foreground values
([1]in this case) has to have the same length since we'll need one
foreground value for each shape.So let's see multiple shapes!>>>fromshapely.geometryimportPoint,LineString>>>fromgeo_rasterizeimportrasterize>>>shapes=[Point(3,4),LineString([(0,3),(3,0)])]>>>foregrounds=[3,7]>>>raster_size=(4,5)>>>print(rasterize(shapes,foregrounds,raster_size))[[0070][0770][7700][7000][0003]]What happens when two shapes burn in the same pixel? That depends on
how you set the merge algorithm, given by thealgorithmparameter. The default is'replace'which means the last shape
overwrites the pixel but you can also set it to'add'to that
foreground values will sum. That allows you to make heatmaps!>>>fromshapely.geometryimportPoint,LineString>>>fromgeo_rasterizeimportrasterize>>>shapes=[LineString([(0,0),(5,5)]),LineString([(5,0),(0,5)])]>>>print(rasterize(shapes,[1,1],(5,5),algorithm='add'))[[10001][01011][00210][01110][11001]]Our two lines cross at the center where you'll find2.You can change the default value using thebackgroundparameter. And
you can set the arraydtypeusing thedtypeparameter. Settingdtype='uint8'will reduce the space needed for your raster array by
8x. This is especially useful if you're only interested in binary
rasterization.Geographic transformsAll our examples so far have assumed that our shapes' coordinates are
in the image space. In other words, we've assumed that thexcoordinates will be in the range0..widthand theycoordinates
will be in the range0..height. Alas, that is often not the case!For satellite imagery (or remote sensing imagery in general), images
will almost always specify both a Coordinate Reference System
(CRS) and an
affine transformation in their metadata. Seerasterio's
Georeferencingfor more details.In order to work with most imagery, you have to convert your vector
shapes from whatever their original CRS is (oftenEPSG:4326for
geographic longitude and latitude) into whatever CRS your data file
specifies (often aUTMprojection but there are so many choices). Then, you need to apply an
affine transformation to convert from world coordinates to pixel
coordinates. Since raster imagery usually specifies the inverse
transformation matrix (i.e. apix_to_geotransform), you'll first
need to invert it to get ageo_to_pixtransform before applying it
to the coordinates. And now you've got pixel coordinates appropriate
for your image data!geo-raterizecan ease this tedious process by taking care of the
affine transformation. Just pass an affine transform array using thegeo_to_pixparameter (call.to_gdal()if you have anaffine.Affineinstance).To summarize, you'll have to:extract the CRS from your image and convert your shapes into that
CRS (probably usingpyprojand its integration with [geo types][geo],extract thepix_to_geotransform from your imagery metadatacreate anAffineinstance from that data (GDAL represents these
as a[f64; 6]array and you can useAffine.from_gdal)calltransform.inverseto get the correspondinggeo_to_pixtransform (remember that not all transforms are invertible!)calltransform.to_gdal()and use the resulting array with thegeo_to_pixparameter