Skip to content

Commit

Permalink
[Vector.addfield] support setting all types (also lists)
Browse files Browse the repository at this point in the history
  • Loading branch information
johntruckenbrodt committed Jun 30, 2024
1 parent 9c77f58 commit 3dbccd7
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions spatialist/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,18 @@ def addfield(self, name, type, width=10, values=None):
-------
"""
fieldDefn = ogr.FieldDefn(name, type)
type_name = ogr.GetFieldTypeName(type)
field_defn = ogr.FieldDefn(name, type)
if type == ogr.OFTString:
fieldDefn.SetWidth(width)
self.layer.CreateField(fieldDefn)
field_defn.SetWidth(width)
self.layer.CreateField(field_defn)
if values is not None:
if len(values) != self.nfeatures:
raise RuntimeError('number of values does not match number of features')
for i, feature in enumerate(self.layer):
feature.SetField(name, values[i])
index = feature.GetFieldIndex(name)
method = getattr(feature, f'SetField{type_name}')
method(index, values[i])
self.layer.SetFeature(feature)

def addlayer(self, name, srs, geomType):
Expand Down

0 comments on commit 3dbccd7

Please sign in to comment.