Skip to main content

A basic implementation of the __geo_interface__

Project description

Introduction

PyGeoIf provides a GeoJSON-like protocol for geo-spatial (GIS) vector data.

Other Python programs and packages that you may have heard of already implement this protocol:

So when you want to write your own geospatial library with support for this protocol you may use pygeoif as a starting point and build your functionality on top of it

You may think of pygeoif as a ‘shapely ultralight’ which lets you construct geometries and perform very basic operations like reading and writing geometries from/to WKT, constructing line strings out of points, polygons from linear rings, multi polygons from polygons, etc. It was inspired by shapely and implements the geometries in a way that when you are familiar with shapely you feel right at home with pygeoif

It was written to provide clean and python only geometries for fastkml

https://github.com/cleder/pygeoif/actions/workflows/run-all-tests.yml/badge.svg https://codecov.io/gh/cleder/pygeoif/branch/main/graph/badge.svg?token=2EfiwBXs9X https://img.shields.io/badge/code%20style-black-000000.svg https://img.shields.io/badge/type%20checker-mypy-blue

Example

>>> from pygeoif import geometry
>>> p = geometry.Point(1,1)
>>> p.__geo_interface__
{'type': 'Point', 'bbox': (1, 1, 1, 1), 'coordinates': (1, 1)}
>>> print(p)
POINT (1 1)
>>> p
Point(1, 1)
>>> l = geometry.LineString([(0.0, 0.0), (1.0, 1.0)])
>>> l.bounds
(0.0, 0.0, 1.0, 1.0)
>>> print(l)
LINESTRING (0.0 0.0, 1.0 1.0)

You find more examples in the tests directory which cover every aspect of pygeoif or in fastkml.

Classes

All classes implement the attribute:

  • __geo_interface__: as discussed above

All geometry classes implement the attributes:

  • geom_type: Returns a string specifying the Geometry Type of the object

  • bounds: Returns a (minx, miny, maxx, maxy) tuple (float values) that bounds the object.

  • wkt: Returns the ‘Well Known Text’ representation of the object

Point

A zero dimensional geometry

A point has zero length and zero area.

Attributes

x, y, zfloat

Coordinate values

Example

>>> from pygeoif import Point
>>> p = Point(1.0, -1.0)
>>> print(p)
POINT (1.0 -1.0)
>>> p.y
-1.0
>>> p.x
1.0

LineString

A one-dimensional figure comprising one or more line segments

A LineString has non-zero length and zero area. It may approximate a curve and need not be straight. Unlike a LinearRing, a LineString is not closed.

Attributes

geomssequence

A sequence of Points

LinearRing

A closed one-dimensional geometry comprising one or more line segments

A LinearRing that crosses itself or touches itself at a single point is invalid and operations on it may fail.

A Linear Ring is self closing

Polygon

A two-dimensional figure bounded by a linear ring

A polygon has a non-zero area. It may have one or more negative-space “holes” which are also bounded by linear rings. If any rings cross each other, the geometry is invalid and operations on it may fail.

Attributes

exteriorLinearRing

The ring which bounds the positive space of the polygon.

interiorssequence

A sequence of rings which bound all existing holes.

MultiPoint

A collection of one or more points

Attributes

geomssequence

A sequence of Points

MultiLineString

A collection of one or more line strings

A MultiLineString has non-zero length and zero area.

Attributes

geomssequence

A sequence of LineStrings

MultiPolygon

A collection of one or more polygons

Attributes

geomssequence

A sequence of Polygon instances

GeometryCollection

A heterogenous collection of geometries (Points, LineStrings, LinearRings and Polygons)

Attributes

geomssequence

A sequence of geometry instances

Please note: GEOMETRYCOLLECTION isn’t supported by the Shapefile format. And this sub-class isn’t generally supported by ordinary GIS sw (viewers and so on). So it’s very rarely used in the real GIS professional world.

Example

>>> from pygeoif import geometry
>>> p = geometry.Point(1.0, -1.0)
>>> p2 = geometry.Point(1.0, -1.0)
>>> geoms = [p, p2]
>>> c = geometry.GeometryCollection(geoms)
>>> [geom for geom in geoms]
[Point(1.0, -1.0), Point(1.0, -1.0)]

Feature

Aggregates a geometry instance with associated user-defined properties.

Attributes

geometryobject

A geometry instance

propertiesdict

A dictionary linking field keys with values associated with with geometry instance

Example

>>> from pygeoif import Point, Feature
>>> p = Point(1.0, -1.0)
>>> props = {'Name': 'Sample Point', 'Other': 'Other Data'}
>>> a = Feature(p, props)
>>> a.properties
{'Name': 'Sample Point', 'Other': 'Other Data'}
>>> a.properties['Name']
'Sample Point'

FeatureCollection

A heterogenous collection of Features

Attributes

features: sequence

A sequence of feature instances

Example

