def test_mesh_spacing_uniformness(self): MESH_SPACING = 10 tl = geo.Point(60, 60) tr = geo.Point(70, 60) bottom_line = [geo.Point(lon, 58) for lon in xrange(70, 59, -1)] poly = geo.Polygon([tl, tr] + bottom_line) mesh = poly.discretize(mesh_spacing=MESH_SPACING) self.assertIsInstance(mesh, geo.Mesh) mesh = list(mesh) for i, point in enumerate(mesh): if i == len(mesh) - 1: # the point is last in the mesh break next_point = mesh[i + 1] if next_point.longitude < point.longitude: # this is the next row (down along the meridian). # let's check that the new row stands exactly # mesh spacing kilometers below the previous one. self.assertAlmostEqual( point.distance(geo.Point(point.longitude, next_point.latitude)), MESH_SPACING, places=4 ) continue dist = point.distance(next_point) self.assertAlmostEqual(MESH_SPACING, dist, places=4)
def test_valid_points(self): points = [geo.Point(170, -10), geo.Point(170, 10), geo.Point(176, 0), geo.Point(-170, -5), geo.Point(-175, -10), geo.Point(-178, -6)] poly = geo.Polygon(points) self.assertEqual(len(poly.lons), 6) self.assertEqual(len(poly.lats), 6) self.assertEqual(list(poly.lons), [170, 170, 176, -170, -175, -178]) self.assertEqual(list(poly.lats), [-10, 10, 0, -5, -10, -6]) self.assertEqual(poly.lons.dtype, 'float') self.assertEqual(poly.lats.dtype, 'float')
def validate_site_model(sm_nodes, sites): """Given the geometry for a site model and the geometry of interest for the calculation, make sure the geometry of interest lies completely inside of the convex hull formed by the site model locations. If a site of interest lies directly on top of a vertex or edge of the site model area (a polygon), it is considered "inside" :param sm_nodes: Sequence of :class:`~openquake.db.models.SiteModel` objects. :param sites: Sequence of :class:`~openquake.shapes.Site` objects which represent the calculation points of interest. :raises: :exc:`openquake.job.config.ValidationException` if the area of interest (given as a collection of sites) is not entirely contained by the site model. """ sm_mp = geometry.MultiPoint( [(n.location.x, n.location.y) for n in sm_nodes] ) sm_poly = nhlib_geo.Polygon( [nhlib_geo.Point(*x) for x in sm_mp.convex_hull.exterior.coords] ) interest_mesh = nhlib_geo.Mesh.from_points_list(sites) # "Intersects" is the correct operation (not "contains"), since we're just # checking a collection of points (mesh). "Contains" would tell us if the # points are inside the polygon, but would return `False` if a point was # directly on top of a polygon edge or vertex. We want these points to be # included. intersects = sm_poly.intersects(interest_mesh) if not intersects.all(): raise ValidationException( ['Sites of interest are outside of the site model coverage area.' ' This configuration is invalid.'] )
def test_no_points_outside_of_polygon(self): dist = 1e-4 points = [ geo.Point(0, 0), geo.Point(dist * 4.5, 0), geo.Point(dist * 4.5, -dist * 4.5), geo.Point(dist * 3.5, -dist * 4.5), geo.Point(dist * (4.5 - 0.8), -dist * 1.5), geo.Point(0, -dist * 1.5) ] poly = geo.Polygon(points) mesh = list(poly.discretize(mesh_spacing=1.1e-2)) self.assertEqual(mesh, [ geo.Point(dist, -dist), geo.Point(dist * 2, -dist), geo.Point(dist * 3, -dist), geo.Point(dist * 4, -dist), geo.Point(dist * 4, -dist * 2), geo.Point(dist * 4, -dist * 3), geo.Point(dist * 4, -dist * 4), ])
def test_polygon_on_international_date_line(self): MESH_SPACING = 10 bl = geo.Point(177, 40) bml = geo.Point(179, 40) bmr = geo.Point(-179, 40) br = geo.Point(-177, 40) tr = geo.Point(-177, 43) tmr = geo.Point(-179, 43) tml = geo.Point(179, 43) tl = geo.Point(177, 43) poly = geo.Polygon([bl, bml, bmr, br, tr, tmr, tml, tl]) mesh = list(poly.discretize(mesh_spacing=MESH_SPACING)) west = east = mesh[0] for point in mesh: if geo_utils.get_longitudinal_extent(point.longitude, west.longitude) > 0: west = point if geo_utils.get_longitudinal_extent(point.longitude, east.longitude) < 0: east = point self.assertLess(west.longitude, 177.15) self.assertGreater(east.longitude, -177.15)
def assert_failed_creation(self, points, exc, msg): with self.assertRaises(exc) as ae: geo.Polygon(points) self.assertEqual(ae.exception.message, msg)
def setUp(self): self.corners = [ geo.Point(-10, 10), geo.Point(10, 10), geo.Point(10, -10), geo.Point(-10, -10), ] self.poly = geo.Polygon(self.corners)