Command line recording -python read shp file records

1, reads the shapefile main reading below, including spatialRef projection information, layerDefn layer definition information, geomType geometric object type, fieldDefn field definition information. After geomlist was a feature of each Wkt geometryRef into coordinates expressed in the form of points, reclist last acquired all field information of each feature by a name attribute.

2、

# Read ArcGIS Shape files example
from OSGeo Import OGR
filename = "cntry98.shp"

# Read-only manner open shp files (False the Read-only, the Read-True / the Write)
ds = ogr.Open (filename, False)

# Obtain a layer
layer = ds.GetLayer (0)

# Projection information
spatialref = layer.GetSpatialRef ()

Layer # definition information
lydefn = layer.GetLayerDefn ()

# Geometric object type (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 data records - acquisition geometry object and its corresponding property

geomlist,reclist=[],[]

# Obtain a first SF
Feature = layer.GetNextFeature ()
the while None Not Feature IS:
     GEOM = feature.GetGeometryRef ()
     geomlist + = [geom.ExportToWkt ()]
     REC = {}
     for FD in FieldList:
         REC [FD [ 'name ']] = feature.GetField (FD [' name '])
     reclist + = [REC]
     Feature layer.GetNextFeature = ()
# close the data source
ds.Destroy ()
# list of fields, geometry and attribute values
>>> len (FieldList)
13 is
>>> len (geomlist)
254
>>> Print (spatialRef)
GEOGCS [ "the WGS 84",
    the DATUM [ "WGS_1984",
        SPHEROID [ "the WGS 84", 6378137,298.257223563,
            AUTHORITY [ "the 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'}

Guess you like

Origin www.cnblogs.com/vividautumn/p/11553371.html