npm包geojsplit的python实现。用于将GeoJSON文件拆分为更小的部分。
geojsplit的Python项目详细描述
土工格栅
节点包geojsplit:https://github.com/woodb/geojsplit的python实现
安装
用诗歌
$ poetry add geojsplit
将向当前虚拟环境添加geojsplit并更新诗.锁文件。如果您想贡献或开发geojsplit
^{pr2}$您可能需要一些额外的配置,使诗歌更好地发挥康达虚拟环境
poetry config settings.virtualenvs.path <path_to_conda_install>/envs # tell poetry where you virtualenvs are stored
poetry config settings.virtualenvs.create 0 # tell poetry not to try to create its own virtualenvs.
有关详细信息,请参见https://github.com/sdispater/poetry/issues/105#issuecomment-498042062。在
$ poetry config settings.virtualenvs.path $CONDA_ENV_PATH
$ poetry config settings.virtualenvs.create 0
有pip
尽管geojsplit是使用poetry开发的(因此没有设置.py)在pip中,pep517实现意味着我们可以直接安装它
$ pip install geojsplit
使用
尽管geojsplit的库代码和命令行工具都相对简单,但它们都有各自的用例。您可能希望直接使用后端GeoJSONBatchStreamer
类,以便对GeoJOSN文档进行更复杂的操作。作为一个命令行工具,geojsplit还可以作为处理大型GeoJSON文档的预处理步骤,即管道导入GDAL的ogr2ogr工具。在
作为一个图书馆
一旦安装,geojsplit就可以像
from geojsplit import geojsplit
geojson = geojsplit.GeoJSONBatchStreamer("/path/to/some.geojson")
for feature_collection in geojson.stream():
do_something(feature_collection)
...
如果/path/to/some.geojson
不存在,FileNotFound
将被引发。在
可以使用批处理参数(默认值为100)控制流式处理到要素集合中的要素数量。在
>>> g = geojson.stream(batch=2) # instatiate generator object
>>> data = next(g)
>>> print(data)
{"features": [{"geometry": {"coordinates": [[[-118.254638, 33.7843], [-118.254637,
33.784231], [-118.254556, 33.784232], [-118.254559, 33.784339], [-118.254669,
33.784338], [-118.254668, 33.7843], [-118.254638, 33.7843]]], "type": "Polygon"},
"properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [[[-118.254414,
33.784255], [-118.254232, 33.784255], [-118.254232, 33.784355], [-118.254414,
33.784355], [-118.254414, 33.784255]]], "type": "Polygon"}, "properties": {}, "type":
"Feature"}], "type": "FeatureCollection"}
>>> print(len(data["features"]))
2
如果GeoJSON文档具有不同的格式,或者希望迭代文档中的不同元素,也可以将不同的值传递给prefix
关键字参数(默认值为'features.item'
)。这是一个直接传递给ijson.items
调用的参数,有关更多信息,请参见https://github.com/ICRAR/ijson。在
作为命令行工具
安装后,您的PATH
中应该有geojsplit可执行文件。在
$ geojsplit -h
usage: geojsplit [-h] [-l GEOMETRY_COUNT] [-a SUFFIX_LENGTH] [-o OUTPUT]
[-n LIMIT] [-v] [-d] [--version]
geojson
Split a geojson file into many geojson files.
positional arguments:
geojson filename of geojson file to split
optional arguments:
-h, --help show this help message and exit
-l GEOMETRY_COUNT, --geometry-count GEOMETRY_COUNT
the number of features to be distributed to each file.
-a SUFFIX_LENGTH, --suffix-length SUFFIX_LENGTH
number of characters in the suffix length for split
geojsons
-o OUTPUT, --output OUTPUT
output directory to save split geojsons
-n LIMIT, --limit LIMIT
limit number of split geojson file to at most LIMIT,
with GEOMETRY_COUNT number of features.
-v, --verbose increase output verbosity
-d, --dry-run see output without actually writing to file
--version show geojsplit version number
默认情况下,分割的GeoJSON文件保存为filename_x<SUFFIX_LENGTH characters long>.geojson
。默认后缀长度为4,这意味着可以生成456976个唯一的文件。如果需要更多,请使用-a
或--suffix-length
适当地增加该值。在
--geometry-count
标志对应于GeoJSONBatchStreamer.stream
方法的批处理关键字参数。请注意,如果几何图形计数不等于要素集合中的要素数量,则最后一批要素将是<;几何图形计数。在
最后,要只迭代GeoJSON文档的前n个元素,请使用--limit
。在
- 项目
标签: