-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditFieldsOpenSource.py~
109 lines (72 loc) · 3.11 KB
/
EditFieldsOpenSource.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import processing
layer = iface.legendInterface().layers()[0] #if only one layer or find better way to do this
features = processing.features(layer)
#OR
count = 0
depths = []
for feature in layer.getFeatures(): #getFeatures() returns an interator of the layer's features
depths.append([count, feature.attributes()[-2]])
count +=1
#To get field by name rather than index:
idx = layer.fieldNameIndex('name')
for feature in layer.getFeatures():
depths.append(feature.attributes()[idx])
#add field to shp with ogr: (can be done while shp is open in qgis)
from osgeo import ogr
source = ogr.Open('/home/nlibassi/Geodesy/Thesis/Project/Vector/ITRF96TM30/20160929_BathymetryCorrected/20160929_BathymetryCorrected_attr.shp', update=True)
layerO = source.GetLayer()
layerDefn = layerO.GetLayerDefn()
fieldNames = [layerDefn.GetFieldDefn(i).GetName() for i in range(layerDefn.GetFieldCount())]
#check for existence of current field with name of new field
print len(fieldNames), 'MW5Depth' in fieldNames
5 False
newField = ogr.FieldDefn('MW5Depth', ogr.OFTReal)
layerO.CreateField(newField)
#not sure if both of these are necessary but may be necessary before writing data to new field
source.Destroy()
source = None
#ogr field types
"""
FIELD_TYPES = [
'int', # OFTInteger, Simple 32bit integer
None, # OFTIntegerList, List of 32bit integers
'float', # OFTReal, Double Precision floating point
None, # OFTRealList, List of doubles
'str', # OFTString, String of ASCII chars
None, # OFTStringList, Array of strings
None, # OFTWideString, deprecated
None, # OFTWideStringList, deprecated
None, # OFTBinary, Raw Binary data
None, # OFTDate, Date
None, # OFTTime, Time
None, # OFTDateTime, Date and Time
]
"""
#print values from a field:
for feature in layerO:
print feature.GetField("Depth") #where Depth is example field name
#add field with shapely or others?
#No need for get field index in future, just use field names from LayerDefn
from osgeo import ogr
source = ogr.Open('/home/nlibassi/Geodesy/Thesis/Project/Vector/ITRF96TM30/20160929_BathymetryCorrected/20160929_BathymetryCorrected_attr2.shp', update=True)
layer = source.GetLayer()
layerDefn = layer.GetLayerDefn()
fieldNames = [layerDefn.GetFieldDefn(i).GetName() for i in range(layerDefn.GetFieldCount())]
type(layerDefn)
<class 'osgeo.ogr.FeatureDefn'>
fieldNames
['latitude', 'longitude', 'elevation', 'name', 'TestFld']
feature = layer.GetFeature(0)
feature.GetFieldIndex('TestFld')
#output: 4
for feature in layer:
feature.SetField(4, 'test')
source.Destroy()
source = None
#getting dictionary in the following format with pyqgis: {rowNum: depth}
layer = iface.legendInterface().layers()[0]
depths = {}
for feature in layer.getFeatures():
depths.append(feature.attributes()[1]:feature.attributes()[-1])
#how to write to a field?
#have not yet been able to write to a field in ogr using SetField() method