Skip to main content

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

Metadata

Release files for geo-rasterize 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for geo-rasterize 0.9.0
File Size Uploaded Provenance
geo_rasterize-0.9.0.tar.gz 15.0 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for geo-rasterize 0.9.0
File
geo_rasterize-0.9.0-cp37-abi3-win_amd64.whl CPython 3.7 abi3 Windows x86-64 Details
geo_rasterize-0.9.0-cp37-abi3-win32.whl CPython 3.7 abi3 Windows x86-32 Details
geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.7 abi3 Linux glibc 2.17+ x86-64 Details
geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.7 abi3 Linux glibc 2.17+ x86-32 Details
geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.7 abi3 Linux glibc 2.17+ ARM64 Details
geo_rasterize-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl CPython 3.7 abi3 macOS 10.9+ x86-64, macOS 10.9+ universal2 (ARM64, x86-64), macOS 11.0+ ARM64 Details

Total release size: 1.8 MB

Release files / geo_rasterize-0.9.0.tar.gz

Download URL geo_rasterize-0.9.0.tar.gz
Size 15.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4a23c0c2e69646c2e65a831dd356e4291b409534bfbc4b19af3854fa0dd76f51
BLAKE2b-256 checksum
How to use checksums
f4614ebfbfd7e02ef67097ce656501e4dd9426dffa291633bfafaad305dba984
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.12.20

Release files / geo_rasterize-0.9.0-cp37-abi3-win_amd64.whl

Download URL geo_rasterize-0.9.0-cp37-abi3-win_amd64.whl
Size 213.7 kB
Tags CPython 3.7 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
b687cf7e1701a2b126204531d6644ab7e125484f574029a1145347600dc3ec4a
BLAKE2b-256 checksum
How to use checksums
037aff423f3c4f10718b9947761b7d83ba7fdb9358bbb7dd50c305378522ed4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.12.20

Release files / geo_rasterize-0.9.0-cp37-abi3-win32.whl

Download URL geo_rasterize-0.9.0-cp37-abi3-win32.whl
Size 198.8 kB
Tags CPython 3.7 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
f79f250828324a48f535a3ec66f1e1107d763ad96e39fb053e70362d6d92458a
BLAKE2b-256 checksum
How to use checksums
64f1180964fe681d075a465e3504f6e17477f46257f750d39d57c64351f701b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.12.20

Release files / geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 298.9 kB
Tags CPython 3.7 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
3d83000d9266f80107ea6f579850c5213efc563f8553af90941170387bb65dc6
BLAKE2b-256 checksum
How to use checksums
0edd16ec792ae5eff70c7d47b86f770fb2f1d374a1e4cd10d6f4079c391608b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.12.20

Release files / geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Size 319.8 kB
Tags CPython 3.7 Linux glibc 2.17+ x86-32 abi3
SHA-256 checksum
How to use checksums
0b2580b339df7518a7e445482947e00614e850c2cb5e5cebba35bc3beb43256e
BLAKE2b-256 checksum
How to use checksums
05b2900bafe858249537d35b418cb7297464de026a825b28360e4a4b09a592b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.12.20

Release files / geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL geo_rasterize-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 258.2 kB
Tags CPython 3.7 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
06e563c5ac7aa19ef11d97c66333b0c9fa0831e9d6d8865ad47373796fd47216
BLAKE2b-256 checksum
How to use checksums
7f4d9ffed048b992e5aa64a7f56f519e28e739a0975161cb68cce7be037de7ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.12.20

Release files / geo_rasterize-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl

Download URL geo_rasterize-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Size 506.9 kB
Tags CPython 3.7 abi3 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
289a53452820b28a5493c957beb5c40208993420734ea2ec88a3a3feee434513
BLAKE2b-256 checksum
How to use checksums
e466e997033c9a581a67cb815aef66443414c55998948718b3491598a54cc9ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.12.20

Security for geo-rasterize 0.9.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page