def __init__(self, taxonomy, rc, epsilon_sampling=0): self.taxonomy = taxonomy self.rc = rc self.epsilon_sampling = epsilon_sampling self.hc = rc.get_hazard_calculation() max_dist = rc.best_maximum_distance * 1000 # km to meters cursor = models.getcursor('job_init') hazard_exposure = models.extract_from([self.hc.oqjob], 'exposuremodel') if self.rc.exposure_model is hazard_exposure: # no need of geospatial queries, just join on the location self.assoc_query = cursor.mogrify("""\ WITH assocs AS ( SELECT %s, exp.id, hsite.id FROM riski.exposure_data AS exp JOIN hzrdi.hazard_site AS hsite ON exp.site::text = hsite.location::text WHERE hsite.hazard_calculation_id = %s AND exposure_model_id = %s AND taxonomy=%s AND ST_COVERS(ST_GeographyFromText(%s), exp.site) ) INSERT INTO riskr.asset_site (job_id, asset_id, site_id) SELECT * FROM assocs""", (rc.oqjob.id, self.hc.id, rc.exposure_model.id, taxonomy, rc.region_constraint.wkt)) else: # associate each asset to the closest hazard site self.assoc_query = cursor.mogrify("""\ WITH assocs AS ( SELECT DISTINCT ON (exp.id) %s, exp.id, hsite.id FROM riski.exposure_data AS exp JOIN hzrdi.hazard_site AS hsite ON ST_DWithin(exp.site, hsite.location, %s) WHERE hsite.hazard_calculation_id = %s AND exposure_model_id = %s AND taxonomy=%s AND ST_COVERS(ST_GeographyFromText(%s), exp.site) ORDER BY exp.id, ST_Distance(exp.site, hsite.location, false) ) INSERT INTO riskr.asset_site (job_id, asset_id, site_id) SELECT * FROM assocs""", (rc.oqjob.id, max_dist, self.hc.id, rc.exposure_model.id, taxonomy, rc.region_constraint.wkt)) # insert the associations for the current taxonomy with transaction.commit_on_success(using='job_init'): cursor.execute(self.assoc_query) # now read the associations just inserted self.asset_sites = models.AssetSite.objects.filter( job=rc.oqjob, asset__taxonomy=taxonomy) if not self.asset_sites: raise AssetSiteAssociationError( 'Could not associated any asset of taxonomy %s to ' 'hazard sites within the distance of %s km' % (taxonomy, self.rc.best_maximum_distance)) self.asset_ids = [a.asset_id for a in self.asset_sites] self.site_ids = [a.site_id for a in self.asset_sites] self.rupture_ids = {} self.epsilons_shape = {}
def __init__(self, taxonomy, calc): self.exposure_model = calc.exposure_model self.hazard_outputs = calc.get_hazard_outputs() self.taxonomy = taxonomy self.calc = calc self.oqparam = models.OqJob.objects.get( pk=calc.oqparam.hazard_calculation_id) self.calculation_mode = self.calc.oqparam.calculation_mode self.number_of_ground_motion_fields = self.oqparam.get_param( 'number_of_ground_motion_fields', 0) max_dist = calc.best_maximum_distance * 1000 # km to meters self.cursor = models.getcursor('job_init') hazard_exposure = models.extract_from([self.oqparam], 'exposuremodel') if self.exposure_model and hazard_exposure and \ self.exposure_model.id == hazard_exposure.id: # no need of geospatial queries, just join on the location self.assoc_query = self.cursor.mogrify( """\ WITH assocs AS ( SELECT %s, exp.id, hsite.id FROM riski.exposure_data AS exp JOIN hzrdi.hazard_site AS hsite ON exp.site::text = hsite.location::text WHERE hsite.hazard_calculation_id = %s AND exposure_model_id = %s AND taxonomy=%s AND ST_COVERS(ST_GeographyFromText(%s), exp.site) ) INSERT INTO riskr.asset_site (job_id, asset_id, site_id) SELECT * FROM assocs""", (self.calc.job.id, self.oqparam.id, self.exposure_model.id, taxonomy, self.calc.oqparam.region_constraint)) else: # associate each asset to the closest hazard site self.assoc_query = self.cursor.mogrify( """\ WITH assocs AS ( SELECT DISTINCT ON (exp.id) %s, exp.id, hsite.id FROM riski.exposure_data AS exp JOIN hzrdi.hazard_site AS hsite ON ST_DWithin(exp.site, hsite.location, %s) WHERE hsite.hazard_calculation_id = %s AND exposure_model_id = %s AND taxonomy=%s AND ST_COVERS(ST_GeographyFromText(%s), exp.site) ORDER BY exp.id, ST_Distance(exp.site, hsite.location, false) ) INSERT INTO riskr.asset_site (job_id, asset_id, site_id) SELECT * FROM assocs""", (self.calc.job.id, max_dist, self.oqparam.id, self.exposure_model.id, taxonomy, self.calc.oqparam.region_constraint)) self.num_assets = 0 self._rupture_ids = {} self.epsilons_shape = {}
def __init__(self, taxonomy, calc): self.exposure_model = calc.exposure_model self.hazard_outputs = calc.get_hazard_outputs() self.taxonomy = taxonomy self.calc = calc self.oqparam = models.OqJob.objects.get( pk=calc.oqparam.hazard_calculation_id) self.calculation_mode = self.calc.oqparam.calculation_mode self.number_of_ground_motion_fields = self.oqparam.get_param( 'number_of_ground_motion_fields', 0) max_dist = calc.best_maximum_distance * 1000 # km to meters self.cursor = models.getcursor('job_init') hazard_exposure = models.extract_from([self.oqparam], 'exposuremodel') if self.exposure_model and hazard_exposure and \ self.exposure_model.id == hazard_exposure.id: # no need of geospatial queries, just join on the location self.assoc_query = self.cursor.mogrify("""\ WITH assocs AS ( SELECT %s, exp.id, hsite.id FROM riski.exposure_data AS exp JOIN (SELECT id, lon, lat FROM hzrdi.hazard_site WHERE hazard_calculation_id = %s AND ST_Covers(ST_GeometryFromText(%s), ST_MakePoint(lon, lat))) AS hsite ON ST_X(exp.site::GEOMETRY) = hsite.lon AND ST_Y(exp.site::GEOMETRY) = hsite.lat WHERE exposure_model_id = %s AND taxonomy=%s ) INSERT INTO riskr.asset_site (job_id, asset_id, site_id) SELECT * FROM assocs""", (self.calc.job.id, self.oqparam.id, self.calc.oqparam.region_constraint, self.exposure_model.id, taxonomy)) else: # associate each asset to the closest hazard site self.assoc_query = self.cursor.mogrify("""\ WITH assocs AS ( SELECT DISTINCT ON (exp.id) %s, exp.id, hsite.id FROM riski.exposure_data AS exp JOIN (SELECT id, lon, lat FROM hzrdi.hazard_site WHERE hazard_calculation_id = %s AND ST_Covers(ST_GeometryFromText(%s), ST_MakePoint(lon, lat))) AS hsite ON ST_DWithin(exp.site, ST_MakePoint(hsite.lon, hsite.lat)::geography, %s) WHERE exposure_model_id = %s AND taxonomy=%s ORDER BY exp.id, ST_Distance( exp.site, ST_MakePoint(hsite.lon, hsite.lat)::geography, false) ) INSERT INTO riskr.asset_site (job_id, asset_id, site_id) SELECT * FROM assocs""", (self.calc.job.id, self.oqparam.id, self.calc.oqparam.region_constraint, max_dist, self.exposure_model.id, taxonomy)) self.num_assets = 0 self._rupture_ids = {} self.epsilons_shape = {}
def __init__(self, taxonomy, rc): self.hazard_outputs = rc.hazard_outputs() self.taxonomy = taxonomy self.rc = rc self.hc = rc.hazard_calculation self.calculation_mode = self.rc.oqjob.get_param('calculation_mode') self.number_of_ground_motion_fields = self.hc.get_param( 'number_of_ground_motion_fields', 0) max_dist = rc.best_maximum_distance * 1000 # km to meters self.cursor = models.getcursor('job_init') hazard_exposure = models.extract_from([self.hc], 'exposuremodel') if self.rc.exposure_model is hazard_exposure: # no need of geospatial queries, just join on the location self.assoc_query = self.cursor.mogrify("""\ WITH assocs AS ( SELECT %s, exp.id, hsite.id FROM riski.exposure_data AS exp JOIN hzrdi.hazard_site AS hsite ON exp.site::text = hsite.location::text WHERE hsite.hazard_calculation_id = %s AND exposure_model_id = %s AND taxonomy=%s AND ST_COVERS(ST_GeographyFromText(%s), exp.site) ) INSERT INTO riskr.asset_site (job_id, asset_id, site_id) SELECT * FROM assocs""", (rc.oqjob.id, self.hc.id, rc.exposure_model.id, taxonomy, rc.region_constraint)) else: # associate each asset to the closest hazard site self.assoc_query = self.cursor.mogrify("""\ WITH assocs AS ( SELECT DISTINCT ON (exp.id) %s, exp.id, hsite.id FROM riski.exposure_data AS exp JOIN hzrdi.hazard_site AS hsite ON ST_DWithin(exp.site, hsite.location, %s) WHERE hsite.hazard_calculation_id = %s AND exposure_model_id = %s AND taxonomy=%s AND ST_COVERS(ST_GeographyFromText(%s), exp.site) ORDER BY exp.id, ST_Distance(exp.site, hsite.location, false) ) INSERT INTO riskr.asset_site (job_id, asset_id, site_id) SELECT * FROM assocs""", (rc.oqjob.id, max_dist, self.hc.id, rc.exposure_model.id, taxonomy, rc.region_constraint)) self.num_assets = 0 self._rupture_ids = {} self.epsilons_shape = {}