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 details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.7+ Windows x86-64

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

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 details)

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 details)

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 details)

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 details)

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

File details

Details for the file geo_rasterize-0.9.0.tar.gz.

File metadata

  • Download URL: geo_rasterize-0.9.0.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.12.20

File hashes

Hashes for geo_rasterize-0.9.0.tar.gz
Algorithm Hash digest
SHA256 4a23c0c2e69646c2e65a831dd356e4291b409534bfbc4b19af3854fa0dd76f51
MD5 23fb2d8efc43011a7523a90babbfdd8b
BLAKE2b-256 f4614ebfbfd7e02ef67097ce656501e4dd9426dffa291633bfafaad305dba984

See more details on using hashes here.

File details

Details for the file geo_rasterize-0.9.0-cp37-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for geo_rasterize-0.9.0-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b687cf7e1701a2b126204531d6644ab7e125484f574029a1145347600dc3ec4a
MD5 e839fb59b4c72feb49840ddd81cd8648
BLAKE2b-256 037aff423f3c4f10718b9947761b7d83ba7fdb9358bbb7dd50c305378522ed4d

See more details on using hashes here.

File details

Details for the file geo_rasterize-0.9.0-cp37-abi3-win32.whl.

File metadata

File hashes

Hashes for geo_rasterize-0.9.0-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 f79f250828324a48f535a3ec66f1e1107d763ad96e39fb053e70362d6d92458a
MD5 81926376c9e6ef3e390b8a0c43f87981
BLAKE2b-256 64f1180964fe681d075a465e3504f6e17477f46257f750d39d57c64351f701b1

See more details on using hashes here.

File details

Details for the file geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3d83000d9266f80107ea6f579850c5213efc563f8553af90941170387bb65dc6
MD5 5d6629a3ae64cb07a23834e60731c2ef
BLAKE2b-256 0edd16ec792ae5eff70c7d47b86f770fb2f1d374a1e4cd10d6f4079c391608b5

See more details on using hashes here.

File details

Details for the file geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 0b2580b339df7518a7e445482947e00614e850c2cb5e5cebba35bc3beb43256e
MD5 d91e6b762a4e7573e2cf8b90d24c28e7
BLAKE2b-256 05b2900bafe858249537d35b418cb7297464de026a825b28360e4a4b09a592b9

See more details on using hashes here.

File details

Details for the file geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 06e563c5ac7aa19ef11d97c66333b0c9fa0831e9d6d8865ad47373796fd47216
MD5 efd907197cb99ed678a45ccc8aad4923
BLAKE2b-256 7f4d9ffed048b992e5aa64a7f56f519e28e739a0975161cb68cce7be037de7ea

See more details on using hashes here.

File details

Details for the file geo_rasterize-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for geo_rasterize-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 289a53452820b28a5493c957beb5c40208993420734ea2ec88a3a3feee434513
MD5 7223bd4e9c21247291684ef7fef42567
BLAKE2b-256 e466e997033c9a581a67cb815aef66443414c55998948718b3491598a54cc9ce

See more details on using hashes here.

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