跳转到主要内容

GeoJSON的Python绑定和实用工具

项目描述

GitHub Actions Codecov Jazzband

此Python库包含

目录

安装

geojson 与 Python 3.7 - 3.12 兼容。推荐的安装方法是使用 pip

pip install geojson

GeoJSON对象

该库实现了 RFC 7946 中描述的所有 GeoJSON 对象,见 GeoJSON 格式规范

所有对象键也可以用作属性。

GeometryCollection 和 FeatureCollection 中包含的对象可以直接索引。

>>> from geojson import Point

>>> Point((-115.81, 37.24))  # doctest: +ELLIPSIS
{"coordinates": [-115.8..., 37.2...], "type": "Point"}

上述示例的结果可视化请见 此处。关于 Point 的一般信息,请参阅 第 3.1.2 节附录 A:点,见 GeoJSON 格式规范

多点

>>> from geojson import MultiPoint

>>> MultiPoint([(-155.52, 19.61), (-156.22, 20.74), (-157.97, 21.46)])  # doctest: +ELLIPSIS
{"coordinates": [[-155.5..., 19.6...], [-156.2..., 20.7...], [-157.9..., 21.4...]], "type": "MultiPoint"}

上述示例的结果可视化请见 此处。关于 MultiPoint 的一般信息,请参阅 第 3.1.3 节附录 A:多点,见 GeoJSON 格式规范

线字符串

>>> from geojson import LineString

>>> LineString([(8.919, 44.4074), (8.923, 44.4075)])  # doctest: +ELLIPSIS
{"coordinates": [[8.91..., 44.407...], [8.92..., 44.407...]], "type": "LineString"}

上述示例的结果可视化请见 此处。关于 LineString 的一般信息,请参阅 第 3.1.4 节附录 A:线字符串,见 GeoJSON 格式规范

多线字符串

>>> from geojson import MultiLineString

>>> MultiLineString([
...     [(3.75, 9.25), (-130.95, 1.52)],
...     [(23.15, -34.25), (-1.35, -4.65), (3.45, 77.95)]
... ])  # doctest: +ELLIPSIS
{"coordinates": [[[3.7..., 9.2...], [-130.9..., 1.52...]], [[23.1..., -34.2...], [-1.3..., -4.6...], [3.4..., 77.9...]]], "type": "MultiLineString"}

上述示例的结果可视化请见 此处。关于 MultiLineString 的一般信息,请参阅 第 3.1.5 节附录 A:多线字符串,见 GeoJSON 格式规范

多边形

>>> from geojson import Polygon

>>> # no hole within polygon
>>> Polygon([[(2.38, 57.322), (-120.43, 19.15), (23.194, -20.28), (2.38, 57.322)]])  # doctest: +ELLIPSIS
{"coordinates": [[[2.3..., 57.32...], [-120.4..., 19.1...], [23.19..., -20.2...]]], "type": "Polygon"}

>>> # hole within polygon
>>> Polygon([
...     [(2.38, 57.322), (-120.43, 19.15), (23.194, -20.28), (2.38, 57.322)],
...     [(-5.21, 23.51), (15.21, -10.81), (-20.51, 1.51), (-5.21, 23.51)]
... ])  # doctest: +ELLIPSIS
{"coordinates": [[[2.3..., 57.32...], [-120.4..., 19.1...], [23.19..., -20.2...]], [[-5.2..., 23.5...], [15.2..., -10.8...], [-20.5..., 1.5...], [-5.2..., 23.5...]]], "type": "Polygon"}

上述示例的结果可视化请见 此处。关于 Polygon 的一般信息,请参阅 第 3.1.6 节附录 A:多边形,见 GeoJSON 格式规范

多边形集合

>>> from geojson import MultiPolygon

>>> MultiPolygon([
...     ([(3.78, 9.28), (-130.91, 1.52), (35.12, 72.234), (3.78, 9.28)],),
...     ([(23.18, -34.29), (-1.31, -4.61), (3.41, 77.91), (23.18, -34.29)],)
... ])  # doctest: +ELLIPSIS
{"coordinates": [[[[3.7..., 9.2...], [-130.9..., 1.5...], [35.1..., 72.23...]]], [[[23.1..., -34.2...], [-1.3..., -4.6...], [3.4..., 77.9...]]]], "type": "MultiPolygon"}

