fiona读写空间数据文件

Fiona的Python项目详细描述


用法

收藏

记录从 文件中读取并写入 -类似于 集合的 对象 从 fiona.open() 函数返回。记录是基于 geojson格式。他们没有自己的空间方法,所以如果你 想用它们做任何花哨的事你可能需要漂亮的或其他的东西 喜欢它。下面是一个使用fiona从一个数据中读取一些记录的示例 文件,更改其几何属性,并将其写入新的数据文件。

importfiona# Open a file for reading. We'll call this the "source."withfiona.open('tests/data/coutwildrnp.shp')assrc:# The file we'll write to, the "destination", must be initialized# with a coordinate system, a format driver name, and# a record schema.  We can get initial values from the open# collection's ``meta`` property and then modify them as# desired.meta=src.metameta['schema']['geometry']='Point'# Open an output file, using the same format driver and# coordinate reference system as the source. The ``meta``# mapping fills in the keyword parameters of fiona.open().withfiona.open('test_write.shp','w',**meta)asdst:# Process only the records intersecting a box.forfinsrc.filter(bbox=(-107.0,37.0,-105.0,39.0)):# Get a point on the boundary of the record's# geometry.f['geometry']={'type':'Point','coordinates':f['geometry']['coordinates'][0][0]}# Write the record out.dst.write(f)# The destination's contents are flushed to disk and the file is# closed when its ``with`` block ends. This effectively# executes ``dst.flush(); dst.close()``.

读取多层数据

集合也可以由多层文件中的单层或 数据目录。目标层由名称或其整数指定 文件或目录中的索引。函数 提供按索引排序的图层名列表。

forlayernameinfiona.listlayers('tests/data'):withfiona.open('tests/data',layer=layername)assrc:print(layername,len(src))# Output:# (u'coutwildrnp', 67)

层也可以通过索引指定。在这种情况下, 层=0 layer='test_uk' 在数据文件或目录中指定相同的层。

fori,layernameinenumerate(fiona.listlayers('tests/data')):withfiona.open('tests/data',layer=i)assrc:print(i,layername,len(src))# Output:# (0, u'coutwildrnp', 67)

写入多层数据

多层数据也可以写入。当 写作。

withopen('tests/data/cowildrnp.shp')assrc:meta=src.metaf=next(src)withfiona.open('/tmp/foo','w',layer='bar',**meta)asdst:dst.write(f)print(fiona.listlayers('/tmp/foo'))withfiona.open('/tmp/foo',layer='bar')assrc:print(len(src))f=next(src)print(f['geometry']['type'])print(f['properties'])# Output:# [u'bar']# 1# Polygon# OrderedDict([(u'PERIMETER', 1.22107), (u'FEATURE2', None), (u'NAME', u'Mount Naomi Wilderness'), (u'FEATURE1', u'Wilderness'), (u'URL', u'http://www.wilderness.net/index.cfm?fuse=NWPS&sec=wildView&wname=Mount%20Naomi'), (u'AGBUR', u'FS'), (u'AREA', 0.0179264), (u'STATE_FIPS', u'49'), (u'WILDRNP020', 332), (u'STATE', u'UT')])

/tmp/foo目录的视图将确认新文件的创建。

$ ls /tmp/foo
bar.cpg bar.dbf bar.prj bar.shp bar.shx

来自存档和虚拟文件系统的集合

zip和tar存档可以视为虚拟文件系统,而集合可以 由它们内部的路径和层构成。换句话说,菲奥娜让你阅读 并编写压缩的形状文件。

fori,layernameinenumerate(fiona.listlayers('zip://tests/data/coutwildrnp.zip'):withfiona.open('zip://tests/data/coutwildrnp.zip',layer=i)assrc:print(i,layername,len(src))# Output:# (0, u'coutwildrnp', 67)

fiona还可以从更奇特的文件系统中读取数据。法罗群岛R实例,A s3中的压缩形状文件可以这样访问:

withfiona.open('zip+s3://mapbox/rasterio/coutwildrnp.zip')assrc:print(len(src))# Output:# 67

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaJSonarray不会从SeekBar读取double   使用另一个类从Java中的2D数组打印用户输入   java ClassNotFoundException的原因   spring调用两个方法以返回Java中的不同页面   httpurlconnection Java禁止的代码错误,但浏览器错误(2)   java画布矩阵转换   java:在另一个java映射中使用“Map”作为值   java“未找到用于解密的证书”(Apache CXF,WSSecurity)   java如何查看JTable中选择的行   java在没有xmlwrappers的情况下重复xml元素序列集   java将垂直直方图打印到控制台   java Spring JDBCTemplate:构造不带特殊字符的JSON   java PayPal RestApi获取用户信息