def test_create_oqhazardlib_source(self): # Define a complete source area_geom = polygon.Polygon([ point.Point(10., 10.), point.Point(12., 10.), point.Point(12., 8.), point.Point(10., 8.) ]) mfd1 = TruncatedGRMFD(5.0, 8.0, 0.1, 3.0, 1.0) self.area_source = mtkAreaSource('001', 'A Point Source', trt='Active Shallow Crust', geometry=area_geom, upper_depth=0., lower_depth=20., mag_scale_rel=None, rupt_aspect_ratio=1.0, mfd=mfd1, nodal_plane_dist=None, hypo_depth_dist=None) test_source = self.area_source.create_oqhazardlib_source( TOM, 1.0, 10.0, True) self.assertIsInstance(test_source, AreaSource) self.assertIsInstance(test_source.mfd, TruncatedGRMFD) self.assertAlmostEqual(test_source.mfd.b_val, 1.0) self.assertIsInstance(test_source.nodal_plane_distribution, PMF) self.assertIsInstance(test_source.hypocenter_distribution, PMF) self.assertIsInstance(test_source.magnitude_scaling_relationship, WC1994)
def test_extremely_convex(self): # Exercise a case where the area polygon is so convex that holes will # appear after dilating a certain distance. # This test data is based on sample data which produced this failure: # https://bugs.launchpad.net/openquake/+bug/1091130 poly_coords = [ [16.956879, 41.628004], [16.966878, 41.932705], [17.606256, 41.897445], [19.181572, 41.046135], [19.06223673, 40.58765479], [18.61629494, 39.63362123], [18.919869, 39.501369], [18.968954, 39.479085], [19.22257, 40.290941], [20.203748, 38.900256], [20.2, 38.6], [19.67226639, 38.00730337], [19.67226639, 38.00730337], [18.812336, 38.816193], [18.540406, 39.043834], [16.956879, 41.628004], ] poly = polygon.Polygon([geo.Point(*x) for x in poly_coords]) dilation = 15.554238346616508 poly.dilate(dilation)
def test_wkt(self): expected_wkt = ( 'POLYGON((-1.11111 2.22222, -3.33333 4.44444, ' '5.55555 -6.66666, -1.11111 2.22222))') poly = polygon.Polygon( [geo.Point(-1.11111, 2.22222), geo.Point(-3.33333, 4.44444), geo.Point(5.55555, -6.66666)]) self.assertEqual(expected_wkt, poly.wkt)
def test_geometry_inputs(self): # Tests the geometry definition simple_polygon = polygon.Polygon([point.Point(2.0, 3.0), point.Point(3.0, 3.0), point.Point(3.0, 2.0), point.Point(2.0, 2.0)]) simple_polygon_array = np.array([[2.0, 3.0], [3.0, 3.0], [3.0, 2.0], [2.0, 2.0]]) # Using nhlib.geo.polygon.Polygon class as input self.area_source.create_geometry(simple_polygon, 0.0, 30.0) # Check that geometry is an instance of nhlib.geo.polygon.Polygon self.assertTrue(isinstance(self.area_source.geometry, polygon.Polygon)) np.testing.assert_array_almost_equal(self.area_source.geometry.lons, np.array([2., 3., 3., 2.])) np.testing.assert_array_almost_equal(self.area_source.geometry.lats, np.array([3., 3., 2., 2.])) self.assertAlmostEqual(0.0, self.area_source.upper_depth) self.assertAlmostEqual(30.0, self.area_source.lower_depth) self.area_source = mtkAreaSource('101', 'A Source') # Using numpy array as input self.area_source.create_geometry(simple_polygon_array, 0.0, 30.0) self.assertTrue(isinstance(self.area_source.geometry, polygon.Polygon)) # Check that geometry is an instance of nhlib.geo.polygon.Polygon np.testing.assert_array_almost_equal(self.area_source.geometry.lons, np.array([2., 3., 3., 2.])) np.testing.assert_array_almost_equal(self.area_source.geometry.lats, np.array([3., 3., 2., 2.])) self.assertAlmostEqual(0.0, self.area_source.upper_depth) self.assertAlmostEqual(30.0, self.area_source.lower_depth) # For any other input type - check ValueError is raised self.area_source = mtkAreaSource('101', 'A Source') with self.assertRaises(ValueError) as ver: self.area_source.create_geometry('a bad input', 0.0, 30.0) self.assertEqual(str(ver.exception), 'Unrecognised or unsupported geometry definition') # For numpy array with only two rows self.area_source = mtkAreaSource('101', 'A Source') simple_polygon_array = np.array([[2.0, 3.0], [3.0, 3.0]]) with self.assertRaises(ValueError) as ver: self.area_source.create_geometry(simple_polygon_array, 0.0, 30.0) self.assertEqual(str(ver.exception), 'Incorrectly formatted polygon geometry - ' 'needs three or more vertices')
def test_select_events_in_source(self): ''' Basic test of method to select events from catalogue in polygon ''' self.area_source = mtkAreaSource('101', 'A Source') simple_polygon = polygon.Polygon([ point.Point(2.0, 3.0), point.Point(3.0, 3.0), point.Point(3.0, 2.0), point.Point(2.0, 2.0) ]) self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(1.0, 4.5, 0.5) self.catalogue.data['latitude'] = np.arange(1.0, 4.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) # Simple Case - No buffer selector0 = CatalogueSelector(self.catalogue) self.area_source.create_geometry(simple_polygon, 0., 30.) self.area_source.select_catalogue(selector0, 0.) np.testing.assert_array_almost_equal( np.array([2., 2.5, 3.]), self.area_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([2., 2.5, 3.]), self.area_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.area_source.catalogue.data['depth']) # Simple case - dilated by 200 km (selects all) self.area_source.select_catalogue(selector0, 200.) np.testing.assert_array_almost_equal( np.array([1., 1.5, 2., 2.5, 3., 3.5, 4.0]), self.area_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([1., 1.5, 2., 2.5, 3., 3.5, 4.0]), self.area_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.ones(7, dtype=float), self.area_source.catalogue.data['depth']) # Bad case - no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.area_source.select_catalogue(selector0, 0.0) self.assertEqual(ver.exception.message, 'No events found in catalogue!')
def test_create_oqnmrl_area_source(self): ''' Tests the conversion of a point source to an instance of the :class: oqnrmllib.models.AreaSource ''' # Define a complete source area_geom = polygon.Polygon([ point.Point(10., 10.), point.Point(12., 10.), point.Point(12., 8.), point.Point(10., 8.) ]) self.area_source = mtkAreaSource('001', 'A Point Source', trt='Active Shallow Crust', geometry=area_geom, upper_depth=0., lower_depth=20., mag_scale_rel=None, rupt_aspect_ratio=1.0, mfd=models.TGRMFD(a_val=3., b_val=1.0, min_mag=5.0, max_mag=8.0), nodal_plane_dist=None, hypo_depth_dist=None) expected_source = models.AreaSource('001', 'A Point Source', geometry=models.AreaGeometry( area_geom.wkt, 0., 20.), mag_scale_rel='WC1994', rupt_aspect_ratio=1.0, mfd=models.TGRMFD(a_val=3., b_val=1.0, min_mag=5.0, max_mag=8.0), nodal_plane_dist=None, hypo_depth_dist=None) test_source = self.area_source.create_oqnrml_source(use_defaults=True) self.assertTrue(isinstance(test_source, models.AreaSource)) self.assertEqual(test_source.id, expected_source.id) self.assertEqual(test_source.name, expected_source.name) self.assertAlmostEqual(test_source.mfd.b_val, expected_source.mfd.b_val)
def test_clockwise(self): poly = polygon.Polygon([geo.Point(0, 0), geo.Point(0, 1), geo.Point(1, 0.5)]) dilated = poly.dilate(20) elons = [ 0.0804399, 0.0645005, 0.0479561, 0.0309619, 0.0136773, -0.0037356, -0.0211136, -0.0382935, -0.0551142, -0.0714181, -0.0870522, -0.1018698, -0.1157321, -0.1285089, -0.1400805, -0.1503383, -0.1591861, -0.1665409, -0.1723339, -0.1765105, -0.1790318, -0.1798739, -0.1799013, -0.1790601, -0.1765397, -0.1723637, -0.1665713, -0.1592168, -0.1503691, -0.1401112, -0.1285392, -0.1157618, -0.1018986, -0.0870797, -0.0714441, -0.0551384, -0.0383156, -0.0211335, -0.0037531, 0.0136624, 0.0309498, 0.0479468, 0.0644941, 0.0804365, 1.0804671, 1.0955589, 1.1097657, 1.1229560, 1.1350074, 1.1458085, 1.1552592, 1.1632720, 1.1697727, 1.1747011, 1.1780116, 1.1796735, 1.1796714, 1.1780054, 1.1746910, 1.1697588, 1.1632545, 1.1552383, 1.1457846, 1.1349808, 1.1229271, 1.1097349, 1.0955266, 1.0804337 ] elats = [ -0.1608776, -0.1679056, -0.1733589, -0.1771863, -0.1793519, -0.1798355, -0.1786324, -0.1757539, -0.1712271, -0.1650944, -0.1574134, -0.1482560, -0.1377081, -0.1258688, -0.1128490, -0.0987708, -0.0837663, -0.0679761, -0.0515485, -0.0346374, -0.0174015, -0.0000025, 0.9999975, 1.0173956, 1.0346306, 1.0515410, 1.0679680, 1.0837578, 1.0987621, 1.1128403, 1.1258603, 1.1377001, 1.1482485, 1.1574067, 1.1650887, 1.1712225, 1.1757506, 1.1786305, 1.1798351, 1.1793533, 1.1771894, 1.1733639, 1.1679125, 1.1608864, 0.6608661, 0.6523872, 0.6424971, 0.6312873, 0.6188616, 0.6053351, 0.5908330, 0.5754898, 0.5594475, 0.5428546, 0.5258649, 0.5086357, 0.4913265, 0.4740977, 0.4571088, 0.4405171, 0.4244763, 0.4091349, 0.3946351, 0.3811110, 0.3686880, 0.3574810, 0.3475939, 0.3391182 ] ebbox = [-0.17990133, 1.17967345, 1.17983512, -0.17983547] numpy.testing.assert_allclose(dilated.lons, elons, rtol=0, atol=1e-7) numpy.testing.assert_allclose(dilated.lats, elats, rtol=0, atol=1e-7) numpy.testing.assert_allclose(dilated._bbox, ebbox) self.assertIs(dilated._projection, poly._projection) self.assertEqual(len(dilated._polygon2d.boundary.coords), len(elons) + 1)
def test_counterclockwise(self): poly = polygon.Polygon( [geo.Point(5, 6), geo.Point(4, 6), geo.Point(4, 5)]) dilated = poly.dilate(20) elons = [ 5.1280424, 4.1280406, 4.1149304, 4.1007112, 4.0855200, 4.0695036, 4.0528165, 4.0356198, 4.0180793, 4.0003644, 3.9826460, 3.9650949, 3.9478807, 3.9311693, 3.9151220, 3.8998938, 3.8856315, 3.8724729, 3.8605451, 3.8499631, 3.8408293, 3.8332319, 3.8272443, 3.8229245, 3.8203143, 3.8194390, 3.8191353, 3.8199981, 3.8225928, 3.8268944, 3.8328618, 3.8404377, 3.8495493, 3.8601090, 3.8720154, 3.8851539, 3.8993981, 3.9146108, 3.9306458, 3.9473485, 3.9645582, 3.9821092, 3.9998325, 5.0001675, 5.0178827, 5.0354259, 5.0526283, 5.0693244, 5.0853535, 5.1005616, 5.1148024, 5.1279389, 5.1398448, 5.1504059, 5.1595205, 5.1671011, 5.1730751, 5.1773852, 5.1799900, 5.1808646, 5.1800010, 5.1774074, 5.1731090, 5.1671473, 5.1595797, 5.1504790, 5.1399327 ] elats = [ 5.8729890, 4.8731997, 4.8612916, 4.8507226, 4.8415948, 4.8339963, 4.8280004, 4.8236650, 4.8210319, 4.8201265, 4.8209575, 4.8235169, 4.8277800, 4.8337058, 4.8412369, 4.8503008, 4.8608099, 4.8726630, 4.8857454, 4.8999311, 4.9150831, 4.9310551, 4.9476930, 4.9648361, 4.9823190, 4.9999728, 5.9999728, 6.0175942, 6.0350466, 6.0521621, 6.0687761, 6.0847286, 6.0998661, 6.1140429, 6.1271226, 6.1389792, 6.1494986, 6.1585795, 6.1661344, 6.1720906, 6.1763909, 6.1789936, 6.1798739, 6.1798739, 6.1789944, 6.1763940, 6.1720977, 6.1661467, 6.1585984, 6.1495254, 6.1390149, 6.1271681, 6.1140988, 6.0999329, 6.0848065, 6.0688651, 6.0522620, 6.0351568, 6.0177140, 6.0001013, 5.9824878, 5.9650430, 5.9479344, 5.9313265, 5.9153789, 5.9002447, 5.8860693 ] ebbox = [3.81913534, 5.18086464, 6.17987385, 4.82012646] numpy.testing.assert_allclose(dilated.lons, elons, rtol=0, atol=1e-7) numpy.testing.assert_allclose(dilated.lats, elats, rtol=0, atol=1e-7) numpy.testing.assert_allclose(dilated._bbox, ebbox) self.assertIs(dilated._projection, poly._projection) self.assertEqual(len(dilated._polygon2d.boundary.coords), len(elons) + 1)