def test_the_earthquake_fatality_estimation_allen(self): """Fatality computation computed correctly with GeoServer Data """ # Simulate bounding box from application viewport_bbox_string = '104.3,-8.2,110.04,-5.17' # Upload exposure data for this test name = 'Population_2010' exposure_filename = '%s/%s.asc' % (TESTDATA, name) exposure_layer = save_to_geonode(exposure_filename, user=self.user, overwrite=True) workspace = exposure_layer.workspace layer_name = exposure_layer.name msg = 'Expected layer name to be "%s". Got %s' % (name, layer_name) assert layer_name == name.lower(), msg exposure_name = '%s:%s' % (workspace, layer_name) # Check metadata assert_bounding_box_matches(exposure_layer, exposure_filename) exp_bbox_string = get_bounding_box_string(exposure_filename) check_layer(exposure_layer, full=True) # Now we know that exposure layer is good, lets upload some # hazard layers and do the calculations filename = 'lembang_mmi_hazmap.asc' # Save hazard_filename = '%s/%s' % (TESTDATA, filename) hazard_layer = save_to_geonode(hazard_filename, user=self.user, overwrite=True) hazard_name = '%s:%s' % (hazard_layer.workspace, hazard_layer.name) # Check metadata assert_bounding_box_matches(hazard_layer, hazard_filename) haz_bbox_string = get_bounding_box_string(hazard_filename) check_layer(hazard_layer, full=True) calculate_url = reverse('safe-calculate') # Run calculation c = Client() rv = c.post(calculate_url, data=dict( hazard_server=INTERNAL_SERVER_URL, hazard=hazard_name, exposure_server=INTERNAL_SERVER_URL, exposure=exposure_name, #bbox=viewport_bbox_string, bbox=exp_bbox_string, # This one reproduced the # crash for lembang impact_function='I T B Fatality Function', keywords='test,shakemap,usgs')) self.assertEqual(rv.status_code, 200) self.assertEqual(rv['Content-Type'], 'application/json') data = json.loads(rv.content) if 'errors' in data: errors = data['errors'] if errors is not None: msg = ('The server returned the error message: %s' % str(errors)) raise Exception(msg) assert 'success' in data assert 'hazard_layer' in data assert 'exposure_layer' in data assert 'run_duration' in data assert 'run_date' in data assert 'layer' in data assert data['success'] # Download result and check layer = data['layer'] layer_id = layer['id'] result_layer = download(INTERNAL_SERVER_URL, layer_id, get_bounding_box_string(hazard_filename)) assert os.path.exists(result_layer.filename)
def test_jakarta_flood_study(self): """HKV Jakarta flood study calculated correctly using the API """ # FIXME (Ole): Redo with population as shapefile later # Expected values from HKV expected_value = 1537920 # Name files for hazard level, exposure and expected fatalities population = 'Population_Jakarta_geographic' plugin_name = 'Flood Evacuation Function' # Upload exposure data for this test exposure_filename = '%s/%s.asc' % (TESTDATA, population) exposure_layer = save_to_geonode(exposure_filename, user=self.user, overwrite=True) workspace = exposure_layer.workspace exposure_name = '%s:%s' % (workspace, exposure_layer.name) # Check metadata assert_bounding_box_matches(exposure_layer, exposure_filename) exp_bbox_string = get_bounding_box_string(exposure_filename) check_layer(exposure_layer, full=True) # Now we know that exposure layer is good, lets upload some # hazard layers and do the calculations filename = 'jakarta_flood_design.tif' hazard_filename = os.path.join(UNITDATA, 'hazard', filename) exposure_filename = os.path.join(TESTDATA, population) # Save hazard_layer = save_to_geonode(hazard_filename, user=self.user, overwrite=True) hazard_name = '%s:%s' % (hazard_layer.workspace, hazard_layer.name) # Check metadata assert_bounding_box_matches(hazard_layer, hazard_filename) haz_bbox_string = get_bounding_box_string(hazard_filename) check_layer(hazard_layer, full=True) calculate_url = reverse('safe-calculate') # Run calculation c = Client() rv = c.post(calculate_url, data=dict( hazard_server=INTERNAL_SERVER_URL, hazard=hazard_name, exposure_server=INTERNAL_SERVER_URL, exposure=exposure_name, bbox=exp_bbox_string, impact_function=plugin_name, keywords='test,flood,HKV')) self.assertEqual(rv.status_code, 200) self.assertEqual(rv['Content-Type'], 'application/json') data = json.loads(rv.content) if 'errors' in data: errors = data['errors'] if errors is not None: raise Exception(errors) assert 'hazard_layer' in data assert 'exposure_layer' in data assert 'run_duration' in data assert 'run_date' in data assert 'layer' in data
def test_earthquake_exposure_plugin(self): """Population exposure to individual MMI levels can be computed """ # Upload exposure data for this test # FIXME (Ole): While this dataset is ok for testing, # note that is has been resampled without scaling # so numbers are about 25 times too large. # Consider replacing test populations dataset for good measures, # just in case any one accidentally started using this dataset # for real. name = 'Population_2010' exposure_filename = '%s/%s.asc' % (TESTDATA, name) exposure_layer = save_to_geonode(exposure_filename, user=self.user, overwrite=True) exposure_name = '%s:%s' % (exposure_layer.workspace, exposure_layer.name) # Check metadata assert_bounding_box_matches(exposure_layer, exposure_filename) exp_bbox_string = get_bounding_box_string(exposure_filename) check_layer(exposure_layer, full=True) # Upload hazard data filename = 'lembang_mmi_hazmap.asc' hazard_filename = '%s/%s' % (TESTDATA, filename) hazard_layer = save_to_geonode(hazard_filename, user=self.user, overwrite=True) hazard_name = '%s:%s' % (hazard_layer.workspace, hazard_layer.name) # Check metadata assert_bounding_box_matches(hazard_layer, hazard_filename) haz_bbox_string = get_bounding_box_string(hazard_filename) check_layer(hazard_layer, full=True) calculate_url = reverse('safe-calculate') # Run calculation c = Client() rv = c.post(calculate_url, data=dict( hazard_server=INTERNAL_SERVER_URL, hazard=hazard_name, exposure_server=INTERNAL_SERVER_URL, exposure=exposure_name, bbox=haz_bbox_string, impact_function='Earthquake Building Damage Function', keywords='test,population,exposure,usgs')) self.assertEqual(rv.status_code, 200) self.assertEqual(rv['Content-Type'], 'application/json') data = json.loads(rv.content) if 'errors' in data: errors = data['errors'] if errors is not None: msg = ('The server returned the error message: %s' % str(errors)) raise Exception(msg) assert 'success' in data assert 'hazard_layer' in data assert 'exposure_layer' in data assert 'run_duration' in data assert 'run_date' in data assert 'layer' in data assert data['success'] # Download result and check layer_name = data['layer'].split('/')[-1] result_layer = download(INTERNAL_SERVER_URL, layer_name, get_bounding_box_string(hazard_filename)) assert os.path.exists(result_layer.filename) # Check calculated values keywords = result_layer.get_keywords() assert 'mmi-classes' in keywords assert 'affected-population' in keywords mmi_classes = [int(x) for x in keywords['mmi-classes'].split('_')] count = [float(x) for x in keywords['affected-population'].split('_')] # Brute force count for each population level population = download(INTERNAL_SERVER_URL, exposure_name, get_bounding_box_string(hazard_filename)) intensity = download(INTERNAL_SERVER_URL, hazard_name, get_bounding_box_string(hazard_filename)) # Extract data H = intensity.get_data(nan=0) P = population.get_data(nan=0) brutecount = {} for mmi in mmi_classes: brutecount[mmi] = 0 for i in range(P.shape[0]): for j in range(P.shape[1]): mmi = H[i, j] if not numpy.isnan(mmi): mmi_class = int(round(mmi)) pop = P[i, j] if not numpy.isnan(pop): brutecount[mmi_class] += pop for i, mmi in enumerate(mmi_classes): assert numpy.allclose(count[i], brutecount[mmi], rtol=1.0e-6)
def test_metadata_twice(self): """Layer metadata can be correctly uploaded multiple times """ # This test reproduces ticket #99 by creating new data, # uploading twice and verifying metadata # Base test data filenames = ['jakarta_flood_design.tif', ] for org_filename in filenames: org_basename, ext = os.path.splitext(os.path.join(UNITDATA, 'hazard', org_filename)) # Copy data to temporary unique name basename = unique_filename(dir='/tmp') cmd = '/bin/cp -f %s.keywords %s.keywords' % (org_basename, basename) os.system(cmd) # Not needed since we are dealing with a raster #cmd = '/bin/cp -f %s.prj %s.prj' % (org_basename, basename) #os.system(cmd) if ext == '.tif': layertype = 'raster' filename = '%s.tif' % basename cmd = '/bin/cp %s.tif %s' % (org_basename, filename) os.system(cmd) elif ext == '.shp': layertype = 'vector' filename = '%s.shp' % basename for e in ['shp', 'shx', 'sbx', 'sbn', 'dbf']: cmd = '/bin/cp %s.%s %s.%s' % (org_basename, e, basename, e) os.system(cmd) else: msg = ('Unknown layer extension in %s. ' 'Expected .shp or .asc' % filename) raise Exception(msg) # Repeat multiple times for i in range(3): # Upload layer = save_to_geonode(filename, user=self.user, overwrite=True) # Get metadata layer_name = '%s:%s' % (layer.workspace, layer.name) metadata = get_metadata(INTERNAL_SERVER_URL, layer_name) # Verify assert 'id' in metadata assert 'title' in metadata assert 'layertype' in metadata assert 'keywords' in metadata assert 'bounding_box' in metadata assert len(metadata['bounding_box']) == 4 # Check integrity between Django layer and file assert_bounding_box_matches(layer, filename) # Check integrity between file and OWS metadata ref_bbox = get_bounding_box(filename) msg = ('Bounding box from OWS did not match bounding box ' 'from file. They are\n' 'From file %s: %s\n' 'From OWS: %s' % (filename, ref_bbox, metadata['bounding_box'])) assert numpy.allclose(metadata['bounding_box'], ref_bbox), msg assert layer.title == metadata['title'] assert layer_name == metadata['id'] assert layertype == metadata['layertype'] # Check keywords if layertype == 'raster': category = 'hazard' subcategory = 'flood' else: msg = 'Unknown layer type %s' % layertype raise Exception(msg) keywords = metadata['keywords'] msg = 'Did not find key "category" in keywords: %s' % keywords assert 'category' in keywords, msg msg = ('Did not find key "subcategory" in keywords: %s' % keywords) assert 'subcategory' in keywords, msg msg = ('Category keyword %s did not match expected %s' % (keywords['category'], category)) assert category == keywords['category'], msg msg = ('Subcategory keyword %s did not match expected %s' % (keywords['subcategory'], category)) assert subcategory == keywords['subcategory'], msg
def test_metadata(self): """Metadata is retrieved correctly for both raster and vector data """ # Upload test data filenames = [os.path.join('hazard', 'jakarta_flood_design.tif'), os.path.join('exposure', 'buildings_osm_4326.shp')] layers = [] paths = [] for filename in filenames: path = os.path.join(UNITDATA, filename) layer = save_to_geonode(path, user=self.user, overwrite=True) # Record layer and file layers.append(layer) paths.append(path) # Check integrity for i, layer in enumerate(layers): if filenames[i].endswith('.shp'): layertype = 'vector' elif filenames[i].endswith('.tif'): layertype = 'raster' else: msg = ('Unknown layer extension in %s. ' 'Expected .shp or .tif' % filenames[i]) raise Exception(msg) layer_name = '%s:%s' % (layer.workspace, layer.name) metadata = get_metadata(INTERNAL_SERVER_URL, layer_name) assert 'id' in metadata assert 'title' in metadata assert 'layertype' in metadata assert 'keywords' in metadata assert 'bounding_box' in metadata assert len(metadata['bounding_box']) == 4 # Check integrity between Django layer and file assert_bounding_box_matches(layer, paths[i]) # Check integrity between file and OWS metadata ref_bbox = get_bounding_box(paths[i]) msg = ('Bounding box from OWS did not match bounding box ' 'from file. They are\n' 'From file %s: %s\n' 'From OWS: %s' % (paths[i], ref_bbox, metadata['bounding_box'])) assert numpy.allclose(metadata['bounding_box'], ref_bbox), msg assert layer.title == metadata['title'] assert layer_name == metadata['id'] assert layertype == metadata['layertype'] # Check keywords if layertype == 'raster': category = 'hazard' subcategory = 'flood' elif layertype == 'vector': category = 'exposure' subcategory = 'structure' else: msg = 'Unknown layer type %s' % layertype raise Exception(msg) keywords = metadata['keywords'] msg = 'Did not find key "category" in keywords: %s' % keywords assert 'category' in keywords, msg msg = 'Did not find key "subcategory" in keywords: %s' % keywords assert 'subcategory' in keywords, msg msg = ('Category keyword %s did not match expected %s' % (keywords['category'], category)) assert category == keywords['category'], msg msg = ('Subcategory keyword %s did not match expected %s' % (keywords['subcategory'], category)) assert subcategory == keywords['subcategory'], msg
def test_metadata(self): """Metadata is retrieved correctly for both raster and vector data """ # Upload test data filenames = [ os.path.join('hazard', 'jakarta_flood_design.tif'), os.path.join('exposure', 'buildings_osm_4326.shp') ] layers = [] paths = [] for filename in filenames: path = os.path.join(UNITDATA, filename) layer = save_to_geonode(path, user=self.user, overwrite=True) # Record layer and file layers.append(layer) paths.append(path) # Check integrity for i, layer in enumerate(layers): if filenames[i].endswith('.shp'): layertype = 'vector' elif filenames[i].endswith('.tif'): layertype = 'raster' else: msg = ('Unknown layer extension in %s. ' 'Expected .shp or .tif' % filenames[i]) raise Exception(msg) layer_name = '%s:%s' % (layer.workspace, layer.name) metadata = get_metadata(INTERNAL_SERVER_URL, layer_name) assert 'id' in metadata assert 'title' in metadata assert 'layertype' in metadata assert 'keywords' in metadata assert 'bounding_box' in metadata assert len(metadata['bounding_box']) == 4 # Check integrity between Django layer and file assert_bounding_box_matches(layer, paths[i]) # Check integrity between file and OWS metadata ref_bbox = get_bounding_box(paths[i]) msg = ('Bounding box from OWS did not match bounding box ' 'from file. They are\n' 'From file %s: %s\n' 'From OWS: %s' % (paths[i], ref_bbox, metadata['bounding_box'])) assert numpy.allclose(metadata['bounding_box'], ref_bbox), msg assert layer.title == metadata['title'] assert layer_name == metadata['id'] assert layertype == metadata['layertype'] # Check keywords if layertype == 'raster': category = 'hazard' subcategory = 'flood' elif layertype == 'vector': category = 'exposure' subcategory = 'structure' else: msg = 'Unknown layer type %s' % layertype raise Exception(msg) keywords = metadata['keywords'] msg = 'Did not find key "category" in keywords: %s' % keywords assert 'category' in keywords, msg msg = 'Did not find key "subcategory" in keywords: %s' % keywords assert 'subcategory' in keywords, msg msg = ('Category keyword %s did not match expected %s' % (keywords['category'], category)) assert category == keywords['category'], msg msg = ('Subcategory keyword %s did not match expected %s' % (keywords['subcategory'], category)) assert subcategory == keywords['subcategory'], msg
def test_metadata_twice(self): """Layer metadata can be correctly uploaded multiple times """ # This test reproduces ticket #99 by creating new data, # uploading twice and verifying metadata # Base test data filenames = [ 'jakarta_flood_design.tif', ] for org_filename in filenames: org_basename, ext = os.path.splitext( os.path.join(UNITDATA, 'hazard', org_filename)) # Copy data to temporary unique name basename = unique_filename(dir='/tmp') cmd = '/bin/cp -f %s.keywords %s.keywords' % (org_basename, basename) os.system(cmd) # Not needed since we are dealing with a raster #cmd = '/bin/cp -f %s.prj %s.prj' % (org_basename, basename) #os.system(cmd) if ext == '.tif': layertype = 'raster' filename = '%s.tif' % basename cmd = '/bin/cp %s.tif %s' % (org_basename, filename) os.system(cmd) elif ext == '.shp': layertype = 'vector' filename = '%s.shp' % basename for e in ['shp', 'shx', 'sbx', 'sbn', 'dbf']: cmd = '/bin/cp %s.%s %s.%s' % (org_basename, e, basename, e) os.system(cmd) else: msg = ('Unknown layer extension in %s. ' 'Expected .shp or .asc' % filename) raise Exception(msg) # Repeat multiple times for i in range(3): # Upload layer = save_to_geonode(filename, user=self.user, overwrite=True) # Get metadata layer_name = '%s:%s' % (layer.workspace, layer.name) metadata = get_metadata(INTERNAL_SERVER_URL, layer_name) # Verify assert 'id' in metadata assert 'title' in metadata assert 'layertype' in metadata assert 'keywords' in metadata assert 'bounding_box' in metadata assert len(metadata['bounding_box']) == 4 # Check integrity between Django layer and file assert_bounding_box_matches(layer, filename) # Check integrity between file and OWS metadata ref_bbox = get_bounding_box(filename) msg = ('Bounding box from OWS did not match bounding box ' 'from file. They are\n' 'From file %s: %s\n' 'From OWS: %s' % (filename, ref_bbox, metadata['bounding_box'])) assert numpy.allclose(metadata['bounding_box'], ref_bbox), msg assert layer.title == metadata['title'] assert layer_name == metadata['id'] assert layertype == metadata['layertype'] # Check keywords if layertype == 'raster': category = 'hazard' subcategory = 'flood' else: msg = 'Unknown layer type %s' % layertype raise Exception(msg) keywords = metadata['keywords'] msg = 'Did not find key "category" in keywords: %s' % keywords assert 'category' in keywords, msg msg = ('Did not find key "subcategory" in keywords: %s' % keywords) assert 'subcategory' in keywords, msg msg = ('Category keyword %s did not match expected %s' % (keywords['category'], category)) assert category == keywords['category'], msg msg = ('Subcategory keyword %s did not match expected %s' % (keywords['subcategory'], category)) assert subcategory == keywords['subcategory'], msg
def test_the_earthquake_fatality_estimation_allen(self): """Fatality computation computed correctly with GeoServer Data """ # Simulate bounding box from application viewport_bbox_string = '104.3,-8.2,110.04,-5.17' # Upload exposure data for this test name = 'Population_2010' exposure_filename = '%s/%s.asc' % (TESTDATA, name) exposure_layer = save_to_geonode(exposure_filename, user=self.user, overwrite=True) workspace = exposure_layer.workspace layer_name = exposure_layer.name msg = 'Expected layer name to be "%s". Got %s' % (name, layer_name) assert layer_name == name.lower(), msg exposure_name = '%s:%s' % (workspace, layer_name) # Check metadata assert_bounding_box_matches(exposure_layer, exposure_filename) exp_bbox_string = get_bounding_box_string(exposure_filename) check_layer(exposure_layer, full=True) # Now we know that exposure layer is good, lets upload some # hazard layers and do the calculations filename = 'lembang_mmi_hazmap.asc' # Save hazard_filename = '%s/%s' % (TESTDATA, filename) hazard_layer = save_to_geonode(hazard_filename, user=self.user, overwrite=True) hazard_name = '%s:%s' % (hazard_layer.workspace, hazard_layer.name) # Check metadata assert_bounding_box_matches(hazard_layer, hazard_filename) haz_bbox_string = get_bounding_box_string(hazard_filename) check_layer(hazard_layer, full=True) calculate_url = reverse('safe-calculate') # Run calculation c = Client() rv = c.post( calculate_url, data=dict( hazard_server=INTERNAL_SERVER_URL, hazard=hazard_name, exposure_server=INTERNAL_SERVER_URL, exposure=exposure_name, #bbox=viewport_bbox_string, bbox=exp_bbox_string, # This one reproduced the # crash for lembang impact_function='I T B Fatality Function', keywords='test,shakemap,usgs')) self.assertEqual(rv.status_code, 200) self.assertEqual(rv['Content-Type'], 'application/json') data = json.loads(rv.content) if 'errors' in data: errors = data['errors'] if errors is not None: msg = ('The server returned the error message: %s' % str(errors)) raise Exception(msg) assert 'success' in data assert 'hazard_layer' in data assert 'exposure_layer' in data assert 'run_duration' in data assert 'run_date' in data assert 'layer' in data assert data['success'] # Download result and check layer = data['layer'] layer_id = layer['id'] result_layer = download(INTERNAL_SERVER_URL, layer_id, get_bounding_box_string(hazard_filename)) assert os.path.exists(result_layer.filename)
def test_jakarta_flood_study(self): """HKV Jakarta flood study calculated correctly using the API """ # FIXME (Ole): Redo with population as shapefile later # Expected values from HKV expected_value = 1537920 # Name files for hazard level, exposure and expected fatalities population = 'Population_Jakarta_geographic' plugin_name = 'Flood Evacuation Function' # Upload exposure data for this test exposure_filename = '%s/%s.asc' % (TESTDATA, population) exposure_layer = save_to_geonode(exposure_filename, user=self.user, overwrite=True) workspace = exposure_layer.workspace exposure_name = '%s:%s' % (workspace, exposure_layer.name) # Check metadata assert_bounding_box_matches(exposure_layer, exposure_filename) exp_bbox_string = get_bounding_box_string(exposure_filename) check_layer(exposure_layer, full=True) # Now we know that exposure layer is good, lets upload some # hazard layers and do the calculations filename = 'jakarta_flood_design.tif' hazard_filename = os.path.join(UNITDATA, 'hazard', filename) exposure_filename = os.path.join(TESTDATA, population) # Save hazard_layer = save_to_geonode(hazard_filename, user=self.user, overwrite=True) hazard_name = '%s:%s' % (hazard_layer.workspace, hazard_layer.name) # Check metadata assert_bounding_box_matches(hazard_layer, hazard_filename) haz_bbox_string = get_bounding_box_string(hazard_filename) check_layer(hazard_layer, full=True) calculate_url = reverse('safe-calculate') # Run calculation c = Client() rv = c.post(calculate_url, data=dict(hazard_server=INTERNAL_SERVER_URL, hazard=hazard_name, exposure_server=INTERNAL_SERVER_URL, exposure=exposure_name, bbox=exp_bbox_string, impact_function=plugin_name, keywords='test,flood,HKV')) self.assertEqual(rv.status_code, 200) self.assertEqual(rv['Content-Type'], 'application/json') data = json.loads(rv.content) if 'errors' in data: errors = data['errors'] if errors is not None: raise Exception(errors) assert 'hazard_layer' in data assert 'exposure_layer' in data assert 'run_duration' in data assert 'run_date' in data assert 'layer' in data