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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Converts Stetl Packet FORMATs. This can be used to connect
# Stetl components with different output/input formats.
#
# Author:Just van den Broecke
from stetl.component import Config
from stetl.util import Util, etree
from stetl.filter import Filter
from stetl.packet import FORMAT
import json
log = Util.get_log("formatconverter")
class FormatConverter(Filter):
"""
Converts (almost) any packet format (if converter available).
consumes=FORMAT.any, produces=FORMAT.any but actual formats
are changed at initialization based on the input to output format to
be converted via the input_format and output_format config parameters.
"""
# Start attribute config meta
# Applying Decorator pattern with the Config class to provide
# read-only config values from the configured properties.
@Config(ptype=dict, default=None, required=False)
def converter_args(self):
"""
Custom converter-specific arguments.
"""
pass
# End attribute config meta
# Constructor
def __init__(self, configdict, section):
Filter.__init__(self, configdict, section, consumes=FORMAT.any, produces=FORMAT.any)
self.converter = None
def init(self):
if self.output_format == FORMAT.any:
# Any as output is always valid, do nothing
self.converter = FormatConverter.no_op
return
# generate runtime error as we may have registered converters at init time...
if self.input_format not in FORMAT_CONVERTERS.keys():
raise NotImplementedError('No format converters found for input format %s' % self.input_format)
# ASSERTION: converters present for input_format
if self.output_format not in FORMAT_CONVERTERS[self.input_format].keys():
raise NotImplementedError('No format converters found for input format %s to output format %s' % (
self.input_format, self.output_format))
# ASSERTION: converters present for input_format and output_format
# Lookup and assign the active converter
self.converter = FORMAT_CONVERTERS[self.input_format][self.output_format]
# OGR feature def
self.feat_def = None
def invoke(self, packet):
if packet.data is None:
packet.format = self.output_format
return packet
if self.converter_args is not None:
self.converter(packet, self.converter_args)
else:
self.converter(packet)
packet.format = self.output_format
return packet
@staticmethod
def add_converter(input_format, output_format, converter_fun):
# Add to existing input format converters or create new
if input_format not in FORMAT_CONVERTERS.keys():
FORMAT_CONVERTERS[input_format] = {output_format: converter_fun}
else:
FORMAT_CONVERTERS[input_format][output_format] = converter_fun
@staticmethod
def no_op(packet):
return packet
# START etree_doc
@staticmethod
def etree_doc2geojson_collection(packet, converter_args=None):
"""
Use converter_args to determine XML tag names for features and GeoJSON feature id.
For example
converter_args = {
'root_tag': 'FeatureCollection',
'feature_tag': 'featureMember',
'feature_id_attr': 'fid'
}
:param packet:
:param converter_args:
:return:
"""
packet.data = packet.data.getroot()
packet = FormatConverter.etree_elem2struct(packet)
feature_coll = {'type': 'FeatureCollection', 'features': []}
root_tag = 'FeatureCollection'
feature_tag = 'featureMember'
if converter_args:
root_tag = converter_args['root_tag']
feature_tag = converter_args['feature_tag']
features = packet.data[root_tag][feature_tag]
for feature in features:
packet.data = feature
packet = FormatConverter.struct2geojson_feature(packet, converter_args)
feature_coll['features'].append(packet.data)
packet.data = feature_coll
return packet
@staticmethod
def etree_doc2string(packet):
packet.data = etree.tostring(packet.data, pretty_print=True, xml_declaration=True)
return packet
@staticmethod
def etree_doc2struct(packet, strip_space=True, strip_ns=True, sub=False, attr_prefix='', gml2ogr=True,
ogr2json=True):
"""
:param packet:
:param strip_space:
:param strip_ns:
:param sub:
:param attr_prefix:
:param gml2ogr:
:param ogr2json:
:return:
"""
packet.data = packet.data.getroot()
return FormatConverter.etree_elem2struct(packet, strip_space, strip_ns, sub, attr_prefix, gml2ogr, ogr2json)
# END etree_doc
# START etree_elem
@staticmethod
def etree_elem2geojson_feature(packet, converter_args=None):
"""
"""
packet = FormatConverter.etree_elem2struct(packet, converter_args)
packet = FormatConverter.struct2geojson_feature(packet, converter_args)
return packet
@staticmethod
def etree_elem2struct(packet, strip_space=True, strip_ns=True, sub=False, attr_prefix='', gml2ogr=True,
ogr2json=True):
"""
:param packet:
:param strip_space:
:param strip_ns:
:param sub:
:param attr_prefix:
:param gml2ogr:
:param ogr2json:
:return:
"""
packet.data = Util.elem_to_dict(packet.data, strip_space, strip_ns, sub, attr_prefix, gml2ogr, ogr2json)
return packet
# END etree_elem
# START geojson_feature
@staticmethod
def geojson_feature2ogr_feature(packet, converter_args=None):
from stetl.util import ogr
# str = json.dumps(packet.data)
json_feat = packet.data
json_geom = json_feat["geometry"]
json_props = json_feat["properties"]
# Create OGR Geometry from GeoJSON geom-fields
geom_dict = dict()
geom_dict["type"] = json_geom["type"]
geom_dict["coordinates"] = json_geom["coordinates"]
geom_str = json.dumps(geom_dict)
ogr_geom = ogr.CreateGeometryFromJson(geom_str)
# Once: create OGR Feature definition
# TODO: assume all string-fields for now, may use type-mapping definition in converter_args
comp = packet.component
if comp.feat_def is None:
comp.feat_def = ogr.FeatureDefn()
field_def = ogr.FieldDefn("id", ogr.OFTString)
comp.feat_def.AddFieldDefn(field_def)
for field_name in json_props:
# OGR needs UTF-8 internally
if isinstance(field_name, unicode):
field_name = field_name.encode('utf8')
field_def = ogr.FieldDefn(field_name, ogr.OFTString)
comp.feat_def.AddFieldDefn(field_def)
ogr_geom_type = ogr_geom.GetGeometryType()
comp.feat_def.SetGeomType(ogr_geom_type)
# Create and populate Feature with id, geom and attributes
feature = ogr.Feature(comp.feat_def)
json_id = json_feat["id"]
if isinstance(json_id, unicode):
json_id = json_id.encode('utf8')
feature.SetField("id", json_id)
feature.SetGeometry(ogr_geom)
for field_name in json_props:
# OGR needs UTF-8 internally
field_value = json_props[field_name]
if isinstance(field_value, unicode):
field_value = field_value.encode('utf8')
if not isinstance(field_value, basestring):
field_value = str(field_value)
# OGR needs UTF-8 internally
if isinstance(field_name, unicode):
field_name = field_name.encode('utf8')
# print("id=%s k=%s v=%s" % (json_id, field_name, field_value))
feature.SetField(field_name, field_value)
packet.data = feature
return packet
# END geojson_feature
# START geojson_collection
@staticmethod
def geojson_coll2ogr_feature_arr(packet, converter_args=None):
json_feat_arr = packet.data["features"]
ogr_feat_arr = list()
for feat in json_feat_arr:
packet.data = feat
packet = FormatConverter.geojson_feature2ogr_feature(packet)
ogr_feat_arr.append(packet.data)
packet.data = ogr_feat_arr
return packet
# END geojson_collection
# START ogr_feature
@staticmethod
def ogr_feature2struct(packet, converter_args=None):
s = packet.data.ExportToJson()
import ast
# http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary
# ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}")
packet.data = ast.literal_eval(s)
return packet
# END ogr_feature
@staticmethod
def ogr_feature_arr2geojson_coll(packet, converter_args=None):
# See http://geojson.org/geojson-spec.html
geojson_coll = {'type': 'FeatureCollection', 'features': []}
import ast
for feature in packet.data:
geojson_coll['features'].append(ast.literal_eval(feature.ExportToJson()))
packet.data = geojson_coll
return packet
@staticmethod
def record2struct(packet, converter_args=None):
if converter_args is not None:
struct = dict()
struct[converter_args['top_name']] = packet.data
packet.data = struct
return packet
@staticmethod
def record2record_array(packet, converter_args=None):
if not hasattr(packet, 'arr'):
packet.arr = list()
if packet.data is not None:
packet.arr.append(packet.data)
packet.consume()
# At end of stream or when max array size reached: close the array
if packet.is_end_of_stream() is True or \
(converter_args is not None and len(packet.arr) >= converter_args['max_len']):
# End of stream reached: assembled record array
packet.data = packet.arr
packet.arr = list()
return packet
@staticmethod
def record_array2struct(packet, converter_args=None):
return FormatConverter.record2struct(packet, converter_args)
@staticmethod
def string2etree_doc(packet):
packet.data = etree.fromstring(packet.data)
return packet
@staticmethod
def struct2string(packet):
packet.data = packet.to_string()
return packet
@staticmethod
def struct2geojson_feature(packet, converter_args=None):
key, feature_struct = packet.data.popitem()
feature = {'type': 'feature', 'properties': {}}
id_field = None
if converter_args:
id_field = converter_args['feature_id_attr']
for attr_name in feature_struct:
val = feature_struct[attr_name]
if attr_name == 'geometry':
feature['geometry'] = val
elif attr_name == id_field:
feature['id'] = val
else:
feature['properties'][attr_name] = val
packet.data = feature
return packet
# 'xml_line_stream', 'etree_doc', 'etree_element', 'etree_feature_array', 'xml_doc_as_string',
# 'string', 'record', 'record_array', 'geojson_collection', geojson_feature', 'struct',
# 'ogr_feature', 'ogr_feature_array', 'any'
FORMAT_CONVERTERS = {
FORMAT.etree_doc: {
FORMAT.geojson_collection: FormatConverter.etree_doc2geojson_collection,
FORMAT.string: FormatConverter.etree_doc2string,
FORMAT.struct: FormatConverter.etree_doc2struct,
FORMAT.xml_doc_as_string: FormatConverter.etree_doc2string
},
FORMAT.etree_element: {
FORMAT.geojson_feature: FormatConverter.etree_elem2geojson_feature,
FORMAT.string: FormatConverter.etree_doc2string,
FORMAT.struct: FormatConverter.etree_elem2struct,
FORMAT.xml_doc_as_string: FormatConverter.etree_doc2string
},
FORMAT.geojson_feature: {
FORMAT.ogr_feature: FormatConverter.geojson_feature2ogr_feature
},
FORMAT.geojson_collection: {
FORMAT.ogr_feature_array: FormatConverter.geojson_coll2ogr_feature_arr
},
FORMAT.ogr_feature: {
FORMAT.geojson_feature: FormatConverter.ogr_feature2struct,
FORMAT.struct: FormatConverter.ogr_feature2struct
},
FORMAT.ogr_feature_array: {
FORMAT.geojson_collection: FormatConverter.ogr_feature_arr2geojson_coll
},
FORMAT.record: {
FORMAT.struct: FormatConverter.record2struct,
FORMAT.record_array: FormatConverter.record2record_array
},
FORMAT.record_array: {
FORMAT.struct: FormatConverter.record_array2struct
},
FORMAT.string: {
FORMAT.etree_doc: FormatConverter.string2etree_doc,
FORMAT.xml_doc_as_string: FormatConverter.no_op
},
FORMAT.struct: {
FORMAT.string: FormatConverter.struct2string,
FORMAT.geojson_feature: FormatConverter.struct2geojson_feature
},
FORMAT.xml_doc_as_string: {
FORMAT.etree_doc: FormatConverter.string2etree_doc,
FORMAT.string: FormatConverter.no_op
},
FORMAT.line_stream: {
FORMAT.string: FormatConverter.no_op
},
FORMAT.xml_line_stream: {
FORMAT.string: FormatConverter.no_op
}
}
|