Python bindings and utilities for GeoJSON


Keywords
gis, geography, json, geojson, python
License
BSD-3-Clause
Install
pip install geojson==1.3.2

Documentation

geojson

GitHub Actions

Codecov

Jazzband

PyPI

This Python library contains:

Table of Contents

Installation

geojson is compatible with Python 3.7 - 3.12. The recommended way to install is via pip:

pip install geojson

GeoJSON Objects

This library implements all the GeoJSON Objects described in The GeoJSON Format Specification.

All object keys can also be used as attributes.

The objects contained in GeometryCollection and FeatureCollection can be indexed directly.

Point

Visualize the result of the example above here. General information about Point can be found in Section 3.1.2 and Appendix A: Points within The GeoJSON Format Specification.

MultiPoint

Visualize the result of the example above here. General information about MultiPoint can be found in Section 3.1.3 and Appendix A: MultiPoints within The GeoJSON Format Specification.

LineString

Visualize the result of the example above here. General information about LineString can be found in Section 3.1.4 and Appendix A: LineStrings within The GeoJSON Format Specification.

MultiLineString

Visualize the result of the example above here. General information about MultiLineString can be found in Section 3.1.5 and Appendix A: MultiLineStrings within The GeoJSON Format Specification.

Polygon

Visualize the results of the example above here. General information about Polygon can be found in Section 3.1.6 and Appendix A: Polygons within The GeoJSON Format Specification.

MultiPolygon

Visualize the result of the example above here. General information about MultiPolygon can be found in Section 3.1.7 and Appendix A: MultiPolygons within The GeoJSON Format Specification.

GeometryCollection

Visualize the result of the example above here. General information about GeometryCollection can be found in Section 3.1.8 and Appendix A: GeometryCollections within The GeoJSON Format Specification.

Feature

Visualize the results of the examples above here. General information about Feature can be found in Section 3.2 within The GeoJSON Format Specification.

FeatureCollection

Visualize the result of the example above here. General information about FeatureCollection can be found in Section 3.3 within The GeoJSON Format Specification.

GeoJSON encoding/decoding

All of the GeoJSON Objects implemented in this library can be encoded and decoded into raw GeoJSON with the geojson.dump, geojson.dumps, geojson.load, and geojson.loads functions. Note that each of these functions is a wrapper around the core json function with the same name, and will pass through any additional arguments. This allows you to control the JSON formatting or parsing behavior with the underlying core json functions.

Custom classes

This encoding/decoding functionality shown in the previous can be extended to custom classes using the interface described by the __geo_interface__ Specification.

Default and custom precision

GeoJSON Object-based classes in this package have an additional precision attribute which rounds off coordinates to 6 decimal places (roughly 0.1 meters) by default and can be customized per object instance.

Precision can be set at the package level by setting geojson.geometry.DEFAULT_PRECISION

After setting the DEFAULT_PRECISION, coordinates will be rounded off to that precision with geojson.load or geojson.loads. Following one of those with geojson.dump is a quick and easy way to scale down the precision of excessively precise, arbitrarily-sized GeoJSON data.

Helpful utilities

coords

geojson.utils.coords yields all coordinate tuples from a geometry or feature object.

map_coords

geojson.utils.map_coords maps a function over all coordinate values and returns a geometry of the same type. Useful for scaling a geometry.

map_tuples

geojson.utils.map_tuples maps a function over all coordinates and returns a geometry of the same type. Useful for changing coordinate order or applying coordinate transforms.

map_geometries

geojson.utils.map_geometries maps a function over each geometry in the input.

validation

is_valid property provides simple validation of GeoJSON objects.

errors method provides collection of errors when validation GeoJSON objects.

generate_random

geojson.utils.generate_random yields a geometry type with random data

Development

To build this project, run python setup.py build. To run the unit tests, run python -m pip install tox && tox. To run the style checks, run flake8 (install flake8 if needed).

Credits