上述示例的结果可视化请见 此处。关于 MultiPolygon 的一般信息,请参阅 第 3.1.7 节附录 A:多边形集合,见 GeoJSON 格式规范

几何集合

>>> from geojson import GeometryCollection, Point, LineString

>>> my_point = Point((23.532, -63.12))

>>> my_line = LineString([(-152.62, 51.21), (5.21, 10.69)])

>>> geo_collection = GeometryCollection([my_point, my_line])

>>> geo_collection  # doctest: +ELLIPSIS
{"geometries": [{"coordinates": [23.53..., -63.1...], "type": "Point"}, {"coordinates": [[-152.6..., 51.2...], [5.2..., 10.6...]], "type": "LineString"}], "type": "GeometryCollection"}

>>> geo_collection[1]
{"coordinates": [[-152.62, 51.21], [5.21, 10.69]], "type": "LineString"}

>>> geo_collection[0] == geo_collection.geometries[0]
True

上述示例的结果可视化请见 此处。关于 GeometryCollection 的一般信息,请参阅 第 3.1.8 节附录 A:几何集合,见 GeoJSON 格式规范

要素

>>> from geojson import Feature, Point

>>> my_point = Point((-3.68, 40.41))

>>> Feature(geometry=my_point)  # doctest: +ELLIPSIS
{"geometry": {"coordinates": [-3.68..., 40.4...], "type": "Point"}, "properties": {}, "type": "Feature"}

>>> Feature(geometry=my_point, properties={"country": "Spain"})  # doctest: +ELLIPSIS
{"geometry": {"coordinates": [-3.68..., 40.4...], "type": "Point"}, "properties": {"country": "Spain"}, "type": "Feature"}

>>> Feature(geometry=my_point, id=27)  # doctest: +ELLIPSIS
{"geometry": {"coordinates": [-3.68..., 40.4...], "type": "Point"}, "id": 27, "properties": {}, "type": "Feature"}

上述示例的结果可视化请见 此处。关于 Feature 的一般信息,请参阅 第 3.2 节,见 GeoJSON 格式规范

要素集合

>>> from geojson import Feature, Point, FeatureCollection

>>> my_feature = Feature(geometry=Point((1.6432, -19.123)))

>>> my_other_feature = Feature(geometry=Point((-80.234, -22.532)))

>>> feature_collection = FeatureCollection([my_feature, my_other_feature])

>>> feature_collection # doctest: +ELLIPSIS
{"features": [{"geometry": {"coordinates": [1.643..., -19.12...], "type": "Point"}, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [-80.23..., -22.53...], "type": "Point"}, "properties": {}, "type": "Feature"}], "type": "FeatureCollection"}

>>> feature_collection.errors()
[]

>>> (feature_collection[0] == feature_collection['features'][0], feature_collection[1] == my_other_feature)
(True, True)

上述示例的结果可视化请见 此处。关于 FeatureCollection 的一般信息,请参阅 第 3.3 节,见 GeoJSON 格式规范

GeoJSON编码/解码

本库中实现的GeoJSON对象都可以通过geojson.dumpgeojson.dumpsgeojson.loadgeojson.loads函数进行编码和解码为原始GeoJSON。注意,这些函数都是围绕同名核心json函数的包装器,并将传递任何额外的参数。这允许您使用底层核心json函数控制JSON格式化或解析行为。

>>> import geojson

>>> my_point = geojson.Point((43.24, -1.532))

>>> my_point  # doctest: +ELLIPSIS
{"coordinates": [43.2..., -1.53...], "type": "Point"}

>>> dump = geojson.dumps(my_point, sort_keys=True)

>>> dump  # doctest: +ELLIPSIS
'{"coordinates": [43.2..., -1.53...], "type": "Point"}'

>>> geojson.loads(dump)  # doctest: +ELLIPSIS
{"coordinates": [43.2..., -1.53...], "type": "Point"}

自定义类

前面展示的编码/解码功能可以通过__geo_interface__规范描述的接口扩展到自定义类。

>>> import geojson

>>> class MyPoint():
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...
...     @property
...     def __geo_interface__(self):
...         return {'type': 'Point', 'coordinates': (self.x, self.y)}

>>> point_instance = MyPoint(52.235, -19.234)

>>> geojson.dumps(point_instance, sort_keys=True)  # doctest: +ELLIPSIS
'{"coordinates": [52.23..., -19.23...], "type": "Point"}'

默认和自定义精度

