def write_metadata(self, metadata): """ Write gml:ids and other meta data for `nrml`, `riskResult`, and `lossMap` XML elements. :param metadata: A dict containing metadata about the loss map. For example:: {'nrmlID': 'n1', 'riskResultID': 'rr1', 'lossMapID': 'lm1', 'endBranchLabel': 'vf1', 'lossCategory': 'economic_loss', 'unit': 'EUR'} If any of these items are not defined in the dict, default values will be used. :type metadata: dict """ # set gml:id attributes: for node, key in ( (self.root_node, 'nrmlID'), (self.risk_result_node, 'riskResultID'), (self.loss_map_node, 'lossMapID')): nrml.set_gml_id( node, metadata.get(key, self.DEFAULT_METADATA[key])) # set the rest of the <lossMap> attributes for key in ('endBranchLabel', 'lossCategory', 'unit'): self.loss_map_node.set( key, metadata.get(key, self.DEFAULT_METADATA[key]))
def write_metadata(self, metadata): """ Write gml:ids and other meta data for `nrml`, `riskResult`, and `lossMap` XML elements. :param metadata: A dict containing metadata about the loss map. For example:: {'nrmlID': 'n1', 'riskResultID': 'rr1', 'lossMapID': 'lm1', 'endBranchLabel': 'vf1', 'lossCategory': 'economic_loss', 'unit': 'EUR'} If any of these items are not defined in the dict, default values will be used. :type metadata: dict """ # set gml:id attributes: for node, key in ((self.root_node, 'nrmlID'), (self.risk_result_node, 'riskResultID'), (self.loss_map_node, 'lossMapID')): nrml.set_gml_id(node, metadata.get(key, self.DEFAULT_METADATA[key])) # set the rest of the <lossMap> attributes for key in ('endBranchLabel', 'lossCategory', 'unit'): self.loss_map_node.set( key, metadata.get(key, self.DEFAULT_METADATA[key]))
def _generate_lmnode(self, site): """ convenience method to generate a new lmnode """ # Generate an id for the new LMNode # Note: ids are created start at '1' self.lmnode_counter += 1 lmnode_id = "lmn_%i" % self.lmnode_counter # Create the new LMNode lmnode_el = etree.SubElement(self.loss_map_node, xml.RISK_LMNODE_TAG) # Set the gml:id nrml.set_gml_id(lmnode_el, lmnode_id) # We also need Site, gml:Point, and gml:pos nodes # for the new LMNode. # Each one (site, point, pos) is the parent of the next. site_el = etree.SubElement(lmnode_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (site.longitude, site.latitude) return lmnode_el
def _generate_map_node(self, site): """ convenience method to generate a new map node """ # Generate an id for the new node element # Note: ids are created start at '1' self.node_counter += 1 map_node_id = "mn_%i" % self.node_counter # Create the new node element map_node_el = etree.SubElement(self.map_container, self.MAP_NODE_TAG) # Set the gml:id nrml.set_gml_id(map_node_el, map_node_id) # We also need Site, gml:Point, and gml:pos nodes # for the new map node. # Each one (site, point, pos) is the parent of the next. site_el = etree.SubElement(map_node_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (site.longitude, site.latitude) return map_node_el
def write(self, point, _values): if self.root_node is None: # nrml:nrml, needs gml:id self._create_root_element() nrml.set_gml_id(self.root_node, nrml.NRML_DEFAULT_ID) # nrml:riskResult, needs gml:id result_el = etree.SubElement(self.root_node, xml.RISK_RESULT_TAG) nrml.set_gml_id(result_el, nrml.RISKRESULT_DEFAULT_ID) etree.SubElement(result_el, xml.NRML_CONFIG_TAG) self.result_el = result_el
def write_metadata(self, metadata): """ Write gml:ids and other meta data for `nrml`, `riskResult`, and map container XML elements. :param metadata: A dict containing metadata about the map. """ # set gml:id attributes: for node, key in ((self.root_node, 'nrmlID'), (self.risk_result_node, 'riskResultID'), (self.map_container, self.CONTAINER_ID_ATTRIBUTE)): nrml.set_gml_id(node, metadata.get(key, self.UNDEFINED)) # set the rest of the map container attributes for key in self.METADATA: self.map_container.set(key, str(metadata.get(key, self.UNDEFINED)))
def write_metadata(self, metadata): """ Write gml:ids and other meta data for `nrml`, `riskResult`, and map container XML elements. :param metadata: A dict containing metadata about the map. """ # set gml:id attributes: for node, key in ( (self.root_node, 'nrmlID'), (self.risk_result_node, 'riskResultID'), (self.map_container, self.CONTAINER_ID_ATTRIBUTE)): nrml.set_gml_id(node, metadata.get(key, self.UNDEFINED)) # set the rest of the map container attributes for key in self.METADATA: self.map_container.set(key, str(metadata.get(key, self.UNDEFINED)))
def write(self, point, values): if isinstance(point, shapes.GridPoint): point = point.site asset_object = values[1] if self.root_node is None: # nrml:nrml, needs gml:id self._create_root_element() if 'nrml_id' in asset_object: nrml.set_gml_id(self.root_node, str(asset_object['nrml_id'])) else: nrml.set_gml_id(self.root_node, nrml.NRML_DEFAULT_ID) # nrml:riskResult, needs gml:id result_el = etree.SubElement(self.root_node, xml.RISK_RESULT_TAG) if 'riskres_id' in asset_object: nrml.set_gml_id(result_el, str(asset_object['riskres_id'])) else: nrml.set_gml_id(result_el, nrml.RISKRESULT_DEFAULT_ID) etree.SubElement(result_el, xml.NRML_CONFIG_TAG) self.result_el = result_el
def write(self, point, values): """Writes an asset element with loss map ratio information. :param point: the point of the grid we want to compute :type point: :py:class:`openquake.shapes.Site`, :py:class:`openquake.shapes.GridPoint` :param values: is a pair of (loss map values, asset_object) :type values: with the following members :py:class:`openquake.shapes.Curve` :py:class:`dict` (asset_object) ***assetID*** - the assetID ***endBranchLabel*** - endBranchLabel ***riskres_id*** - for example, 'rr' ***list_id*** - 'list' """ super(CurveXMLWriter, self).write(point, values) if isinstance(point, shapes.GridPoint): point = point.site (curve_object, asset_object) = values # container element, needs gml:id if self.curve_list_el is None: self.curve_list_el = etree.SubElement(self.result_el, self.container_tag) if 'list_id' in asset_object: nrml.set_gml_id(self.curve_list_el, str( asset_object['list_id'])) else: nrml.set_gml_id(self.curve_list_el, self.CONTAINER_DEFAULT_ID) asset_id = str(asset_object['assetID']) try: asset_el = self.assets_per_id[asset_id] except KeyError: # nrml:asset, needs gml:id asset_el = etree.SubElement(self.curve_list_el, xml.RISK_ASSET_TAG) nrml.set_gml_id(asset_el, asset_id) self.assets_per_id[asset_id] = asset_el # check if nrml:site is already existing site_el = asset_el.find(xml.RISK_SITE_TAG) if site_el is None: site_el = etree.SubElement(asset_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (point.longitude, point.latitude) elif not xml.element_equal_to_site(site_el, point): error_msg = "asset %s cannot have two differing sites: %s, %s " \ % (asset_id, xml.lon_lat_from_site(site_el), point) raise ValueError(error_msg) # loss/loss ratio curves - sub-element already created? curves_el = asset_el.find(self.curves_tag) if curves_el is None: curves_el = etree.SubElement(asset_el, self.curves_tag) curve_el = etree.SubElement(curves_el, self.curve_tag) # attribute for endBranchLabel (optional) if 'endBranchLabel' in asset_object: curve_el.set(xml.RISK_END_BRANCH_ATTR_NAME, str(asset_object[xml.RISK_END_BRANCH_ATTR_NAME])) abscissa_el = etree.SubElement(curve_el, self.abscissa_tag) abscissa_el.text = _curve_vals_as_gmldoublelist(curve_object) poe_el = etree.SubElement(curve_el, xml.RISK_POE_TAG) poe_el.text = _curve_poe_as_gmldoublelist(curve_object)
def write(self, point, values): """Writes an asset element with loss map ratio information. This method assumes that `riskResult` and `lossMap` element data has already been written. :param point: the region location of the data being written :type point: :py:class:`openquake.shapes.Site` :param values: is a pair of (loss dict, asset dict) :type values: tuple with the following members :py:class:`dict` (loss dict) with the following keys: ***mean_loss*** - the Mean Loss for a certain Node/Site ***stddev_loss*** - the Standard Deviation for a certain Node/Site :py:class:`dict` (asset dict) ***assetID*** - the assetID """ def new_loss_node(lmnode_el, loss_dict, asset_dict): """ Create a new asset loss node under a pre-existing parent LMNode. """ loss_el = etree.SubElement(lmnode_el, xml.RISK_LOSS_MAP_LOSS_CONTAINER_TAG) loss_el.set(xml.RISK_LOSS_MAP_ASSET_REF_TAG, str(asset_dict['assetID'])) mean_loss = etree.SubElement(loss_el, xml.RISK_LOSS_MAP_MEAN_LOSS_TAG) mean_loss.text = "%s" % loss_dict['mean_loss'] stddev = etree.SubElement(loss_el, xml.RISK_LOSS_MAP_STANDARD_DEVIATION_TAG) stddev.text = "%s" % loss_dict['stddev_loss'] loss_dict, asset_dict = values # search for a Site xml node matching the input `point` # Note: The following function performs a linear search # and could be expensive when dealing with large loss maps. site_node = self._get_site_elem_for_site(point) if site_node is not None: # append a new loss element to an existing LMNode lmnode_el = site_node.getparent() new_loss_node(lmnode_el, loss_dict, asset_dict) else: # Generate an id for the new LMNode # Note: ids are created start at '1' self.lmnode_counter += 1 lmnode_id = "lmn_%i" % self.lmnode_counter # Create the new LMNode lmnode_el = etree.SubElement(self.loss_map_node, xml.RISK_LMNODE_TAG) # Set the gml:id nrml.set_gml_id(lmnode_el, lmnode_id) # We also need Site, gml:Point, and gml:pos nodes # for the new LMNode. # Each one (site, point, pos) is the parent of the next. site_el = etree.SubElement(lmnode_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (point.longitude, point.latitude) # now add the loss node as a child of the LMNode new_loss_node(lmnode_el, loss_dict, asset_dict)
def write(self, point, values): """Writes an asset element with loss map ratio information. :param point: the point of the grid we want to compute :type point: :py:class:`openquake.shapes.Site`, :py:class:`openquake.shapes.GridPoint` :param values: is a pair of (loss map values, asset_object) :type values: with the following members :py:class:`openquake.shapes.Curve` :py:class:`dict` (asset_object) ***assetID*** - the assetID ***endBranchLabel*** - endBranchLabel ***riskres_id*** - for example, 'rr' ***list_id*** - 'list' """ super(CurveXMLWriter, self).write(point, values) if isinstance(point, shapes.GridPoint): point = point.site (curve_object, asset_object) = values # container element, needs gml:id if self.curve_list_el is None: self.curve_list_el = etree.SubElement(self.result_el, self.container_tag) if 'list_id' in asset_object: nrml.set_gml_id(self.curve_list_el, str(asset_object['list_id'])) else: nrml.set_gml_id(self.curve_list_el, self.CONTAINER_DEFAULT_ID) asset_id = str(asset_object['assetID']) try: asset_el = self.assets_per_id[asset_id] except KeyError: # nrml:asset, needs gml:id asset_el = etree.SubElement(self.curve_list_el, xml.RISK_ASSET_TAG) nrml.set_gml_id(asset_el, asset_id) self.assets_per_id[asset_id] = asset_el # check if nrml:site is already existing site_el = asset_el.find(xml.RISK_SITE_TAG) if site_el is None: site_el = etree.SubElement(asset_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (point.longitude, point.latitude) elif not xml.element_equal_to_site(site_el, point): error_msg = "asset %s cannot have two differing sites: %s, %s " \ % (asset_id, xml.lon_lat_from_site(site_el), point) raise ValueError(error_msg) # loss/loss ratio curves - sub-element already created? curves_el = asset_el.find(self.curves_tag) if curves_el is None: curves_el = etree.SubElement(asset_el, self.curves_tag) curve_el = etree.SubElement(curves_el, self.curve_tag) # attribute for endBranchLabel (optional) if 'endBranchLabel' in asset_object: curve_el.set(xml.RISK_END_BRANCH_ATTR_NAME, str(asset_object[xml.RISK_END_BRANCH_ATTR_NAME])) abscissa_el = etree.SubElement(curve_el, self.abscissa_tag) abscissa_el.text = _curve_vals_as_gmldoublelist(curve_object) poe_el = etree.SubElement(curve_el, xml.RISK_POE_TAG) poe_el.text = _curve_poe_as_gmldoublelist(curve_object)
def write(self, point, values): """Writes an asset element with loss map ratio information. :param point: the point of the grid we want to compute :type point: :py:class:`openquake.shapes.Site`, :py:class:`openquake.shapes.GridPoint` :param values: is a pair of (loss map values, asset) :type values: with the following members :py:class:`openquake.shapes.Curve` :py:class:`openquake.db.models.ExposureData`` """ super(CurveXMLWriter, self).write(point, values) if isinstance(point, shapes.GridPoint): point = point.site (curve_object, asset) = values # container element, needs gml:id if self.curve_list_el is None: self.curve_list_el = etree.SubElement(self.result_el, self.container_tag) nrml.set_gml_id(self.curve_list_el, self.CONTAINER_DEFAULT_ID) try: asset_el = self.assets_per_id[asset.asset_ref] except KeyError: # nrml:asset, needs gml:id asset_el = etree.SubElement(self.curve_list_el, xml.RISK_ASSET_TAG) nrml.set_gml_id(asset_el, asset.asset_ref) self.assets_per_id[asset.asset_ref] = asset_el # check if nrml:site is already existing site_el = asset_el.find(xml.RISK_SITE_TAG) if site_el is None: site_el = etree.SubElement(asset_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (point.longitude, point.latitude) elif not xml.element_equal_to_site(site_el, point): error_msg = "asset %s cannot have two differing sites: %s, %s " \ % (asset.asset_ref, xml.lon_lat_from_site(site_el), point) raise ValueError(error_msg) # loss/loss ratio curves - sub-element already created? curves_el = asset_el.find(self.curves_tag) if curves_el is None: curves_el = etree.SubElement(asset_el, self.curves_tag) curve_el = etree.SubElement(curves_el, self.curve_tag) abscissa_el = etree.SubElement(curve_el, self.abscissa_tag) abscissa_el.text = _curve_vals_as_gmldoublelist(curve_object) poe_el = etree.SubElement(curve_el, xml.RISK_POE_TAG) poe_el.text = _curve_poe_as_gmldoublelist(curve_object)
def write(self, point, values): """Writes an asset element with loss map ratio information. This method assumes that `riskResult` and `lossMap` element data has already been written. :param point: the region location of the data being written :type point: :py:class:`openquake.shapes.Site` :param values: is a pair of (loss dict, asset dict) :type values: tuple with the following members :py:class:`dict` (loss dict) with the following keys: ***mean_loss*** - the Mean Loss for a certain Node/Site ***stddev_loss*** - the Standard Deviation for a certain Node/Site :py:class:`dict` (asset dict) ***assetID*** - the assetID """ def new_loss_node(lmnode_el, loss_dict, asset_dict): """ Create a new asset loss node under a pre-existing parent LMNode. """ loss_el = etree.SubElement(lmnode_el, xml.RISK_LOSS_MAP_LOSS_CONTAINER_TAG) loss_el.set(xml.RISK_LOSS_MAP_ASSET_REF_TAG, str(asset_dict['assetID'])) mean_loss = etree.SubElement( loss_el, xml.RISK_LOSS_MAP_MEAN_LOSS_TAG) mean_loss.text = "%s" % loss_dict['mean_loss'] stddev = etree.SubElement(loss_el, xml.RISK_LOSS_MAP_STANDARD_DEVIATION_TAG) stddev.text = "%s" % loss_dict['stddev_loss'] loss_dict, asset_dict = values # search for a Site xml node matching the input `point` # Note: The following function performs a linear search # and could be expensive when dealing with large loss maps. site_node = self._get_site_elem_for_site(point) if site_node is not None: # append a new loss element to an existing LMNode lmnode_el = site_node.getparent() new_loss_node(lmnode_el, loss_dict, asset_dict) else: # Generate an id for the new LMNode # Note: ids are created start at '1' self.lmnode_counter += 1 lmnode_id = "lmn_%i" % self.lmnode_counter # Create the new LMNode lmnode_el = etree.SubElement(self.loss_map_node, xml.RISK_LMNODE_TAG) # Set the gml:id nrml.set_gml_id(lmnode_el, lmnode_id) # We also need Site, gml:Point, and gml:pos nodes # for the new LMNode. # Each one (site, point, pos) is the parent of the next. site_el = etree.SubElement(lmnode_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (point.longitude, point.latitude) # now add the loss node as a child of the LMNode new_loss_node(lmnode_el, loss_dict, asset_dict)
def write(self, point, values): """Writes an asset element with loss/loss ratio information. point must be of type shapes.Site or shapes.GridPoint values is a pair of (curve_object, asset_object), with curve_object of type shapes.Curve. asset_object is a dictionary that looks like: {'assetID': foo, # this is the only required item 'nrml_id': 'nrml', 'riskres_id': 'rr', 'list_id': 'list', 'endBranchLabel' : '1_1' } """ if isinstance(point, shapes.GridPoint): point = point.site (curve_object, asset_object) = values # if we are writing the first hazard curve, create wrapping elements if self.root_node is None: # nrml:nrml, needs gml:id self._create_root_element() if 'nrml_id' in asset_object: nrml.set_gml_id(self.root_node, str(asset_object['nrml_id'])) else: nrml.set_gml_id(self.root_node, nrml.NRML_DEFAULT_ID) # nrml:riskResult, needs gml:id result_el = etree.SubElement(self.root_node, xml.RISK_RESULT_TAG) if 'riskres_id' in asset_object: nrml.set_gml_id(result_el, str(asset_object['riskres_id'])) else: nrml.set_gml_id(result_el, nrml.RISKRESULT_DEFAULT_ID) # container element, needs gml:id self.curve_list_el = etree.SubElement(result_el, self.container_tag) if 'list_id' in asset_object: nrml.set_gml_id(self.curve_list_el, str(asset_object['list_id'])) else: nrml.set_gml_id(self.curve_list_el, self.CONTAINER_DEFAULT_ID) asset_id = str(asset_object['assetID']) try: asset_el = self.assets_per_id[asset_id] except KeyError: # nrml:asset, needs gml:id asset_el = etree.SubElement(self.curve_list_el, xml.RISK_ASSET_TAG) nrml.set_gml_id(asset_el, asset_id) self.assets_per_id[asset_id] = asset_el # check if nrml:site is already existing site_el = asset_el.find(xml.RISK_SITE_TAG) if site_el is None: site_el = etree.SubElement(asset_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (point.longitude, point.latitude) elif not xml.element_equal_to_site(site_el, point): error_msg = "asset %s cannot have two differing sites: %s, %s " \ % (asset_id, xml.lon_lat_from_site(site_el), point) raise ValueError(error_msg) # loss/loss ratio curves - sub-element already created? curves_el = asset_el.find(self.curves_tag) if curves_el is None: curves_el = etree.SubElement(asset_el, self.curves_tag) curve_el = etree.SubElement(curves_el, self.curve_tag) # attribute for endBranchLabel (optional) if 'endBranchLabel' in asset_object: curve_el.set(xml.RISK_END_BRANCH_ATTR_NAME, str(asset_object[xml.RISK_END_BRANCH_ATTR_NAME])) abscissa_el = etree.SubElement(curve_el, self.abscissa_tag) abscissa_el.text = _curve_vals_as_gmldoublelist(curve_object) poe_el = etree.SubElement(curve_el, xml.RISK_POE_TAG) poe_el.text = _curve_poe_as_gmldoublelist(curve_object)
def write(self, site, values): """Writes an asset element with loss map ratio information. This method assumes that `riskResult` and `lossMap` element data has already been written. :param site: the region location of the data being written :type site: :py:class:`openquake.shapes.Site` :param values: contains a list of pairs in the form (loss dict, asset dict) with all the data to be written related to the given site :type values: tuple with the following members :py:class:`dict` (loss dict) with the following keys: ***mean_loss*** - the Mean Loss for a certain Node/Site ***stddev_loss*** - the Standard Deviation for a certain Node/Site :py:class:`dict` (asset dict) ***assetID*** - the assetID """ def new_loss_node(lmnode_el, loss_dict, asset_dict): """ Create a new asset loss node under a pre-existing parent LMNode. """ loss_el = etree.SubElement(lmnode_el, xml.RISK_LOSS_MAP_LOSS_CONTAINER_TAG) loss_el.set(xml.RISK_LOSS_MAP_ASSET_REF_TAG, str(asset_dict['assetID'])) mean_loss = etree.SubElement( loss_el, xml.RISK_LOSS_MAP_MEAN_LOSS_TAG) mean_loss.text = "%s" % loss_dict['mean_loss'] stddev = etree.SubElement(loss_el, xml.RISK_LOSS_MAP_STANDARD_DEVIATION_TAG) stddev.text = "%s" % loss_dict['stddev_loss'] # Generate an id for the new LMNode # Note: ids are created start at '1' self.lmnode_counter += 1 lmnode_id = "lmn_%i" % self.lmnode_counter # Create the new LMNode lmnode_el = etree.SubElement(self.loss_map_node, xml.RISK_LMNODE_TAG) # Set the gml:id nrml.set_gml_id(lmnode_el, lmnode_id) # We also need Site, gml:Point, and gml:pos nodes # for the new LMNode. # Each one (site, point, pos) is the parent of the next. site_el = etree.SubElement(lmnode_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (site.longitude, site.latitude) # now add the loss nodes as a child of the LMNode # we have loss data in first position, asset data in second position # ({'stddev_loss': 100, 'mean_loss': 0}, {'assetID': 'a1711'}) for value in values: new_loss_node(lmnode_el, value[0], value[1])
def write(self, point, values): """Writes an asset element with loss/loss ratio information. point must be of type shapes.Site or shapes.GridPoint values is a pair of (curve_object, asset_object), with curve_object of type shapes.Curve. asset_object is a dictionary that looks like: {'assetID': foo, # this is the only required item 'nrml_id': 'nrml', 'riskres_id': 'rr', 'list_id': 'list', 'endBranchLabel' : '1_1' } """ if isinstance(point, shapes.GridPoint): point = point.site (curve_object, asset_object) = values # if we are writing the first hazard curve, create wrapping elements if self.root_node is None: # nrml:nrml, needs gml:id self._create_root_element() if 'nrml_id' in asset_object: nrml.set_gml_id(self.root_node, str(asset_object['nrml_id'])) else: nrml.set_gml_id(self.root_node, nrml.NRML_DEFAULT_ID) # nrml:riskResult, needs gml:id result_el = etree.SubElement(self.root_node, xml.RISK_RESULT_TAG) if 'riskres_id' in asset_object: nrml.set_gml_id(result_el, str(asset_object['riskres_id'])) else: nrml.set_gml_id(result_el, nrml.RISKRESULT_DEFAULT_ID) # container element, needs gml:id self.curve_list_el = etree.SubElement(result_el, self.container_tag) if 'list_id' in asset_object: nrml.set_gml_id(self.curve_list_el, str( asset_object['list_id'])) else: nrml.set_gml_id(self.curve_list_el, self.CONTAINER_DEFAULT_ID) asset_id = str(asset_object['assetID']) try: asset_el = self.assets_per_id[asset_id] except KeyError: # nrml:asset, needs gml:id asset_el = etree.SubElement(self.curve_list_el, xml.RISK_ASSET_TAG) nrml.set_gml_id(asset_el, asset_id) self.assets_per_id[asset_id] = asset_el # check if nrml:site is already existing site_el = asset_el.find(xml.RISK_SITE_TAG) if site_el is None: site_el = etree.SubElement(asset_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (point.longitude, point.latitude) elif not xml.element_equal_to_site(site_el, point): error_msg = "asset %s cannot have two differing sites: %s, %s " \ % (asset_id, xml.lon_lat_from_site(site_el), point) raise ValueError(error_msg) # loss/loss ratio curves - sub-element already created? curves_el = asset_el.find(self.curves_tag) if curves_el is None: curves_el = etree.SubElement(asset_el, self.curves_tag) curve_el = etree.SubElement(curves_el, self.curve_tag) # attribute for endBranchLabel (optional) if 'endBranchLabel' in asset_object: curve_el.set(xml.RISK_END_BRANCH_ATTR_NAME, str(asset_object[xml.RISK_END_BRANCH_ATTR_NAME])) abscissa_el = etree.SubElement(curve_el, self.abscissa_tag) abscissa_el.text = _curve_vals_as_gmldoublelist(curve_object) poe_el = etree.SubElement(curve_el, xml.RISK_POE_TAG) poe_el.text = _curve_poe_as_gmldoublelist(curve_object)
def write(self, site, values): """Writes an asset element with loss map ratio information. This method assumes that `riskResult` and `lossMap` element data has already been written. :param site: the region location of the data being written :type site: :py:class:`openquake.shapes.Site` :param values: contains a list of pairs in the form (loss dict, asset dict) with all the data to be written related to the given site :type values: tuple with the following members :py:class:`dict` (loss dict) with the following keys: ***mean_loss*** - the Mean Loss for a certain Node/Site ***stddev_loss*** - the Standard Deviation for a certain Node/Site :py:class:`dict` (asset dict) ***assetID*** - the assetID """ def new_loss_node(lmnode_el, loss_dict, asset_dict): """ Create a new asset loss node under a pre-existing parent LMNode. """ loss_el = etree.SubElement(lmnode_el, xml.RISK_LOSS_MAP_LOSS_CONTAINER_TAG) loss_el.set(xml.RISK_LOSS_MAP_ASSET_REF_TAG, str(asset_dict['assetID'])) mean_loss = etree.SubElement(loss_el, xml.RISK_LOSS_MAP_MEAN_LOSS_TAG) mean_loss.text = "%s" % loss_dict['mean_loss'] stddev = etree.SubElement(loss_el, xml.RISK_LOSS_MAP_STANDARD_DEVIATION_TAG) stddev.text = "%s" % loss_dict['stddev_loss'] # Generate an id for the new LMNode # Note: ids are created start at '1' self.lmnode_counter += 1 lmnode_id = "lmn_%i" % self.lmnode_counter # Create the new LMNode lmnode_el = etree.SubElement(self.loss_map_node, xml.RISK_LMNODE_TAG) # Set the gml:id nrml.set_gml_id(lmnode_el, lmnode_id) # We also need Site, gml:Point, and gml:pos nodes # for the new LMNode. # Each one (site, point, pos) is the parent of the next. site_el = etree.SubElement(lmnode_el, xml.RISK_SITE_TAG) point_el = etree.SubElement(site_el, xml.GML_POINT_TAG) point_el.set(xml.GML_SRS_ATTR_NAME, xml.GML_SRS_EPSG_4326) pos_el = etree.SubElement(point_el, xml.GML_POS_TAG) pos_el.text = "%s %s" % (site.longitude, site.latitude) # now add the loss nodes as a child of the LMNode # we have loss data in first position, asset data in second position # ({'stddev_loss': 100, 'mean_loss': 0}, {'assetID': 'a1711'}) for value in values: new_loss_node(lmnode_el, value[0], value[1])