Open jiaoL06 opened 1 year ago
The field names are the same for all shapes. The name is the first item in the list for each field.
https://github.com/GeospatialPython/pyshp#reading-records
To see the fields for the Reader object above (sf) call the "fields" attribute:
>> fields = sf.fields
e.g.:
[('DeletionFlag', 'C', 1, 0), ['name', 'C', 50, 0]]
To preserve the association between shapes and their records, I find it best to iterate over sf.shapeRecords()
https://github.com/GeospatialPython/pyshp#reading-geometry-and-records-simultaneously
This yields shapeRecords, which have a record attribute, which has a really nice ._as_dict method. The keys of the dictionary that returns are the field names.
with shapefile.Reader(shapname) as sf:
for shaperec in sf.shapeRecords():
points = shaperec.shape.points
x, y = zip(*points)
z = shaperec.shape.z # I think, but haven't got a 3D shapefile right now to test this
record_dict = sr.record.as_dict()
fields = record_dict.keys()
values = record_dict.values()
What's your question?
when I create the shapefile,such as: w = shapefile.Writer(shapname) w.field("name1", "C") w.linez([data1]) w.record("cross1") w.field("name2", "C") w.linez([data2]) w.record("line1") w.close() Then I read the shapefile: sf = shapefile.Reader(shapname) datas = sf.shapes() for data in datas: points = data.points x, y = zip(*points) z = data.z
How to get the field name for data(x,y,z)?