def to_node(self, doc, tag, ns_key=None, parent=None, check_validity=False, strict=DEFAULT_STRICT, exclude=()): if parent is None: parent = doc.getroot() if ns_key is None: node = create_new_node(doc, tag, parent=parent) else: node = create_new_node(doc, '{}:{}'.format(ns_key, tag), parent=parent) typ = self.GeometryType if typ is None: return node coords = getattr(self, typ).get_coordinates_list() if typ == 'Point': self._serialize_point(coords, doc, 'sfa:Point', node) elif typ == 'Line': self._serialize_line(coords, doc, 'sfa:Line', node) elif typ == 'LinearRing': self._serialize_line(coords, doc, 'sfa:LinearRing', node) elif typ == 'Polygon': self._serialize_polygon(coords, doc, 'sfa:Polygon', node) elif typ == 'MultiPoint': self._serialize_line(coords, doc, 'sfa:MultiPoint', node) elif typ == 'MultiLineString': self._serialize_multilinestring(coords, doc, 'sfa:MultiLineString', node) elif typ == 'MultiPolygon': self._serialize_multipolygon(coords, doc, 'sfa:MultiPolygon', node) else: raise ValueError('Unsupported serialization type {}'.format(typ)) return node
def to_node(self, doc, tag, ns_key=None, parent=None, check_validity=False, strict=DEFAULT_STRICT, exclude=()): if parent is None: parent = doc.getroot() if ns_key is None: node = create_new_node(doc, tag, parent=parent) else: node = create_new_node(doc, '{}:{}'.format(ns_key, tag), parent=parent) node.attrib['size'] = str(self.size) end_point_key = self._child_xml_ns_key.get('Vertex', ns_key) for entry in self.Vertex: entry.to_node(doc, 'Vertex', ns_key=end_point_key, parent=node, check_validity=check_validity, strict=strict) return node
def to_node(self, doc, tag, ns_key=None, parent=None, check_validity=False, strict=DEFAULT_STRICT, exclude=()): if parent is None: parent = doc.getroot() if ns_key is None: node = create_new_node(doc, tag, parent=parent) else: node = create_new_node(doc, '{}:{}'.format(ns_key, tag), parent=parent) if 'RemapLUT' in self._child_xml_ns_key: rtag = '{}:RemapLUT'.format(self._child_xml_ns_key['RemapLUT']) elif ns_key is not None: rtag = '{}:RemapLUT'.format(ns_key) else: rtag = 'RemapLUT' if self._remap_lut is not None: value = ' '.join('{0:d},{1:d},{2:d}'.format(*entry) for entry in self._remap_lut) entry = create_text_node(doc, rtag, value, parent=node) entry.attrib['size'] = str(self.size) return node
def to_node(self, doc, tag, ns_key=None, parent=None, check_validity=False, strict=DEFAULT_STRICT, exclude=()): def make_entry(arr): value = ' '.join(str(el) for el in arr) entry = create_text_node(doc, ltag, value, parent=node) entry.attrib['lut'] = str(arr.size) if parent is None: parent = doc.getroot() if ns_key is None: node = create_new_node(doc, tag, parent=parent) else: node = create_new_node(doc, '{}:{}'.format(ns_key, tag), parent=parent) if 'LUTValues' in self._child_xml_ns_key: ltag = '{}:LUTValues'.format(self._child_xml_ns_key['LUTValues']) elif ns_key is not None: ltag = '{}:LUTValues'.format(ns_key) else: ltag = 'LUTValues' if self._lut_values is not None: node.attrib['numLuts'] = str(self.numLUTs) node.attrib['size'] = str(self.size) if self._lut_values.ndim == 1: make_entry(self._lut_values) else: for j in range(self._lut_values.shape[1]): make_entry(self._lut_values[:, j]) return node
def to_node(self, doc, tag, ns_key=None, parent=None, check_validity=False, strict=DEFAULT_STRICT, exclude=()): if parent is None: parent = doc.getroot() if ns_key is None: node = create_new_node(doc, tag, parent=parent) else: node = create_new_node(doc, '{}:{}'.format(ns_key, tag), parent=parent) if 'Coefs' in self._child_xml_ns_key: ctag = '{}:Coef'.format(self._child_xml_ns_key['Coefs']) elif ns_key is not None: ctag = '{}:Coef'.format(ns_key) else: ctag = 'Coef' node.attrib['numPhasings'] = str(self.numPhasings) node.attrib['numPoints'] = str(self.numPoints) fmt_func = self._get_formatter('Coefs') for i, val1 in enumerate(self._coefs): for j, val in enumerate(val1): # if val != 0.0: # should we serialize it sparsely? cnode = create_text_node(doc, ctag, fmt_func(val), parent=node) cnode.attrib['phasing'] = str(i) cnode.attrib['point'] = str(j) return node
def to_node(self, doc, tag, ns_key=None, parent=None, check_validity=False, strict=DEFAULT_STRICT): if self.size == 0: return None # nothing to be done if ns_key is None: anode = create_new_node(doc, tag, parent=parent) else: anode = create_new_node(doc, '{}:{}'.format(ns_key, tag), parent=parent) for i, entry in enumerate(self._array): entry.to_node(doc, self._child_tag, ns_key=ns_key, parent=anode, check_validity=check_validity, strict=strict) return anode
def _serialize_point(coords, doc, tag, parent): if len(coords) < 2: raise ValueError('coords must have at least two elements') fmt_func = '{0:0.16G}'.format node = create_new_node(doc, tag, parent=parent) create_text_node(doc, 'sfa:X', fmt_func(coords[0]), parent=node) create_text_node(doc, 'sfa:Y', fmt_func(coords[1]), parent=node) if len(coords) > 2: create_text_node(doc, 'sfa:Z', fmt_func(coords[2]), parent=node) if len(coords) > 3: create_text_node(doc, 'sfa:M', fmt_func(coords[3]), parent=node)
def _serialize_multipolygon(self, coords, doc, tag, parent): node = create_new_node(doc, tag, parent=parent) for entry in coords: self._serialize_polygon(entry, doc, 'sfa:Element', node)
def _serialize_multilinestring(self, coords, doc, tag, parent): node = create_new_node(doc, tag, parent=parent) for entry in coords: self._serialize_line(entry, doc, 'sfa:Element', node)
def _serialize_polygon(self, coords, doc, tag, parent): node = create_new_node(doc, tag, parent=parent) for entry in coords: self._serialize_line(entry, doc, 'sfa:Ring', node)
def _serialize_line(self, coords, doc, tag, parent): node = create_new_node(doc, tag, parent=parent) for entry in coords: self._serialize_point(entry, doc, 'sfa:Vertex', node)