| 1 |
#!/usr/bin/env python |
| 2 |
# |
| 3 |
# Splits stream of GML lines into etree docs. |
| 4 |
# |
| 5 |
# Author: Just van den Broecke |
| 6 |
# |
| 7 |
import codecs |
| 8 |
from deprecated.sphinx import deprecated |
| 9 |
from stetl.util import Util, etree, StringIO |
| 10 |
from stetl.filter import Filter |
| 11 |
from stetl.packet import FORMAT |
| 12 |
|
| 13 |
log = Util.get_log('gmlsplitter') |
| 14 |
|
| 15 |
|
| 16 |
@deprecated(version='1.0.4', reason='Use the more robust XmlElementStreamerFileInput + XmlAssembler instead!!!') |
| 17 |
class GmlSplitter(Filter): |
| 18 |
""" |
| 19 |
Split a stream of text XML lines into documents |
| 20 |
TODO phase out |
| 21 |
|
| 22 |
consumes=FORMAT.xml_line_stream, produces=FORMAT.etree_doc |
| 23 |
""" |
| 24 |
|
| 25 |
def __init__(self, configdict, section='gml_splitter'): |
| 26 |
Filter.__init__(self, configdict, section, consumes=FORMAT.xml_line_stream, produces=FORMAT.etree_doc) |
| 27 |
|
| 28 |
log.info("cfg = %s" % self.cfg.to_string()) |
| 29 |
self.max_features = self.cfg.get_int('max_features', 10000) |
| 30 |
# File preamble |
| 31 |
self.start_container = self.cfg.get('start_container') |
| 32 |
# File postamble |
| 33 |
self.end_container = self.cfg.get('end_container') |
| 34 |
self.container_tag = self.cfg.get('container_tag') |
| 35 |
# self.feature_tags = self.cfg.get('feature_tags').split(',') |
| 36 |
self.start_feature_markers = self.cfg.get('start_feature_markers').split(',') |
| 37 |
self.end_feature_markers = self.cfg.get('end_feature_markers').split(',') |
| 38 |
self.feature_count = 0 |
| 39 |
self.total_feature_count = 0 |
| 40 |
self.in_heading = True |
| 41 |
|
| 42 |
# End of file is line with end_container_tag |
| 43 |
self.end_container_tag = '</%s' % self.container_tag |
| 44 |
|
| 45 |
# Derive start and end tags from feature_tags |
| 46 |
# for feature_tag in self.feature_tags: |
| 47 |
# self.start_feature_markers.append('<%s' % feature_tag) |
| 48 |
# self.end_feature_markers.append('</%s>' % feature_tag) |
| 49 |
self.expect_end_feature_markers = [] |
| 50 |
self.expect_end_feature_tag = None |
| 51 |
self.buffer = None |
| 52 |
# Reusable XML parser |
| 53 |
self.xml_parser = etree.XMLParser(remove_blank_text=True) |
| 54 |
|
| 55 |
def invoke(self, packet): |
| 56 |
if packet.data is None: |
| 57 |
if (packet.is_end_of_stream() or packet.is_end_of_doc()) and self.buffer: |
| 58 |
# EOF but still data in buffer: make doc |
| 59 |
log.info("Last Buffer") |
| 60 |
self.buffer_to_doc(packet) |
| 61 |
else: |
| 62 |
# Valid line: push to the splitter |
| 63 |
# If buffer filled process it |
| 64 |
self.push_line(packet) |
| 65 |
if packet.is_end_of_doc() is True: |
| 66 |
self.buffer_to_doc(packet) |
| 67 |
|
| 68 |
return packet |
| 69 |
|
| 70 |
def push_line(self, packet): |
| 71 |
# Assume GML lines with single tag per line !! |
| 72 |
# This is ok for e.g. OGR output |
| 73 |
# TODO we need to become more sophisticated when no newlines in XML |
| 74 |
|
| 75 |
line = packet.consume() |
| 76 |
|
| 77 |
if packet.is_end_of_stream() is True: |
| 78 |
return packet |
| 79 |
|
| 80 |
# Start new buffer filling |
| 81 |
if self.buffer is None: |
| 82 |
self.buffer = self.init_buf() |
| 83 |
self.feature_count = 0 |
| 84 |
self.in_heading = True |
| 85 |
packet.set_end_of_doc(False) |
| 86 |
|
| 87 |
if self.is_start_feature(line) is True: |
| 88 |
if self.in_heading: |
| 89 |
# First time: we are in heading |
| 90 |
# Write container start |
| 91 |
self.buffer.write(self.start_container) |
| 92 |
self.in_heading = False |
| 93 |
|
| 94 |
self.buffer.write(line) |
| 95 |
self.buffer.write('<!-- Feature #%s -->\n' % self.feature_count) |
| 96 |
|
| 97 |
else: |
| 98 |
# If within feature or end-of-feature: write |
| 99 |
if self.expect_end_feature_tag is not None and packet.is_end_of_doc() is False: |
| 100 |
self.buffer.write(line) |
| 101 |
|
| 102 |
# If endtag of feature found may also indicate buffer filled with max_features |
| 103 |
if self.is_end_feature(line) is True: |
| 104 |
# Start or end tag of ogr:feature element |
| 105 |
if self.feature_count >= self.max_features and self.expect_end_feature_tag is None: |
| 106 |
self.buffer.write(self.end_container) |
| 107 |
log.info("Buffer filled feat_count = %d total_feat_count= %d" % ( |
| 108 |
self.feature_count, self.total_feature_count)) |
| 109 |
self.feature_count = 0 |
| 110 |
packet.set_end_of_doc() |
| 111 |
return packet |
| 112 |
|
| 113 |
# Last tag (end of container) reaching |
| 114 |
if line.find(self.end_container_tag) >= 0: |
| 115 |
if self.buffer is not None and self.feature_count > 0: |
| 116 |
self.buffer.write(self.end_container) |
| 117 |
log.info("Buffer filled (EOF) feat_count = %d total_feat_count= %d" % ( |
| 118 |
self.feature_count, self.total_feature_count)) |
| 119 |
self.feature_count = 0 |
| 120 |
packet.set_end_of_doc() |
| 121 |
return packet |
| 122 |
|
| 123 |
return packet |
| 124 |
|
| 125 |
def buffer_to_doc(self, packet): |
| 126 |
# Process/transform data in buffer |
| 127 |
self.buffer.seek(0) |
| 128 |
try: |
| 129 |
packet.data = etree.parse(self.buffer, self.xml_parser) |
| 130 |
except Exception as e: |
| 131 |
bufStr = self.buffer.getvalue() |
| 132 |
if not bufStr: |
| 133 |
log.info("parse buffer empty: content=[%s]" % bufStr) |
| 134 |
else: |
| 135 |
log.error("error in buffer parsing %s" % str(e)) |
| 136 |
print(bufStr) |
| 137 |
raise |
| 138 |
self.buffer.close() |
| 139 |
self.buffer = None |
| 140 |
|
| 141 |
def init_buf(self): |
| 142 |
buffer = StringIO() |
| 143 |
buffer = codecs.getwriter("utf8")(buffer) |
| 144 |
return buffer |
| 145 |
|
| 146 |
def is_start_feature(self, line): |
| 147 |
index = 0 |
| 148 |
for feature_tag in self.start_feature_markers: |
| 149 |
if line.find(feature_tag) >= 0: |
| 150 |
# found it ! Now we expect the end tag for this start_tag |
| 151 |
self.expect_end_feature_tag = self.end_feature_markers[index] |
| 152 |
self.expect_end_feature_markers.append(self.expect_end_feature_tag) |
| 153 |
self.feature_count += 1 |
| 154 |
self.total_feature_count += 1 |
| 155 |
return True |
| 156 |
|
| 157 |
# Not found: next tag |
| 158 |
index += 1 |
| 159 |
|
| 160 |
# Nothing found |
| 161 |
return False |
| 162 |
|
| 163 |
def is_end_feature(self, line): |
| 164 |
# Not even within a feature ? |
| 165 |
if self.expect_end_feature_tag is None: |
| 166 |
return False |
| 167 |
|
| 168 |
# ASSERTION: one or more expect_end_feature_tag set, thus within feature |
| 169 |
|
| 170 |
# End-of-feature reached ? |
| 171 |
if line.find(self.expect_end_feature_tag) >= 0: |
| 172 |
self.expect_end_feature_tag = None |
| 173 |
self.expect_end_feature_markers.pop() |
| 174 |
if len(self.expect_end_feature_markers) > 0: |
| 175 |
# Set expected end-tag to last in list |
| 176 |
self.expect_end_feature_tag = self.expect_end_feature_markers[-1] |
| 177 |
|
| 178 |
return True |
| 179 |
|
| 180 |
# Still within feature |
| 181 |
return False |