3

I have a .las file and .shp file with polygons. The shapefile looks like follows: enter image description here

For each polygon, I calculated statistics based on elevation data vertices from the .las. Now I want to add fields into .shp with statistics. The problem occurs when I just try to add extra fields with the following function:

    def writeDims(self, params):
       r = shapefile.Reader(self.shapefile)
       w = shapefile.Writer("new.shp")
       w.fields = list(r.fields)
       for key in params[1].keys():
           w.field(key, "F")
       for shaperec in r.iterShapeRecords():
           w.record(*shaperec.record)
           w.shape(shaperec.shape)
       w.close()

the result shapefile has only part of the full shapefile however with fields: enter image description here

how to prevent such file-cropping?

2
  • How large is your data? How much RAM do you exploit?
    – Taras
    Dec 7, 2021 at 11:40
  • the .shp file is about 5,8 mb. But if RAM is not enough, python will shut down isn't it? Dec 7, 2021 at 17:04

1 Answer 1

1

With the caveat that I have not used this library, you might check your code usage.

The documentation says to only use the tail of list if the field list is from the Reader class.

w.fields = r.fields[1:] # skip first deletion field

You can see that the first items is a field that is not a type you want.

fields = sf.fields

assert fields == [("DeletionFlag", "C", 1, 0), ["AREA", "N", 18, 5],
["BKG_KEY", "C", 12, 0], ["POP1990", "N", 9, 0], ["POP90_SQMI", "N", 10, 1],
["HOUSEHOLDS", "N", 9, 0],

Try changing you code to:

def writeDims(self, params):
   r = shapefile.Reader(self.shapefile)
   w = shapefile.Writer("new.shp")
   w.fields = r.fields[1:] # You should not need to recast r.fields to a list.
   for key in params[1].keys():
       w.field(key, "F")
   for shaperec in r.iterShapeRecords():
       w.record(*shaperec.record)
       w.shape(shaperec.shape)
   w.close()

Additionally, your key/value pairs in QGIS suggest there is a an issue with the field types in your final shapefile. Your float fields should not have spaces and then more numbers.

enter image description here

This suggest that numbers are being inserted as strings and not floats. Not sure if this will fix the problem or it might exist else where, DBF database are really simple (you can edit them in a spreadsheet program) and if the library is not doing any type checking it might be performing incorrect inserts.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.