Python Fiona模块
Fiona允许Python开发人员通过读取和写入地理数据文件与其他计算机系统进行接口。 Fiona包括将地理空间数据抽象库连接到其他应用程序(GDAL)的扩展模块。 Fiona旨在易于使用和可靠。它专注于以通常的Python IO范式读写数据,而不是使用OGR特定的类,而是使用基本的Python类型和协议,如文件,字典,映射和迭代器。 Fiona可以读写多层GIS格式和压缩虚拟文件系统中的真实世界数据,并且可以与其他Python GIS工具(如pyproj,Rtree和Shapely)一起使用。
Fiona是OGR API。它可以读写各种格式的数据。选择Fiona而不是OGR的关键原因是它更符合Python风格,更可靠,更不容易出错。它使用WKT和WKB标记语言来传达与矢量数据相关的空间信息。因此,它与Shapely等其他Python库非常兼容。您可以使用Fiona进行输入和输出,使用Shapely进行地理空间数据的创建和操作。
Fiona模块的需求:
地理信息系统(GIS)帮助我们预测、响应和理解我们的物理、政治、经济和文化景观的变化。GIS曾经只是一些大型机构如国家和城市才使用的东西,但现在由于准确和廉价的全球定位系统、卫星数据的商品化以及开源软件,它现在非常普遍。在GIS中,光栅代表连续的标量场(例如地表温度或海拔),而矢量代表诸如道路和行政边界之类的离散要素。 Fiona只关注这一点。它是对OGR库的矢量数据访问方法的Python包装器。对于极简主义者来说,它只是一个非常基本的包装器。它从文件中读取类似GeoJSON的映射,并将相同类型的映射作为记录发布回文件。这结束了讨论。图层、游标、几何操作、坐标系转换和远程方法调用都由其他Python软件包(如Shapely和pyproj)以及Python语言协议处理。
请记住这一点:Fiona被设计用于在一组特定的任务中取得成功,但在其他任务中则会失败。Fiona为了简洁和一致性而牺牲了内存和速度。与OGR的Python绑定不同,Fiona将向量数据从数据源转换为Python对象,而OGR的Python绑定则使用C指针。后者更易操作且更安全,但占用更多内存。如果您只需要访问单个记录字段,那么Fiona的性能会较慢 – 对于重投影或过滤数据文件,没有什么比ogr2ogr程序更好的选择 – 但如果您需要所有记录的字段和坐标,Fiona的性能要远远优于OGR的Python绑定。尽管复制是一种限制,但它使程序更易于理解。Fiona消除了管理C对象引用以避免崩溃的需要,并允许您使用Python映射访问器与矢量数据进行交互。记录用于表示地理信息系统中的离散地理特征。记录特征的语义含义是被充分认识到的[Kent1978]。记录具有单一类型,该类型的所有记录具有相同的字段,记录的字段涉及单个地理属性,是地理数据中最重要的属性之一。不同的系统对记录进行不同的描述,但它们之间存在足够的相似之处,使得程序员能够开发出有效的抽象数据模型。其中之一就是OGR模型。数据源、图层和要素是三个主要组成部分。要素包含特征和几何,而不是字段。一个OGR图层包含了一种特定的要素(例如,“道路”或“井”)。GeoJSON范例更加直观,它使用特征替代了OGR数据源和图层,而特征集合则替代了OGR数据源和图层。在GIS建模中,“要素”一词因此被过度使用,因为它既指我们的概念模型又指我们的数据模型中的实体。记录文件有许多不同的格式。在大约2005年之前,ESRI Shapefile [ESRI1998]是其中最重要的一种格式,至少在美国是如此,而且今天仍然很受欢迎。它是一种二进制文件类型。空间字段保存在shapefile文件中,而其他字段保存在dbf文件中。
现在让我们来看一下Python中Fiona模块的代码。
代码:
# A sample To understand the usage of the Fiona module in Python with the help of this module we can easily read the file which stores the geographical positioning data There are different functions that we have written that are used to perform different operations some of the functions requires some input in case of these functions the user is prompted with the message to give the required input and then the processing of the input data is done according to the functionality of that function
# All the models which are required for the writing of the code which is written below are important at the beginning of the program
import fiona
import sys
# a sample class is written that will consist of functions that are used to perform different operations some of the functions require some input in the case of these functions the user is prompted with the message to give the required input and then the processing of the input data is done according to the functionality of that function
class FionaClass:
# Sample constructor is written that can be used to initialize the class variables of the above Britain class so we can use this class variable across different functions, the name of the file from where we want to read data and get various information out of it is stored in the fname variable
def __init__(self):
self.fname = None
pass
# This function is used to read the geographical data that is stored in a file with the help of the open function of this module which is provided by Python we have read a shapefile in which the geographical data is stored first of all this function asks the user for the name of the file from which we want to read the geographical data once the user has added the name of the file from which we want to read the geographical data that file is opened with the help of open function of this module in Python and then the data which is read from the specified file is presented to the user as the output of this function
def read_geo_data_from_the_file(self):
print("Enter the name of the file(.shp file)::")
filename = input()
self.fname = filename
opne_handler = fiona.open(filename, 'r')
opne_handler.closed
print("First data in the file is::")
# print(next(opne_handler))
print("Total length of the file openned is {}".format(len(list(opne_handler))))
# This is another function which we have returned in this function the schema of the data which is read from the file which is specified in the previous function is calculated and as a result of this function the schema of the file which was mentioned in the previous function from which we are reading the geographical data is printed to the user
def get_schema_of_file(self):
filename = self.fname
opne_handler = fiona.open(filename, 'r')
print("schema ::", opne_handler.schema)
# This is another function which we have returned in this function the bounds of the data which is read from the file which is specified in the previous function is calculated and as a result of this function the bounds of the file which was mentioned in the previous function from which we are reading the geographical data is printed to the user
def get_bounds_of_file(self):
filename = self.fname
opne_handler = fiona.open(filename, 'r')
print("bounds ::", opne_handler.bounds)
# This is another function which we have returned in this function the crs of the data which is read from the file which is specified in the previous function is calculated and as a result of this function the crs of the file which was mentioned in the previous function from which we are reading the geographical data is printed to the user
def get_crs_of_file(self):
filename = self.fname
opne_handler = fiona.open(filename, 'r')
print("crs ::", opne_handler.crs)
# This is the last function of this class that we have written this function is used to check whether the specified file which is opened to read the geographical data stored in that file is currently open or not other words we can say that this function helps us to get the state of the file from which we are reading our geographical data the return type of this function is of the Boolean type that means the return value of this function will be true if the file is closed and if the file is open the return type of this function will be changed to false
def check_is_file_closed(self):
filename = self.fname
opne_handler = fiona.open(filename, 'r')
print("Is closed ? ::", opne_handler.closed)
def main():
my_geological_object = FionaClass()
while(True):
print("These are the below-listed options, select any one of them::")
print("1. To read data from a shapefile(.shp / .shx file) using the Fiona module of python.")
print("2. To get the schema of the specified shapefile(.shp / .shx file) using the Fiona module of python.")
print("3. To get the bounds of the specified shapefile(.shp / .shx file) using the Fiona module of python.")
print("4. To get the crs of the specified shapefile(.shp / .shx file) using the Fiona module of python.")
print("5. To check whether the specified shapefile(.shp / .shx file) is closed or not.")
print("6. To stop displaying options and exit from the code.")
menu_choice = input()
menu_choice = int(menu_choice)
if menu_choice == 1:
my_geological_object.read_geo_data_from_the_file()
elif menu_choice == 2:
my_geological_object.get_schema_of_file()
elif menu_choice == 3:
my_geological_object.get_bounds_of_file()
elif menu_choice == 4:
my_geological_object.get_crs_of_file()
elif menu_choice == 5:
my_geological_object.check_is_file_closed()
elif menu_choice == 6:
sys.exit()
print("Enter [y] else [n], to move ahead with code execution ")
continue_or_exit = input()
if continue_or_exit == 'y' or continue_or_exit == 'Y':
pass
elif continue_or_exit == 'n' or continue_or_exit == 'N':
sys.exit()
if __name__ == '__main__':
main()
输出:
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模块的用法。在这个程序中,我们编写了不同的函数,用于执行各种不同的功能,例如使用该模块从shapefile中读取数据,并使用不同的函数来获取shapefile的各种参数,例如shapefile的方案,shapefile的边界,以及检查指定的shapefile是否当前已打开等。所有这些函数都作为选项呈现给用户,用户可以从中选择任何一个选项并执行该特定操作,这些选项以递归方式呈现给用户,直到用户停止显示选项并退出代码。
结论:
在本文中,我们了解了Python中Fiona模块的用法以及该模块提供的不同功能。