Ejemplo n.º 1
0
    def test_serialize_a_model_xml(self):
        expected = io.BytesIO(b"""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8" statistics="mean" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" value="15.23"/>
      <loss assetRef="asset_2" value="16.23"/>
    </node>
    <node>
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_3" value="17.23"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(self.filename,
                                          investigation_time=10.0,
                                          poe=0.8,
                                          statistics="mean",
                                          loss_type="structural")

        writer.serialize(self.data)

        _utils.assert_xml_equal(expected, self.filename)
Ejemplo n.º 2
0
    def test_serialize_optional_metadata_xml(self):
        expected = io.BytesIO(b"""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8" statistics="quantile"
        quantileValue="0.5" lossCategory="economic" unit="USD" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" mean="15.23" stdDev="2"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(
            self.filename,
            investigation_time=10.0, poe=0.80, statistics="quantile",
            quantile_value=0.50, unit="USD", loss_category="economic",
            loss_type="structural"
        )

        data = [LOSS_NODE(
            asset_ref="asset_1", location=writers.Site(1.0, 1.5),
            value=15.23, std_dev=2)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
Ejemplo n.º 3
0
    def test_serialize_using_hazard_realization_xml(self):
        expected = io.BytesIO(b"""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8"
           sourceModelTreePath="b1|b2" gsimTreePath="b3|b4"
           lossCategory="economic" unit="USD" lossType="structural">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" value="15.23"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(
            self.filename,
            investigation_time=10.0, poe=0.80, source_model_tree_path="b1|b2",
            gsim_tree_path="b3|b4", unit="USD", loss_category="economic",
            loss_type="structural")

        data = [LOSS_NODE(asset_ref="asset_1",
                          location=writers.Site(1.0, 1.5),
                          value=15.23, std_dev=None)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
Ejemplo n.º 4
0
    def test_empty_model_not_supported_xml(self):
        writer = writers.LossMapXMLWriter(
            self.filename, investigation_time=10.0, poe=0.5,
            statistics="mean", loss_type="structural"
        )

        self.assertRaises(ValueError, writer.serialize, [])
        self.assertRaises(ValueError, writer.serialize, None)
Ejemplo n.º 5
0
def export_lossmaps_xml(ekey, dstore):
    oq = OqParam.from_(dstore.attrs)
    unit_by_lt = {
        riskmodels.cost_type_to_loss_type(ct['name']): ct['unit']
        for ct in dstore['cost_types']
    }
    unit_by_lt['fatalities'] = 'people'
    rlzs = dstore['rlzs_assoc'].realizations
    avglosses = dstore['avglosses']
    riskmodel = dstore['riskmodel']
    assetcol = dstore['assetcol']
    sitemesh = dstore['sitemesh']
    N, L, R = avglosses.shape
    fnames = []
    for l, r in itertools.product(range(L), range(R)):
        rlz = rlzs[r]
        lt = riskmodel.loss_types[l]
        unit = unit_by_lt[lt]
        suffix = '' if L == 1 and R == 1 else '-gsimltp_%s_%s' % (rlz.uid, lt)
        fname = os.path.join(dstore.export_dir,
                             '%s%s.%s' % (ekey[0], suffix, ekey[1]))
        data = []
        for ass, stat in zip(assetcol, avglosses[:, l, r]):
            loc = Location(sitemesh[ass['site_id']])
            lm = LossMap(loc, ass['asset_ref'], stat['mean'], stat['stddev'])
            data.append(lm)
        writer = risk_writers.LossMapXMLWriter(fname,
                                               oq.investigation_time,
                                               poe=None,
                                               loss_type=lt,
                                               gsim_tree_path=None,
                                               unit=unit,
                                               loss_category=None)
        # TODO: replace the category with the exposure category
        writer.serialize(data)
        fnames.append(fname)
    return sorted(fnames)