本包基于GeoJSON对象类的类具有一个额外的precision属性,默认将坐标四舍五入到6位小数(大约0.1米),并且可以针对每个对象实例进行自定义。

>>> from geojson import Point

>>> Point((-115.123412341234, 37.123412341234))  # rounded to 6 decimal places by default
{"coordinates": [-115.123412, 37.123412], "type": "Point"}

>>> Point((-115.12341234, 37.12341234), precision=8)  # rounded to 8 decimal places
{"coordinates": [-115.12341234, 37.12341234], "type": "Point"}

可以通过设置geojson.geometry.DEFAULT_PRECISION在包级别设置精度。

>>> import geojson

>>> geojson.geometry.DEFAULT_PRECISION = 5

>>> from geojson import Point

>>> Point((-115.12341234, 37.12341234))  # rounded to 8 decimal places
{"coordinates": [-115.12341, 37.12341], "type": "Point"}

设置DEFAULT_PRECISION后,坐标将使用geojson.loadgeojson.loads四舍五入到该精度。在那些之后跟随geojson.dump是一种快速便捷的方法来缩小过度精确的、任意尺寸的GeoJSON数据精度。

有用的实用工具

coords

geojson.utils.coords从几何或要素对象中产生所有坐标元组。

>>> import geojson

>>> my_line = LineString([(-152.62, 51.21), (5.21, 10.69)])

>>> my_feature = geojson.Feature(geometry=my_line)

>>> list(geojson.utils.coords(my_feature))  # doctest: +ELLIPSIS
[(-152.62..., 51.21...), (5.21..., 10.69...)]

map_coords

geojson.utils.map_coords在所有坐标值上映射一个函数,并返回相同类型的几何体。对于缩放几何体非常有用。

>>> import geojson

>>> new_point = geojson.utils.map_coords(lambda x: x/2, geojson.Point((-115.81, 37.24)))

>>> geojson.dumps(new_point, sort_keys=True)  # doctest: +ELLIPSIS
'{"coordinates": [-57.905..., 18.62...], "type": "Point"}'

map_tuples

geojson.utils.map_tuples在所有坐标上映射一个函数,并返回相同类型的几何体。对于改变坐标顺序或应用坐标变换非常有用。

>>> import geojson

>>> new_point = geojson.utils.map_tuples(lambda c: (c[1], c[0]), geojson.Point((-115.81, 37.24)))

>>> geojson.dumps(new_point, sort_keys=True)  # doctest: +ELLIPSIS
'{"coordinates": [37.24..., -115.81], "type": "Point"}'

map_geometries

geojson.utils.map_geometries在输入中的每个几何体上映射一个函数。

>>> import geojson

>>> new_point = geojson.utils.map_geometries(lambda g: geojson.MultiPoint([g["coordinates"]]), geojson.GeometryCollection([geojson.Point((-115.81, 37.24))]))

>>> geojson.dumps(new_point, sort_keys=True)
'{"geometries": [{"coordinates": [[-115.81, 37.24]], "type": "MultiPoint"}], "type": "GeometryCollection"}'

验证

is_valid属性提供对GeoJSON对象的简单验证。

>>> import geojson

>>> obj = geojson.Point((-3.68,40.41,25.14,10.34))
>>> obj.is_valid
False

errors方法在验证GeoJSON对象时提供错误集合。

>>> import geojson

>>> obj = geojson.Point((-3.68,40.41,25.14,10.34))
>>> obj.errors()
'a position must have exactly 2 or 3 values'

generate_random

geojson.utils.generate_random产生具有随机数据的几何体类型。

>>> import geojson

>>> geojson.utils.generate_random("LineString")  # doctest: +ELLIPSIS
{"coordinates": [...], "type": "LineString"}

>>> geojson.utils.generate_random("Polygon")  # doctest: +ELLIPSIS
{"coordinates": [...], "type": "Polygon"}

开发

要构建此项目,请运行python setup.py build。要运行单元测试,请运行python -m pip install tox && tox。要运行样式检查,请运行flake8(如果需要,请安装flake8)。

鸣谢

项目详情


下载文件

下载您平台上的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源代码分发

geojson-3.1.0.tar.gz (24.5 kB 查看哈希值)

上传时间 源代码

构建分发

geojson-3.1.0-py3-none-any.whl (15.0 kB 查看哈希值)

上传时间 Python 3

由以下提供支持