Example #1
0
 def test4(self):
     self.r = records.Exposure(id='1',
                               category='population',
                               taxonomySource='',
                               description='test',
                               area_type='per_asset',
                               area_unit='')
     self.assertEqual(
         self.r.to_tuple(),
         ('1', 'population', '', 'test', 'per_asset', None, None, None))
     self.assertFalse(self.r.is_valid())
Example #2
0
    def node_to_records(cls, node):
        """
        Convert the node into a sequence of Exposure records
        """
        taxonomysource = node.attrib.get('taxonomySource', 'UNKNOWN')
        if node['category'] == 'buildings':
            for c in node.conversions.costTypes:
                yield records.CostType(c['name'], c['type'], c['unit'],
                                       c.attrib.get('retrofittedType', ''),
                                       c.attrib.get('retrofittedUnit', ''))
            conv = node.conversions
            try:
                area = conv.area
                area_type = area['type']
                area_unit = area['unit']
            except NameError:
                area_type = ''
                area_unit = ''
            yield records.Exposure(node['id'],
                                   node['category'], taxonomysource,
                                   node.description.text.strip(), area_type,
                                   area_unit, conv.deductible['isAbsolute'],
                                   conv.insuranceLimit['isAbsolute'])
        else:
            yield records.Exposure(node['id'],
                                   node['category'], taxonomysource,
                                   node.description.text.strip())

        locations = {}  # location -> id
        loc_counter = itertools.count(1)
        for asset in node.assets:
            asset_ref = asset['id']

            # convert occupancies
            try:
                occupancies = asset.occupancies
            except NameError:
                occupancies = []
            for occupancy in occupancies:
                yield records.Occupancy(asset_ref, occupancy['period'],
                                        occupancy['occupants'])

            # convert costs
            try:
                costs = asset.costs
            except NameError:
                costs = []
            for cost in costs:
                yield records.Cost(asset_ref, cost['type'], cost['value'],
                                   cost.attrib.get('retrofitted', ''),
                                   cost.attrib.get('deductible', ''),
                                   cost.attrib.get('insuranceLimit', ''))

            # convert locations
            loc = asset.location['lon'], asset.location['lat']
            try:  # known location
                loc_id = locations[loc]
            except KeyError:  # yield only new locations
                loc_id = locations[loc] = str(loc_counter.next())
                yield records.Location(loc_id, loc[0], loc[1])

            # convert assets
            yield records.Asset(loc_id, asset_ref, asset['taxonomy'],
                                asset['number'], asset.attrib.get('area', ''))