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.
pip install pygeoifVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create various geometry types (Point, LineString, Polygon), a Feature with properties, and how to import geometry from a Well-Known Text (WKT) string using `pygeoif`.
Ensure your project runs on Python 3.9 or newer when using pygeoif 1.6.0 or later. For older pygeoif versions, check their specific Python requirements.
Update imports from `from pygeoif.geometry import as_shape` to `from pygeoif import shape` and adjust calls accordingly.
Instead of modifying existing geometry objects, create new ones with the desired changes. For example, `new_point = Point(x + 1, y)` instead of `point.x += 1`.
If migrating from or interoperating with Shapely, thoroughly test your code with pygeoif, especially around geometry validation and complex operations. Consult both libraries' documentation for specific behavior details.
When exchanging data with other GIS tools or formats, be mindful that `GeometryCollection` objects might not be directly ingestible and may require decomposition into simpler geometry types.
Ensure the object passed to `as_shape` has a `__geo_interface__` attribute that returns a GeoJSON-like dictionary, or provide a valid GeoJSON-compatible dictionary directly. If using another library, ensure its geometry objects properly expose the `__geo_interface__`.
```python
from pygeoif import geometry, shape
from shapely.geometry import Point as ShapelyPoint
# Correct: Using an object that implements __geo_interface__ (like Shapely's Point)
p_shapely = ShapelyPoint(1, 1)
py_geom = shape(p_shapely)
# Correct: Using a GeoJSON-compatible dictionary
geojson_dict = {'type': 'Point', 'coordinates': (2, 2)}
py_geom_from_dict = shape(geojson_dict)
```Import the `shape` function directly from the `pygeoif` top-level module instead of `pygeoif.geometry`. If you specifically need `as_shape` and encounter this, ensure your `pygeoif` version is up-to-date, or use the recommended `shape` function. ```python # Incorrect (likely for older versions or if trying to match Shapely's old 'asShape') # from pygeoif.geometry import as_shape # Correct way to import and use in pygeoif (v1.0+) from pygeoif import shape from pygeoif.geometry import Point py_point = Point(1, 1) converted_geom = shape(py_point) ```
If you require advanced geospatial operations, convert your `pygeoif` geometries to `Shapely` geometries first using `shapely.geometry.shape()`, perform the operations, and then convert back to `pygeoif` if necessary. If only basic properties are needed, consult the `pygeoif` documentation for available attributes (`geom_type`, `bounds`, `wkt`) and methods. ```python from pygeoif.geometry import Point from shapely.geometry import shape as shapely_shape from shapely.geometry import Point as ShapelyPoint py_point = Point(0, 0) # Incorrect (pygeoif Point does not have a 'buffer' method) # buffered_point = py_point.buffer(10) # Correct: Convert to Shapely, perform operation, optionally convert back shapely_point = shapely_shape(py_point) buffered_shapely_point = shapely_point.buffer(10) # If you need a pygeoif object again: from pygeoif import shape as pygeoif_shape buffered_pygeoif_point = pygeoif_shape(buffered_shapely_point.__geo_interface__) ```
Ensure the WKT string strictly adheres to the OGC Well-Known Text standard (e.g., 'POINT (X Y)', 'LINESTRING (X1 Y1, X2 Y2)', 'POLYGON ((X1 Y1, X2 Y2, X3 Y3, X1 Y1))'). Validate your WKT string with a known good example or a WKT validator if unsure. ```python from pygeoif import from_wkt # Correct WKT string for a Point point_wkt_valid = 'POINT (10 20)' point_geom = from_wkt(point_wkt_valid) # Incorrect WKT string (e.g., missing parentheses, wrong format) # point_wkt_invalid = 'POINT 10 20' # point_geom = from_wkt(point_wkt_invalid) # Would raise ValueError # Correct WKT string for a LineString linestring_wkt_valid = 'LINESTRING (30 10, 10 30, 40 40)' linestring_geom = from_wkt(linestring_wkt_valid) ```
Access individual coordinates using `point.x`, `point.y`, or create a tuple `(point.x, point.y)`.