Skip to main content

a 2D rasterizer for geospatial applications, written in Rust

Project description

geo-rasterize: a 2D rasterizer for geospatial applications, written in Rust

PyPI Build Status

geo-rasterize is a Python wrapper for a rust library with the same name that rasterizes shapely vector shapes, just like rasterio's features.rasterize. The difference is that while rasterio is 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)! Plus geo-rasterize's rasterization algorithm is based on GDAL's so it should be a drop in replacement for rasterio.features.rasterize and it offers a very similar API.

We publish wheels to PyPI Python 3.7+ for the following platforms:

Operating System Architecture
Linux x86-64
Linux i686
Linux aarch64
Windows x86-64
Windows i686
MacOS Universal2 (x86-64 and aarch64)

Examples

For example, let's rasterize a single Point located 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 a 1 in every pixel the point touches:

>>> from shapely.geometry import Point
>>> from geo_rasterize import rasterize
>>> print(rasterize([Point(1, 2)], [1], (5, 6)))
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 1 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]

And the result is just what we expect: a 5x6 numpy array 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!

>>> from shapely.geometry import Point, LineString
>>> from geo_rasterize import rasterize
>>> shapes = [Point(3, 4), LineString([(0, 3), (3, 0)])]
>>> foregrounds = [3, 7]
>>> raster_size = (4, 5)
>>> print(rasterize(shapes, foregrounds, raster_size))
[[0 0 7 0]
 [0 7 7 0]
 [7 7 0 0]
 [7 0 0 0]
 [0 0 0 3]]

What happens when two shapes burn in the same pixel? That depends on how you set the merge algorithm, given by the algorithm parameter. 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!

>>> from shapely.geometry import Point, LineString
>>> from geo_rasterize import rasterize
>>> shapes = [LineString([(0, 0), (5, 5)]), LineString([(5, 0), (0, 5)])]
>>> print(rasterize(shapes, [1, 1], (5, 5), algorithm='add'))
[[1 0 0 0 1]
 [0 1 0 1 1]
 [0 0 2 1 0]
 [0 1 1 1 0]
 [1 1 0 0 1]]

Our two lines cross at the center where you'll find 2.

You can change the default value using the background parameter. And you can set the array dtype using the dtype parameter. Setting dtype='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 transforms

All our examples so far have assumed that our shapes' coordinates are in the image space. In other words, we've assumed that the x coordinates will be in the range 0..width and the y coordinates will be in the range 0..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. See rasterio's Georeferencing for more details.

In order to work with most imagery, you have to convert your vector shapes from whatever their original CRS is (often EPSG:4326 for geographic longitude and latitude) into whatever CRS your data file specifies (often a UTM projection 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. a pix_to_geo transform), you'll first need to invert it to get a geo_to_pix transform before applying it to the coordinates. And now you've got pixel coordinates appropriate for your image data!

geo-raterize can ease this tedious process by taking care of the affine transformation. Just pass an affine transform array using the geo_to_pix parameter (call .to_gdal() if you have an affine.Affine instance).

To summarize, you'll have to:

  • extract the CRS from your image and convert your shapes into that CRS (probably using pyproj and its integration with [geo types][geo],
  • extract the pix_to_geo transform from your imagery metadata
  • create an Affine instance from that data (GDAL represents these as a [f64; 6] array and you can use Affine.from_gdal)
  • call transform.inverse to get the corresponding geo_to_pix transform (remember that not all transforms are invertible!)
  • call transform.to_gdal() and use the resulting array with the geo_to_pix parameter

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

geo_rasterize-0.9.0.tar.gz (15.0 kB view hashes)

Uploaded Source

Built Distributions

geo_rasterize-0.9.0-cp37-abi3-win_amd64.whl (213.7 kB view hashes)

Uploaded CPython 3.7+ Windows x86-64

geo_rasterize-0.9.0-cp37-abi3-win32.whl (198.8 kB view hashes)

Uploaded CPython 3.7+ Windows x86

geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (298.9 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (319.8 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ i686

geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (258.2 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

geo_rasterize-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (506.9 kB view hashes)

Uploaded CPython 3.7+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page