-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlayer.py
executable file
·35 lines (25 loc) · 925 Bytes
/
layer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
This translation file adds a __LAYER field to a datasource before translating it
Copyright (c) 2012 Paul Norman
<penorman@mac.com>
Released under the MIT license: http://opensource.org/licenses/mit-license.php
'''
from osgeo import ogr
def filterLayer(layer):
if not layer:
return
layername = layer.GetName()
# Add a __LAYER field
field = ogr.FieldDefn('__LAYER', ogr.OFTString)
field.SetWidth(len(layername))
layer.CreateField(field)
# Set the __LAYER field to the name of the current layer
for j in range(layer.GetFeatureCount()):
ogrfeature = layer.GetNextFeature()
ogrfeature.SetField('__LAYER', layername)
layer.SetFeature(ogrfeature)
# Reset the layer's read position so features are read later on
layer.ResetReading()
return layer