码迷,mamicode.com
首页 > 编程语言 > 详细

命令行记录-python 读shp文件记录

时间:2019-09-30 09:43:43      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:坐标   width   eps   dig   int   arcgis   图层   layer   integer   

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文件记录

标签:坐标   width   eps   dig   int   arcgis   图层   layer   integer   

原文地址:https://www.cnblogs.com/vividautumn/p/11553371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!