# Then one where boxes don't overlap view_port = [105.3, -4.3, 110.29, -2.5] try: getOptimalExtent(hazard_bbox, exposure_bbox, view_port) except Exception, e: myMessage = 'Did not find expected error message in %s' % str(e) assert 'did not overlap' in str(e), myMessage else: myMessage = ('Non ovelapping bounding boxes should have raised ' 'an exception') raise Exception(myMessage) # Try with wrong input data try: getOptimalExtent(haz_metadata, exp_metadata, view_port) except Exception, e: myMessage = 'Did not find expected error message in %s' % str(e) assert 'Invalid' in str(e), myMessage else: myMessage = ('Wrong input data should have raised an exception') raise Exception(myMessage) try: getOptimalExtent(None, None, view_port) except Exception, e: myMessage = 'Did not find expected error message in %s' % str(e) assert 'Invalid' in str(e), myMessage else: myMessage = ('Wrong input data should have raised an exception') raise Exception(myMessage)
def test_getOptimalExtent(self): """Optimal extent is calculated correctly """ exposure_path = os.path.join(TESTDATA, 'Population_2010.asc') hazard_path = os.path.join(HAZDATA, 'Lembang_Earthquake_Scenario.asc') # Expected data haz_metadata = {'bounding_box': (105.3000035, -8.3749994999999995, 110.2914705, -5.5667784999999999), 'resolution': (0.0083330000000000001, 0.0083330000000000001)} exp_metadata = {'bounding_box': (94.972335000000001, -11.009721000000001, 141.0140016666665, 6.0736123333332639), 'resolution': (0.0083333333333333003, 0.0083333333333333003)} # Verify relevant metada is ok H = readSafeLayer(hazard_path) E = readSafeLayer(exposure_path) hazard_bbox = H.get_bounding_box() assert numpy.allclose(hazard_bbox, haz_metadata['bounding_box'], rtol=1.0e-12, atol=1.0e-12) exposure_bbox = E.get_bounding_box() assert numpy.allclose(exposure_bbox, exp_metadata['bounding_box'], rtol=1.0e-12, atol=1.0e-12) hazard_res = H.get_resolution() assert numpy.allclose(hazard_res, haz_metadata['resolution'], rtol=1.0e-12, atol=1.0e-12) exposure_res = E.get_resolution() assert numpy.allclose(exposure_res, exp_metadata['resolution'], rtol=1.0e-12, atol=1.0e-12) # First, do some examples that produce valid results ref_res = [105.3000035, -8.3749995, 110.2914705, -5.5667785] view_port = [94.972335, -11.009721, 141.014002, 6.073612] bbox = getOptimalExtent(hazard_bbox, exposure_bbox, view_port) assert numpy.allclose(bbox, ref_res, rtol=1.0e-12, atol=1.0e-12) bbox = getOptimalExtent(hazard_bbox, exposure_bbox, view_port) assert numpy.allclose(bbox, ref_res, rtol=1.0e-12, atol=1.0e-12) view_port = [105.3000035, -8.3749994999999995, 110.2914705, -5.5667784999999999] bbox = getOptimalExtent(hazard_bbox, exposure_bbox, view_port) assert numpy.allclose(bbox, ref_res, rtol=1.0e-12, atol=1.0e-12) # Then one where boxes don't overlap view_port = [105.3, -4.3, 110.29, -2.5] try: getOptimalExtent(hazard_bbox, exposure_bbox, view_port) except Exception, e: myMessage = 'Did not find expected error message in %s' % str(e) assert 'did not overlap' in str(e), myMessage
def test_clipBoth(self): """Raster and Vector layers can be clipped """ # Create a vector layer myName = 'padang' myVectorLayer = QgsVectorLayer(VECTOR_PATH, myName, 'ogr') msg = 'Did not find layer "%s" in path "%s"' % (myName, VECTOR_PATH) # FIXME (Ole): This does not work when file doesn't exist (Issue #170) assert myVectorLayer is not None, msg # Create a raster layer myName = 'shake' myRasterLayer = QgsRasterLayer(RASTERPATH, myName) myMessage = 'Did not find layer "%s" in path "%s"' % (myName, RASTERPATH) # FIXME (Ole): This does not work when file doesn't exist (Issue #170) assert myRasterLayer is not None, myMessage # Create a bounding box myViewportGeoExtent = [99.53, -1.22, 101.20, -0.36] # Get the Hazard extents as an array in EPSG:4326 myHazardGeoExtent = [myRasterLayer.extent().xMinimum(), myRasterLayer.extent().yMinimum(), myRasterLayer.extent().xMaximum(), myRasterLayer.extent().yMaximum()] # Get the Exposure extents as an array in EPSG:4326 myExposureGeoExtent = [myVectorLayer.extent().xMinimum(), myVectorLayer.extent().yMinimum(), myVectorLayer.extent().xMaximum(), myVectorLayer.extent().yMaximum()] # Now work out the optimal extent between the two layers and # the current view extent. The optimal extent is the intersection # between the two layers and the viewport. # Extent is returned as an array [xmin,ymin,xmax,ymax] myGeoExtent = getOptimalExtent(myHazardGeoExtent, myExposureGeoExtent, myViewportGeoExtent) # Clip the vector to the bbox myResult = clipLayer(myVectorLayer, myGeoExtent) # Check the output is valid assert os.path.exists(myResult) readSafeLayer(myResult) # Clip the raster to the bbox myResult = clipLayer(myRasterLayer, myGeoExtent) # Check the output is valid assert os.path.exists(myResult) readSafeLayer(myResult) # ------------------------------- # Check the extra keywords option # ------------------------------- # Clip the vector to the bbox myResult = clipLayer(myVectorLayer, myGeoExtent, theExtraKeywords={'kermit': 'piggy'}) # Check the output is valid assert os.path.exists(myResult) L = readSafeLayer(myResult) kwds = L.get_keywords() msg = 'Extra keyword was not found in %s: %s' % (myResult, kwds) assert kwds['kermit'] == 'piggy' # Clip the raster to the bbox myResult = clipLayer(myRasterLayer, myGeoExtent, theExtraKeywords={'zoot': 'animal'}) # Check the output is valid assert os.path.exists(myResult) L = readSafeLayer(myResult) kwds = L.get_keywords() msg = 'Extra keyword was not found in %s: %s' % (myResult, kwds) assert kwds['zoot'] == 'animal', msg