def parse_farm(xml_document): """Parse locate xml document from requesting GeoServer. :param xml_document: XML document from GeoServer returns. :type xml_document: basestring :returns: Dictionary that contains envelope, farm id, etc :rtype: dict """ feature = {} try: xmldoc = minidom.parseString(xml_document) except ExpatError: return feature try: wfs_xml_feature = xmldoc.getElementsByTagName('gml:featureMember')[0] id_tag = settings.FARM_WORKSPACE + ':' + settings.FARM_ID_COLUMN farm_id_xml = wfs_xml_feature.getElementsByTagName(id_tag)[0] feature['farm_id'] = farm_id_xml.childNodes[0].nodeValue geom_gml = wfs_xml_feature.getElementsByTagName( 'kartoza:wkb_geometry')[0] geom_gml_dom = geom_gml.childNodes[0] envelope = GEOSGeometry.from_gml(geom_gml_dom.toxml()) feature['envelope_extent'] = envelope.extent feature['envelope_centroid'] = envelope.centroid.coords return feature except IndexError: return feature
def parse_locate_return(xml_document, with_envelope=False): """Parse locate xml document from requesting GeoServer. :param xml_document: XML document from GeoServer returns. :type xml_document: basestring :returns: A dictionary of farm ID as the key and its envelope as the value. :rtype: dict """ xmldoc = minidom.parseString(xml_document) features = {} try: wfs_xml_features = xmldoc.getElementsByTagName('wfs:member') for wfs_xml_feature in wfs_xml_features: id_tag = settings.FARM_WORKSPACE + ':' + settings.FARM_ID_COLUMN farm_id_xml = wfs_xml_feature.getElementsByTagName(id_tag)[0] farm_id = farm_id_xml.childNodes[0].nodeValue if with_envelope: bounded_gml = wfs_xml_feature.getElementsByTagName( 'gml:boundedBy')[0] bounded_gml_dom = bounded_gml.childNodes[0] envelope = GEOSGeometry.from_gml( bounded_gml_dom.toxml()).extent else: envelope = None features[farm_id] = envelope return features except IndexError: return features
def from_xml(cls, element: Element): """Push the whole <gml:...> element into the GEOS parser. This avoids having to support the whole GEOS logic. GML is a complex beast with many different forms for the same thing: http://erouault.blogspot.com/2014/04/gml-madness.html """ srs = CRS.from_string(get_attribute(element, "srsName")) # Push the whole <gml:...> element into the GEOS parser. # This avoids having to support the whole GEOS logic. geos_data = GEOSGeometry.from_gml(tostring(element)) geos_data.srid = srs.srid return cls(srs=srs, geos_data=geos_data)