PyGeoIf
PyGeoIf is a Python library providing a basic, pure-Python implementation of the `__geo_interface__` protocol. It enables the creation and manipulation of standard geospatial vector data types like Point, LineString, and Polygon, along with collections, making it suitable as a lightweight alternative to libraries like Shapely or as a foundation for building custom geospatial tools. The current version is 1.6.0, with an active release cadence, often aligning with Python version support changes and feature enhancements.
Warnings
- breaking Python 2 support was removed, and the minimum required Python version has changed multiple times. Version 1.0.0 required Python >=3.7. Version 1.6.0 dropped support for Python 3.8, now requiring Python >=3.9.
- breaking The `as_shape` function was renamed to `shape` in version 1.0.0 to align with Shapely's API. Direct import `from pygeoif import shape` is the modern approach.
- breaking Geometries became immutable in version 1.2.0. Attempting to modify attributes of a geometry object (e.g., coordinates of a Point) directly will raise an error.
- gotcha While pygeoif implements the `__geo_interface__` protocol similarly to Shapely, their internal behavior for certain edge cases (e.g., validity checks, operations on degenerate geometries) can differ. This can lead to unexpected results if switching between libraries without thorough testing.
- gotcha `GeometryCollection` is part of the GeoJSON specification but is not universally supported across all GIS software or file formats (e.g., Shapefile).
Install
-
pip install pygeoif
Imports
- Point
from pygeoif import Point
- LineString
from pygeoif import LineString
- Polygon
from pygeoif import Polygon
- Feature
from pygeoif import Feature
- FeatureCollection
from pygeoif import FeatureCollection
- from_wkt
from pygeoif import from_wkt
- shape
from pygeoif import shape
Quickstart
from pygeoif import Point, LineString, Polygon, Feature, from_wkt
# Create a Point
p = Point(1.0, -1.0)
print(f"Point: {p}")
print(f"Point Geo Interface: {p.__geo_interface__}")
# Create a LineString
l = LineString([(0, 0), (1, 1), (2, 0)])
print(f"LineString: {l}")
# Create a Polygon with a hole
exterior = [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)]
interior = [(2, 2), (2, 8), (8, 8), (8, 2), (2, 2)]
poly = Polygon(exterior, [interior])
print(f"Polygon: {poly.wkt}")
# Create a Feature with properties
feature_props = {'name': 'My Awesome Feature', 'id': 123}
f = Feature(p, feature_props)
print(f"Feature geometry type: {f.geometry.geom_type}")
print(f"Feature properties: {f.properties}")
# Create geometry from WKT
wkt_point = from_wkt('POINT (5 10)')
print(f"WKT Point: {wkt_point}")