一个简单的python geojson文件读写器。
PyGeoj的Python项目详细描述
pygeoj是一个简单的python-geojson文件读取器和编写器,用于 最终用户。它将字典结构公开为 方便的方法,所以用户不必陷入 格式规范的详细信息。
平台
Python2和3。
依赖关系
纯python,没有依赖关系。
安装它
pygeoj是用命令行中的pip安装的:
pip install pygeoj
它还可以将“pygeoj”包文件夹放在importable中。 位置类似“pythonxx/lib/site packages”。
示例用法
首先导入pygeoj模块:
import pygeoj
读数
读取geojson格式的gis文件是一个简单的单行程序(需要 geojson文件将成为“featurecollection”):
testfile = pygeoj.load(filepath="testfile.geojson") # or testfile = pygeoj.load(data=dict(...))
然后可以提取有关geojson文件的基本信息,例如:
len(testfile) # the number of features testfile.bbox # the bounding box region of the entire file testfile.crs # the coordinate reference system testfile.all_attributes # retrieves the combined set of all feature attributes testfile.common_attributes # retrieves only those field attributes that are common to all features
可以通过功能列表中的索引访问各个功能:
testfile[3] # or testfile.get_feature(3)
或者通过迭代所有这些函数:
for feature in testfile: # do something
可以通过多种方式检查功能:
feature.properties feature.geometry.type feature.geometry.coordinates feature.geometry.bbox
编辑
标准的python列表操作可用于编辑和交换 geojson实例中的特性,然后保存到新的geojson 文件:
testfile[3] = testfile[8] # or testfile.replace_feature(3, testfile[8]) del testfile[8] # or testfile.remove_feature(8) testfile.save("test_edit.geojson")
一个现有的特征也可以用简单的方法来调整。 属性设置:
# set your own properties feature.properties = {"newfield1":"newvalue1", "newfield2":"newvalue2"} # borrow the geometry of the 16th feature feature.geometry = testfile[16].geometry
请注意,更改几何图形或坐标时,必须记住 更新bbox以清除任何旧的bbox存储信息。
feature.geometry.update_bbox()
构造
从头开始创建新的geojson文件也很容易:
newfile = pygeoj.new() # The data coordinate system defaults to long/lat WGS84 or can be manually defined: newfile.define_crs(type="link", link="http://spatialreference.org/ref/epsg/26912/esriwkt/", link_type="esriwkt")
然后可以用新功能填充新文件:
newfile.add_feature(properties={"country":"Norway"}, geometry={"type":"Polygon", "coordinates":[[(21,3),(33,11),(44,22)]]} ) newfile.add_feature(properties={"country":"USA"}, geometry={"type":"Polygon", "coordinates":[[(11,23),(14,5),(66,31)]]} )
最后,一些有用的附加信息可以添加到 将geojson文件保存到文件之前:
newfile.add_all_bboxes() newfile.update_bbox() newfile.add_unique_id() newfile.save("test_construct.geojson")
许可证:
根据麻省理工学院的规定,这些代码可以自由共享、使用、重用和修改 许可证,请参见license.txt
学分:
- 卡里姆·巴加特
- mec是
更改
1.0.0(2018-09-13)
- 凹凸到稳定版本
- 正式支持Python3
0.2.5(2017-02-19)
- 修正了更可靠的验证以避免意外错误
- 添加了skiperror选项
- 修正了add_feature()时缺少的专长类型
- 固定CRS不保存
- 加载和验证时添加了fixerror选项
- 修复错误以允许空几何图形和空属性,并正确地 用json表示为空
0.2.4(2015-07-11)
- 用AddiaLalbBxSe()不修复现有BBase 的固定错误
- 修正了geojsonfile bbox有时计算错误的错误。
- 在单个几何体对象上添加了update_bbox()。