Python Fiona 模块2024 年 8 月 29 日 | 阅读 11 分钟 Fiona 允许 Python 开发者通过读写地理数据文件来与地理信息系统集成到其他计算机系统中。Fiona 包含扩展模块,可将地理空间数据抽象库 (GDAL) 连接到其他应用程序。Fiona 的设计易于使用且可靠。它专注于使用常见的 Python IO 范例读写数据,并使用基本 Python 类型和协议(如文件、字典、映射和迭代器)而非 OGR 特定的类。Fiona 可以读写多层 GIS 格式的真实世界数据以及压缩的虚拟文件系统,并且易于与其他 Python GIS 工具(如 pyproj、Rtree 和 Shapely)一起使用。 Fiona 是 OGR API。它可以以多种格式读写数据。选择它而不是 OGR 的主要原因是它更像 Python,更可靠,并且不易出错。它使用 WKT 和 WKB 标记语言来传达与矢量数据相关的空间信息。因此,它与其他 Python 库(如 Shapely)配合得很好。您将使用 Fiona 进行输入和输出,使用 Shapely 进行地理空间数据创建和操作。 Fiona 模块的必要性地理信息系统 (GIS) 帮助我们预测、响应和理解我们物理、政治、经济和文化景观的变化。GIS 曾经只有国家和城市等大型机构才能使用,但由于精确且廉价的世界定位系统、卫星数据的商品化以及开源软件,现在它已经很普遍了。在 GIS 中,栅格表示连续标量场(例如,地表温度或高程),而矢量表示离散要素,如道路和行政边界。Fiona 仅关注此。它是 OGR 库的矢量数据访问方法的 Python 包装器。对于极简主义者来说,这是一个非常基本的包装器。它从文件中读取 GeoJSON 类的映射,并将相同的映射作为记录发布回文件。讨论到此结束。图层、游标、几何操作、坐标系转换和远程方法调用都由 Shapely 和 pyproj 等其他 Python 包以及 Python 语言协议处理。 请注意:Fiona 旨在成功完成特定的任务集,而在其他方面则表现不佳。Fiona 以牺牲内存和速度为代价来换取简单性和一致性。Fiona 将矢量数据从数据源转换为 Python 对象,而 OGR 的 Python 绑定使用 C 指针。这些更容易操作且更安全,但会占用更多内存。如果您只需要访问单个记录字段——并且没有什么比 ogr2ogr 程序更能重新投影或过滤数据文件了——Fiona 的性能会慢一些,但如果您需要记录的所有字段和坐标,Fiona 的性能比 OGR 的 Python 绑定要好得多。虽然复制是一个限制,但它使程序更容易理解。Fiona 消除了管理 C 对象引用的需要,以避免崩溃,并允许您使用 Python 映射访问器与矢量数据进行交互。在地理信息系统中,记录用于表示离散的地理特征。记录特征的语义含义已得到充分认识 [Kent1978]。记录具有单一类型,所有该类型的记录都具有相同的字段,并且记录的字段涉及单个地理属性,这对地理数据来说是最重要的。不同的系统以不同的方式描述记录,但它们之间存在足够的相似性,以至于程序员能够开发出有效的抽象数据模型。其中之一是 OGR 模型。数据源、图层和要素是三个主要组成部分。要素包含属性和几何图形,而不是字段。OGR 图层仅包含一种要素(例如,“道路”或“井”)。GeoJSON 范例稍微简单一些,其中要素取代了 OGR 数据源和图层,要素集合取代了 OGR 数据源和图层。因此,“要素”一词在 GIS 建模中被过度使用,因为它同时指代了我们的概念模型和数据模型中的实体。记录文件有多种格式。直到 2005 年左右,ESRI Shapefile [ESRI1998] 还是其中最重要的,至少在美国是这样,而且至今仍很受欢迎。它是一种二进制文件类型。形状字段保存在 a.shapefile 中,而其他字段保存在 a.dbf 文件中。 现在让我们看一下 Python 中 Fiona 模块的代码。 代码输出 These are the below-listed options, select any one of them:: 1. To read data from a shapefile(.shp / .shx file) using the Fiona module of python. 2. To get the schema of the specified shapefile(.shp / .shx file) using the Fiona module of python. 3. To get the bounds of the specified shapefile(.shp / .shx file) using the Fiona module of python. 4. To get the CRS of the specified shapefile(.shp / .shx file) using the Fiona module of python. 5. To check whether the specified shapefile(.shp / .shx file) is closed or not. 6. To stop displaying options and exit from the code. 1 Enter the name of the file(.shp file):: IND_adm0.shx First data in the file is:: code1.py:18: FionaDeprecationWarning: Collection.__next__() is buggy and will be removed in Fiona 2.0. Switch to `next(iter(collection))`. print(next(opne_handler)) {'type': 'Feature', 'id': '0', 'properties': OrderedDict(), 'geometry': {'type': 'MultiPolygon', 'coordinates': [[[(93.78772735595709, 6.852640151977823), (93.78849029541021, 6.8525710105896), (93.78904724121094, 6.8525710105896), (93.78904724121094, 6.852291107178019), (93.78967285156256, 6.852291107178019), (93.78987884521513, 6.852013111114502), (93.79015350341814, 6.851944923400879), (93.79064178466825, 6.851666927337931), (93.79077911376959, 6.851388931274414), (93.79091644287126, 6.851041793823185), (93.79050445556635, 6.8506250381472), (93.79029083251964, 6.850347042083854), (93.79000091552734, 6.85027885437006), (93.78948211669928, 6.849903106689453), (93.78900146484392, 6.849484920501936), (93.78837585449247, 6.849136829376391), (93.78801727294933, 6.848720073700008), (93.78778076171903, 6.848055839538688), (93.78750610351562, 6.848061084747542), (93.78721618652372, 6.847781181335449), (93.78639221191412, 6.847781181335449), (93.78610992431652, 6.848061084747542), (93.78527832031273, 6.848061084747542), (93.78443908691423, 6.848899841308821), (93.7844467163086, 6.849165916442814), (93.78549194335966, 6.849929809570426), (93.78625488281278, 6.8506250381472), (93.78676605224615, 6.851171970367432), (93.78694152832031, 6.8516712188723545), (93.78737640380882, 6.851666927337931), (93.78737640380882, 6.852084159851358), (93.78730773925781, 6.852358818054199), (93.78737640380882, 6.8525710105896), (93.78772735595709, 6.852640151977823)]], [[(93.71958160400408, 7.2075009346010575), (93.71958160400408, 7.206870079040755), (93.71930694580107, 7.206459999084416), (93.93312072753935, 6.970485210418644), (93.93327331542974, 6.969860076904467), (93.93333435058622, 6.96944284439104), (93.933891296387, 6.968889236450195), (93.933891296387, 6.968610763550032), (93.93444824218756, 6.96805477142334), (93.93444824218756, 6.967776775360392), (93.9347229003908, 6.967501163482893), (93.9347229003908, 6.967223167419377), (93.93499755859398, 6.9669451713564285), (93.93499755859398, 6.966111183166618), (93.9347229003908, 6.965833187103499), (93.9347229003908, 6.965001106262264), (93.93497467041021, 6.964021205902327), (93.93497467041021, 6.963535785675276), (93.9352493286134, 6.963325977325724), (93.935600280762, 6.963187217712687), (93.93601226806652, 6.962841033935774), (93.93622589111322, 6.962562084197941), (93.93656921386747, 6.962355136871281), (93.93722534179688, 6.962223052978686), (93.93805694580101, 6.962223052978686), (93.9388885498048, 6.961389064788875), (93.93945312500011, 6.961390972137451), (93.9397201538086, 6.961111068725586), (93.94000244140653, 6.961111068725586), (93.94027709960938, 6.960833072662581), (93.94110870361351, 6.960833072662581), (93.94139099121111, 6.960555076599064), (93.94167327880882, 6.9605607986450195), (93.94194793701166, 6.9602899551393875), (93.94246673584013, 6.959929943084887), (93.94288635253918, 6.95979118347185), The total length of the file opened is 1 Enter [y] else [n], to move ahead with code execution y These are the below-listed options, select any one of them:: 1. To read data from a shapefile(.shp / .shx file) using the Fiona module of python. 2. To get the schema of the specified shapefile(.shp / .shx file) using the Fiona module of python. 3. To get the bounds of the specified shapefile(.shp / .shx file) using the Fiona module of python. 4. To get the crs of the specified shapefile(.shp / .shx file) using the Fiona module of python. 5. To check whether the specified shapefile(.shp / .shx file) is closed or not. 6. To stop displaying options and exit from the code. 2 schema :: {'properties': OrderedDict(), 'geometry': 'Polygon'} Enter [y] else [n], to move ahead with code execution y These are the below-listed options, select any one of them:: 1. To read data from a shapefile(.shp / .shx file) using the Fiona module of python. 2. To get the schema of the specified shapefile(.shp / .shx file) using the Fiona module of python. 3. To get the bounds of the specified shapefile(.shp / .shx file) using the Fiona module of python. 4. To get the CRS of the specified shapefile(.shp / .shx file) using the Fiona module of python. 5. To check whether the specified shapefile(.shp / .shx file) is closed or not. 6. To stop displaying options and exit from the code. 3 bounds :: (68.18624877929699, 6.754255771636906, 97.41516113281256, 35.5013313293457) Enter [y] else [n], to move ahead with code execution y These are the below-listed options, select any one of them:: 1. To read data from a shapefile(.shp / .shx file) using the Fiona module of python. 2. To get the schema of the specified shapefile(.shp / .shx file) using the Fiona module of python. 3. To get the bounds of the specified shapefile(.shp / .shx file) using the Fiona module of python. 4. To get the CRS of the specified shapefile(.shp / .shx file) using the Fiona module of python. 5. To check whether the specified shapefile(.shp / .shx file) is closed or not. 6. To stop displaying options and exit from the code. 4 CRS :: {} Enter [y] else [n], to move ahead with code execution y These are the below-listed options, select any one of them:: 1. To read data from a shapefile(.shp / .shx file) using the Fiona module of python. 2. To get the schema of the specified shapefile(.shp / .shx file) using the Fiona module of python. 3. To get the bounds of the specified shapefile(.shp / .shx file) using the Fiona module of python. 4. To get the CRS of the specified shapefile(.shp / .shx file) using the Fiona module of python. 5. To check whether the specified shapefile(.shp / .shx file) is closed or not. 6. To stop displaying options and exit from the code. 5 Is closed ? :: False Enter [y] else [n], to move ahead with code execution y These are the below-listed options, select any one of them:: 1. To read data from a shapefile(.shp / .shx file) using the Fiona module of python. 2. To get the schema of the specified shapefile(.shp / .shx file) using the Fiona module of python. 3. To get the bounds of the specified shapefile(.shp / .shx file) using the Fiona module of python. 4. To get the CRS of the specified shapefile(.shp / .shx file) using the Fiona module of python. 5. To check whether the specified shapefile(.shp / .shx file) is closed or not. 6. To stop displaying options and exit from the code. 6 说明 在上面写的代码中,我们展示了 Python 中 Fiona 模块的用法。在这个程序中,我们编写了不同的函数,它们将用于执行各种不同的功能,例如使用这个 Python 模块从 shapefile 读取数据,以及通过不同的函数获取 shapefile 的各种参数。我们从 shapefile 获取的各种参数是 shapefile 的模式、shapefile 的边界,以及检查指定的 shapefile 当前是否已打开。所有这些函数都作为选项提供给用户,用户可以从任何这些选项中选择并执行特定的操作。这些选项以递归方式呈现给用户,直到用户停止显示选项并退出代码。 结论在本文中,我们了解了 Fiona 模块在 Python 中的用法以及该模块提供的不同功能。 |
我们请求您订阅我们的新闻通讯以获取最新更新。