方便的地理空间数据处理。
meridian的Python项目详细描述
子午线
python的高级地理空间数据抽象。
meridian
允许您像对待普通python数据结构一样对待地理空间数据集,并由用于高性能地理空间查询的空间索引支持。
用法
所实现的核心数据结构是SpatialDataset
,它接受GeoJSON-结构化dict
s的iterable(list
,generator
)并建立数据集和索引。数据集中的记录存储在SpatialDataset
as SpatialData
对象中,geojson的properties
中的所有键/值对都是属性。加载完成后,您将拥有一个熟悉的感觉数据结构,可以用来查询数据集。
meridian
与fiona
库生成的类似geojson的对象完全兼容,这使得启动变得非常容易:
importfionafrommeridianimportSpatialDatasetfromshapelyimportgeometrywithfiona.open('/path/to/my/shapefile.shp')assrc:dataset=SpatialDataset(src)# Find out how many records you haveprint(len(dataset))poi=geometry.shape({'type':'Point','coordinates':[-72.319261,43.648956]})# Check if your poi intersects with the datasetprint(dataset.intersects(poi))# True# See how many records intersectprint(dataset.count(poi))# 1# Find the n nearest records to the query geometryprint(dataset.nearest(poi,3))# The dataset itself is iterable.forrecordindataset:print(record)# iterate through all records in the dataset which bbox-intersect with poi# dataset.intersection returns a list of spatialdata objectsforrecordindataset.intersection(poi):print(record)
一个SpatialDataset
上的所有空间查询方法只需要查询对象有一个bounds
属性,该属性返回一个类似(xmin, ymin, xmax, ymax)
的4元组。只要存在,^ {< CD1> }是查询几何实现的不可知的,但是它在引擎盖下使用^ {CD15> }几何图形来记录存储在其中的记录。
aSpatialDataset
中的记录是SpatialData
s:
poi=geometry.shape({'type':'Point','coordinates':[-72.319261,43.648956]})forrecordindataset:print(record.id)# The id in the `id` field of the input geojsonprint(record.geom)# The `shapely` geometry representation of the recordprint(record.bounds)# The bounds of the geometryprint(record.properties)# a dict of all the `properties` in the initial geojson featureprint(record.my_fancy_property)# All individual properties in the geojson feature will be exposed as attributes on the namedtuple# SpatialData objects are fully compatible with all of the objects & operations defined in the shapely package.print(record.intersects(poi))print(poi.intersects(record))# Even advanced operations like cascaded union work as expected.fromshapely.opsimportcascaded_unionunioned=cascaded_union(dataset)print(unioned.wkt)
由于id
字段不是geojson规范的一部分,因此可以选择包含它;没有它,库将正常工作。但是,它确实为用户提供了唯一标识每个数据集中记录的方法。
安装
来自pypi
:
pip install meridian
或者,克隆回购协议并运行
python path/to/repo/setup.py install
您还可以使用pip
直接从github repo安装:
pip install git+git://github.com/tomplex/meridian.git
meridian
需要geos(用于shapely
库)和^{libspatialindex
必须从源代码编译。这些说明应适用于Linux&MacOS:
wget -qO- http://download.osgeo.org/libspatialindex/spatialindex-src-1.8.5.tar.gz | tar xz -C /tmp cd /tmp/spatialindex-src-1.8.5 && ./configure; make; make install
在linux上,您可能需要在之后运行ldconfig
,以确保rtree
python库能够正确地找到库。
如果使用Docker,则会在docker hub上预安装有具有所有依赖项和最新版本meridian
的图像。
找到了
数据占用内存。根据尝试使用的几何图形的数量和大小,可能会耗尽内存。在我的机器2016 MacBook Pro上,我发现一个有350k条记录的数据集,每个多边形平均有6个节点,占用了大约500MB的内存。YMMV公司。
meridian
固执己见,认为数据应该是不可变的。如果需要更改数据,则应创建表示输入+处理的新数据,而不是更改旧数据。因此,SpatialDataset
在行为上更像是frozenset
,而不是list
。
计划功能
- 深入的文档和使用示例
- 格式化compat。内置工具,帮助从其他格式(Postgres、WKT等)加载数据。