import fiona
c = fiona.open('/data/gdata/prov_capital.shp')
c.driver
'ESRI Shapefile'
c.crs
CRS.from_epsg(4326)
c.crs.to_proj4()
'+init=epsg:4326'
{'no_defs': True, 'ellps': 'WGS84', 'datum': 'WGS84', 'proj': 'longlat'}
c.crs
CRS.from_epsg(4326)
CRS返回的结果,是对 PROJ.4
参数的映射。
fiona.crs
模块共有3个函数,以协助完成这些映射。
fiona.crs.to_string
函数是将映射转换到PROJ.4字符串中:
from fiona.crs import to_string
print(to_string(c.crs))
EPSG:4326
fiona.crs.from_string
为不可逆。
from fiona.crs import from_string
from_string("+datum=WGS84 +ellps=WGS84 +no_defs +proj=longlat")
CRS.from_epsg(4326)
fiona.crs.from_epsg
是EPSG代码CRS映射的一个快速方式。
from fiona.crs import from_epsg
from_epsg(3857)
CRS.from_epsg(3857)
len(c)
34
收集记录的 minimum bounding rectangle
(MBR) 或 bounds
可通过只读的
fiona.collection.Collection.bounds
属性来获取。
c.bounds
(87.57610577000008, 19.97013978600006, 126.56611955000005, 45.69344403700006)
from pprint import pprint
import fiona
c = fiona.open('/data/gdata/prov_capital.shp')
pprint(c.schema)
{'geometry': 'Point', 'properties': {'lat': 'float:13.11', 'lon': 'float:13.11', 'name': 'str:100'}}
rec = next(c)
/tmp/ipykernel_2109/975926702.py:1: FionaDeprecationWarning: Collection.__next__() is buggy and will be removed in Fiona 2.0. Switch to `next(iter(collection))`. rec = next(c)
rec = next(iter(c))
set(rec.keys()) - set(c.schema.keys())
{'id'}
set(rec['properties'].keys()) == set(c.schema['properties'].keys())
True
模式映射值也可以是字段类型,如 'Polygon'
, 'float'
, 'str'
。
相应的Python类型可以在 fiona.FIELD_TYPES_MAP
库中查到。
pprint(fiona.FIELD_TYPES_MAP)
{'List[str]': typing.List[str], 'bytes': <class 'bytes'>, 'date': <class 'fiona.schema.FionaDateType'>, 'datetime': <class 'fiona.schema.FionaDateTimeType'>, 'float': <class 'float'>, 'int': <class 'int'>, 'int32': <class 'int'>, 'int64': <class 'int'>, 'str': <class 'str'>, 'time': <class 'fiona.schema.FionaTimeType'>}
rec['properties']
fiona.Properties(name='乌鲁木齐', lat=43.7818, lon=87.5761)
type(rec['properties']['name'])
str
c.schema['properties']['name']
'str:100'
字符串字段可限制最大宽度。 str:25
设置的就是不可以超过 25
个字符。
如果这个值用于打开文件,该值的属性就是将在 25
字符处截断。
默认宽度为80个字符,这意味着 str
和 str:80
属同一意思。
Fiona还有一个函数:可获取字符串属性宽度。
from fiona import prop_width
prop_width('str:25')
25
prop_width('str')
80
另一个函数是可获取Python属性类型。
from fiona import prop_type
prop_type('int')
int
prop_type('float')
float
prop_type('str:25')
str
以上的例子针对于Python 3。Python 2'str'
的性能是 'unicode'
。
import fiona
from pprint import pprint
c = fiona.open('/data/gdata/prov_capital.shp')
rec = next(iter(c))
rec
fiona.Feature(geometry=fiona.Geometry(coordinates=(87.57610577000008, 43.78176563200003), type='Point'), id='0', properties=fiona.Properties(name='乌鲁木齐', lat=43.7818, lon=87.5761))
此条数据记录与本源或其他外部资源的 fiona.collection
无关。它是完全独立的,使用任何方式都很安全。关闭集并不影响数据记录。
c.close()
rec['id']
'0'
c = fiona.open('/data/gdata/prov_capital.shp')
rec = next(iter(c))
rec['id']
'0'
rec['properties']
fiona.Properties(name='乌鲁木齐', lat=43.7818, lon=87.5761)
rec['geometry']
fiona.Geometry(coordinates=(87.57610577000008, 43.78176563200003), type='Point')
类型 | 坐标 |
点 | 单一(x,y)元组 |
线 | (x,y)元组顶点列表 |
多边形 | 环列表[每个(x,y)元组列表] |
点集合 | 点列表[每个(x,y)元组列表] |
线集合 | 线列表[每个(x,y)元组列表] |
面集合 | 多边形列表 |
Fiona,类似于GeoJSON格式,既有北半球的“北方”,又有笛卡尔的“X-Y”偏角。
上文说的元组值 (x, y)
,要么是(本初子午线的经度E、纬度N),要么是其他投影坐标系统(东,北)。
真正顺序是经-纬,而不是纬-经,尽管我们大多都在说 "纬度,经度" ,Fiona x,y
基本都是东向和北向,也就是 (经, 纬)
。
先说经度,后说纬度,与GeoJSON规范保持一致。
from shapely.geometry import shape
l1 = shape({'type': 'LineString', 'coordinates': [(0, 0), (2, 2)]})
l2 = shape({'type': 'LineString', 'coordinates': [(0, 0), (1, 1), (2, 2)]})
l1 == l2
False
l1.equals(l2)
True
注解:Dirty数据,某些文件可能会包含矢量数据 invalid
(在生成结果的质量控制不足时)或intention(“dirty”矢量保存到一个特殊的文件)。
Fiona不会清除dirty数据,所以你应该确保你所得到的是否为纯数据。