def insert_datum(self, asset_data): """ Insert a single asset entry. :param asset_data: an instance of :class:`openquake.nrmllib.risk.parsers.AssetData` """ asset = models.ExposureData.objects.create( exposure_model=self.model, asset_ref=asset_data.asset_ref, taxonomy=asset_data.taxonomy, area=asset_data.area, number_of_units=asset_data.number, site="POINT(%s %s)" % (asset_data.site.longitude, asset_data.site.latitude)) for cost_type in self.cost_types: if not any( [cost_type == cost.cost_type for cost in asset_data.costs]): raise ValueError("Invalid Exposure. " "Missing cost %s for asset %s" % (cost_type, asset.asset_ref)) model = asset_data.exposure_metadata deductible_is_absolute = model.conversions.deductible_is_absolute insurance_limit_is_absolute = ( model.conversions.insurance_limit_is_absolute) for cost in asset_data.costs: cost_type = self.cost_types.get(cost.cost_type, None) if cost_type is None: raise ValueError("Invalid Exposure. Missing conversion " "for cost type %s" % cost.cost_type) if cost.retrofitted is not None: retrofitted = models.ExposureData.per_asset_value( cost.retrofitted, cost_type.retrofitted_conversion, asset_data.area, model.conversions.area_type, asset_data.number, model.asset_category) else: retrofitted = None converted_cost = models.ExposureData.per_asset_value( cost.value, cost_type.conversion, asset_data.area, model.conversions.area_type, asset_data.number, model.asset_category) models.Cost.objects.create( exposure_data=asset, cost_type=cost_type, converted_cost=converted_cost, converted_retrofitted_cost=retrofitted, deductible_absolute=models.make_absolute( cost.deductible, converted_cost, deductible_is_absolute), insurance_limit_absolute=models.make_absolute( cost.limit, converted_cost, insurance_limit_is_absolute)) for odata in asset_data.occupancy: models.Occupancy.objects.create(exposure_data=asset, occupants=odata.occupants, period=odata.period)
def insert_datum(self, asset_data): """ Insert a single asset entry. :param asset_data: an instance of :class:`openquake.commonlib.risk_parsers.AssetData` """ asset = models.ExposureData.objects.create( exposure_model=self.model, asset_ref=asset_data.asset_ref, taxonomy=asset_data.taxonomy, area=asset_data.area, number_of_units=asset_data.number, site="POINT(%s %s)" % (asset_data.site.longitude, asset_data.site.latitude)) for cost_type in self.cost_types: if not any([cost_type == cost.cost_type for cost in asset_data.costs]): raise ValueError("Invalid Exposure. " "Missing cost %s for asset %s" % ( cost_type, asset.asset_ref)) model = asset_data.exposure_metadata deductible_is_absolute = model.conversions.deductible_is_absolute insurance_limit_is_absolute = ( model.conversions.insurance_limit_is_absolute) for cost in asset_data.costs: cost_type = self.cost_types.get(cost.cost_type, None) if cost_type is None: raise ValueError("Invalid Exposure. Missing conversion " "for cost type %s" % cost.cost_type) if cost.retrofitted is not None: retrofitted = models.ExposureData.per_asset_value( cost.retrofitted, cost_type.retrofitted_conversion, asset_data.area, model.conversions.area_type, asset_data.number, model.asset_category) else: retrofitted = None converted_cost = models.ExposureData.per_asset_value( cost.value, cost_type.conversion, asset_data.area, model.conversions.area_type, asset_data.number, model.asset_category) models.Cost.objects.create( exposure_data=asset, cost_type=cost_type, converted_cost=converted_cost, converted_retrofitted_cost=retrofitted, deductible_absolute=models.make_absolute( cost.deductible, converted_cost, deductible_is_absolute), insurance_limit_absolute=models.make_absolute( cost.limit, converted_cost, insurance_limit_is_absolute)) for odata in asset_data.occupancy: models.Occupancy.objects.create(exposure_data=asset, occupants=odata.occupants, period=odata.period)
def insert_datum(self, point, occupancy, values, costs, conversions): """ Insert a single asset entry. :param list point: Asset location (format [lon, lat]). :param list occupancy: A potentially empty list of named tuples each one having an 'occupants' and a 'period' property. :param dict values: Asset attributes (see :class:`openquake.nrmllib.risk.parsers.ExposureModelParser`). :param list costs: A potentially empty list of named tuples each one having the following fields: type, value, retrofitted, deductible and limit. :param dict conversions: A potentially empty dict with conversion, mapping cost types to conversion types (per_area, aggregated, per_unit) It also inserts the main exposure model entry if not already present. """ if not self.model: self.model = models.ExposureModel.objects.create( input=self.exposure_input, name=values["exposureID"], description=values.get("description"), taxonomy_source=values.get("taxonomySource"), category=values["category"], area_type=values.get("areaType"), area_unit=values.get("areaUnit"), deductible_absolute=values.get("deductibleIsAbsolute"), insurance_limit_absolute=values.get("limitIsAbsolute")) for name, (conversion, unit, retrofitted_conversion, retrofitted_unit) in ( conversions.items()): self.conversions[name] = models.CostType.objects.create( exposure_model=self.model, name=name, conversion=conversion, unit=unit, retrofitted_conversion=retrofitted_conversion, retrofitted_unit=retrofitted_unit) asset = models.ExposureData.objects.create( exposure_model=self.model, asset_ref=values["id"], taxonomy=values.get("taxonomy"), area=values.get("area"), number_of_units=values.get("number"), site="POINT(%s %s)" % (point[0], point[1])) for cost_type in self.conversions: if not any([cost_type == cost.type for cost in costs]): raise ValueError("Invalid Exposure. " "Missing cost %s for asset %s" % ( cost_type, asset.asset_ref)) for cost in costs: cost_type = self.conversions.get(cost.type, None) if cost_type is None: raise ValueError("Invalid Exposure. Missing conversion " "for cost type %s" % cost.type) if cost.retrofitted is not None: retrofitted = models.ExposureData.per_asset_value( cost.retrofitted, cost_type.retrofitted_conversion, asset.area, values.get("areaType"), asset.number_of_units, values["category"]) else: retrofitted = None converted_cost = models.ExposureData.per_asset_value( cost.value, cost_type.conversion, asset.area, values.get("areaType"), asset.number_of_units, values["category"]) models.Cost.objects.create( exposure_data=asset, cost_type=cost_type, converted_cost=converted_cost, converted_retrofitted_cost=retrofitted, deductible_absolute=models.make_absolute( cost.deductible, cost, values.get("deductibleIsAbsolute", True)), insurance_limit_absolute=models.make_absolute( cost.limit, converted_cost, values.get("insuranceLimitIsAbsolute", True))) for odata in occupancy: models.Occupancy.objects.create(exposure_data=asset, occupants=odata.occupants, period=odata.period)