命令行记录-python 读shp文件记录
Posted vivid_autumn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了命令行记录-python 读shp文件记录相关的知识,希望对你有一定的参考价值。
1、读shapefile文件主要读以下内容,包括spatialRef投影信息,layerDefn图层定义信息,geomType几何对象类型,fieldDefn字段定义信息。geomlist是得到了每一个feature的geometryRef后转为Wkt形式表示的坐标点位,reclist通过name属性一次获取每个feature的所有field信息。
2、
#读ArcGIS Shape文件示例
from osgeo import ogr
filename="cntry98.shp"
#以只读的方式打开shp文件(False-read only, True-read/write)
ds=ogr.Open(filename,False)
#获取图层
layer=ds.GetLayer(0)
#投影信息
spatialref = layer.GetSpatialRef()
#图层定义信息
lydefn = layer.GetLayerDefn()
#几何对象类型(wkbPoint,wkbLineString,wkbPolygon)
geomtype=lydefn.GetGeomType()
#获取字段列表(字段类型,OFTInteger,OFTReal,OFTString,OFTDateTime)
fieldlist=[]
for i in range(lydefn.GetFieldCount()):
fddefn=lydefn.GetFieldDefn(i)
fddict={‘name‘:fddefn.GetName(),‘type‘:fddefn.GetType(),‘width‘:fddefn.GetWidth(),‘decimal‘:fddefn.GetPrecision()}
fieldlist+=[fddict]
#SF数据记录-获取几何对象及其对应属性
geomlist,reclist=[],[]
#获得第一个SF
feature=layer.GetNextFeature()
while feature is not None:
geom=feature.GetGeometryRef()
geomlist+=[geom.ExportToWkt()]
rec={}
for fd in fieldlist:
rec[fd[‘name‘]]=feature.GetField(fd[‘name‘])
reclist+=[rec]
feature=layer.GetNextFeature()
#关闭数据源
ds.Destroy()
#显示字段列表,几何对象及属性值
>>>len(fieldlist)
13
>>>len(geomlist)
254
>>>print(spatialref)
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]]
>>>print(geomtype)
3
>>>print(fieldlist[0])
{‘name‘: ‘FIPS_CNTRY‘, ‘type‘: 4, ‘width‘: 2, ‘decimal‘: 0}
>>> print(geomlist[0])
POLYGON ((-69.8822326660156 12.4111099243164,-69.94694519042
97 12.4366655349731,-70.0561218261719 12.5344429016113,-70.0
594482421875 12.5380554199219,-70.0602874755859 12.544166564
9414,-70.0633392333984 12.6216659545898,-70.0630645751953 12
.6286106109619,-70.0588989257813 12.6311092376709,-70.053344
7265625 12.6297206878662,-70.0352783203125 12.6197204589844,
-70.0311126708984 12.616943359375,-69.9322357177734 12.52805
519104,-69.8969573974609 12.4808330535889,-69.8914031982422
12.4722213745117,-69.8855590820313 12.4577770233154,-69.8739
013671875 12.4219436645508,-69.8733367919922 12.415833473205
6,-69.8761138916016 12.4116649627686,-69.8822326660156 12.41
11099243164))
>>> print(reclist[0][fieldlist[0][‘name‘]])
AA
>>> print(reclist[0])
{‘FIPS_CNTRY‘: ‘AA‘, ‘GMI_CNTRY‘: ‘ABW‘, ‘ISO_2DIGIT‘: ‘AW‘, ‘ISO_3DIGIT‘: ‘ABW‘, ‘CNTRY_NAME‘: ‘Aruba‘, ‘S
OVEREIGN‘: ‘Netherlands‘, ‘POP_CNTRY‘: 67074, ‘SQKM_CNTRY‘: 182.926, ‘SQMI_CNTRY‘: 70.628, ‘CURR_TYPE‘: ‘Fl
orin‘, ‘CURR_CODE‘: ‘AWG‘, ‘LANDLOCKED‘: ‘N‘, ‘COLOR_MAP‘: ‘1‘}
以上是关于命令行记录-python 读shp文件记录的主要内容,如果未能解决你的问题,请参考以下文章
python 简单的命令行实用程序,用于对大文件进行采样,仅选择记录的子集。这是为了让它随意地变为eaiser
Python学习记录——Ubuntu基本配置快捷键和系统启停命令行
错误记录记录 Android 命令行执行 Java 程序中出现的错误 ( dx 打包 PC 可执行文件报错 | dalvik 命令执行 kotlin 编译的 dex 文件报错 )