>>> from pygeoif import Point, Feature, FeatureCollection
>>> p = Point(1.0, -1.0)
>>> props = {'Name': 'Sample Point', 'Other': 'Other Data'}
>>> a = Feature(p, props)
>>> p2 = Point(1.0, -1.0)
>>> props2 = {'Name': 'Sample Point2', 'Other': 'Other Data2'}
>>> b = Feature(p2, props2)
>>> features = [a, b]
>>> c = FeatureCollection(features)
>>> [feature for feature in c]
[Feature(Point(1.0, -1.0), {'Name': 'Sample Point', 'Other': 'Other Data'},...]

Functions

shape

Create a pygeoif feature from an object that provides the __geo_interface__

>>> from shapely.geometry import Point
>>> from pygeoif import geometry, shape
>>> shape(Point(0,0))
Point(0.0, 0.0)

from_wkt

Create a geometry from its WKT representation

>>> from pygeoif import from_wkt
>>> p = from_wkt('POINT (0 1)')
>>> print(p)
POINT (0.0 1.0)

signed_area

Return the signed area enclosed by a ring using the linear time algorithm at http://www.cgafaq.info/wiki/Polygon_Area. A value >= 0 indicates a counter-clockwise oriented ring.

orient

Returns a copy of a polygon with exteriors and interiors in the right orientation.

if ccw is True than the exterior will be in counterclockwise orientation and the interiors will be in clockwise orientation, or the other way round when ccw is False.

box

Return a rectangular polygon with configurable normal vector.

mapping

Returns the __geo_interface__ dictionary

Development

Installation

You can install PyGeoIf from pypi using pip:

pip install pygeoif

Testing

Install the requirements with pip install -r test-requirements.txt and run the unit and static tests with:

pytest pygeoif
pytest --doctest-glob="README.rst"
yesqa pygeoif/*.py
black pygeoif
flake8 pygeoif
mypy pygeoif

Changelog

1.0.beta.1 (2021-09-20)

  • Add type annotations

  • refactor

  • remove support for python 2

  • minimum python version is 3.6

  • rename as_shape to shape

  • add box factory

  • format with black

  • reconstruct objects from their representation

  • Parse WKT that is not in upper case.

0.7 (2017/05/04)

  • fix broken multipolygon [mindflayer]

  • add “bbox” to __geo_interface__ output [jzmiller1]

0.6 (2015/08/04)

  • Add id to feature [jzmiller1]

0.5 (2015/07/13)

  • Add __iter__ method to FeatureCollection and GeometryCollection [jzmiller1].

  • add pypy and pypy3 and python 3.4 to travis.

  • Add tox configuration for performing local testing [Ian Lee].

  • Add Travis continuous deployment.

0.4 (2013/10/25)

  • after a year in production promote it to Development Status :: 5 - Production/Stable

  • MultiPolygons return tuples as the __geo_interface__

0.3.1 (2012/11/15)

  • specify minor python versions tested with Travis CI

  • fix for signed area

0.3 (2012/11/14)

  • add GeometryCollection

  • len(Multi*) and len(GeometryCollection) returns the number of contained Geometries

  • add orient function to get clockwise or counterclockwise oriented polygons

  • add signed_area function

  • add _set_orientation method to lineStrings, Polygons and MultiPolygons

0.2.1 (2012/08/02)

  • as_shape also accepts an object that is neither a dictionary nor has a __geo_interface__ but can be converted into a __geo_interface__ compliant dictionary

0.2 (2012/08/01)

  • change license to LGPL

  • add wkt as a property

  • as_shape also accepts a __geo_interface__ compliant dictionary

  • test with python3

0.1 (2012/07/27)

  • initial release

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

pygeoif-1.0b4.tar.gz (41.4 kB view details)

Uploaded Source

Built Distribution

pygeoif-1.0b4-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file pygeoif-1.0b4.tar.gz.

File metadata

  • Download URL: pygeoif-1.0b4.tar.gz
  • Upload date:
  • Size: 41.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pygeoif-1.0b4.tar.gz
Algorithm Hash digest
SHA256 fbe68e596b98e4effa82fdc09e7c17343fed699d813796b1ecdaf28be02015a8
MD5 3f9f26780c4d51e7c51ea6ebb2ec3571
BLAKE2b-256 d0364a676a24df352a236561a18febc34786cb7a4d88d9646058685c9b6c6312

See more details on using hashes here.

File details

Details for the file pygeoif-1.0b4-py3-none-any.whl.

File metadata

  • Download URL: pygeoif-1.0b4-py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pygeoif-1.0b4-py3-none-any.whl
Algorithm Hash digest
SHA256 702584ce8b7e1a28d428c7d88b1d396fcb3de0feb45a7b6f8f4f0e470d346204
MD5 2ee847545ab2e0396e5d4b020f0f5a48
BLAKE2b-256 f829fecd06b049cfdb65deb50a6237b15d223019736c20b7d4feed32b25c5c00

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