Debian Codegraph @

stetl/filters/gmlfeatureextractor.py

raw
1 #!/usr/bin/env python
2 #
3 # Extracts arrays of etree GML features from an GML etree document.
4 #
5 # Author: Just van den Broecke
6 #
7 from stetl.util import Util
8 from stetl.filter import Filter
9 from stetl.packet import FORMAT
10
11 log = Util.get_log('gmlfeatureextractor')
12
13
14 class GmlFeatureExtractor(Filter):
15 """
16 Extract arrays of GML features etree elements from etree docs.
17
18 consumes=FORMAT.etree_doc, produces=FORMAT.etree_feature_array
19 """
20
21 # XPATH Query base for extracting features by (non-namespaced thus local-name) tagname
22 xpath_base = "//*[local-name() = '%s']"
23
24 # Constructor
25 def __init__(self, configdict, section='gml_feature_extractor'):
26 Filter.__init__(self, configdict, section, consumes=FORMAT.etree_doc, produces=FORMAT.etree_feature_array)
27
28 log.info("cfg = %s" % self.cfg.to_string())
29
30 # Build the Xpath expresion from configures tagnames
31 self.feature_tags = self.cfg.get('feature_tags').split(',')
32 self.total_features = 0
33 self.xpath_expression = ''
34 index = 0
35 for feature_tag in self.feature_tags:
36 if index > 0:
37 self.xpath_expression += '|'
38 self.xpath_expression += GmlFeatureExtractor.xpath_base % feature_tag
39 index += 1
40
41 log.info("xpath expression = %s" % self.xpath_expression)
42
43 def invoke(self, packet):
44 if packet.data is None or packet.is_end_of_stream():
45 return packet
46
47 # Input is etree_docs so extract all features into an array using XPATH
48 packet.data = packet.data.xpath(self.xpath_expression)
49 self.total_features += len(packet.data)
50 log.info('extracted %d features from GML etree doc (total = %d)' % (len(packet.data), self.total_features))
51 return packet