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 discretize(self, mesh_spacing): """ Get a mesh of uniformly spaced points inside the polygon area with distance of ``mesh_spacing`` km between. :returns: An instance of :class:`~nhlib.geo.mesh.Mesh` that holds the points data. Mesh is created with no depth information (all the points are on the Earth surface). """ self._init_polygon2d() west, east, north, south = self._bbox lons = [] lats = [] # we cover the bounding box (in spherical coordinates) from highest # to lowest latitude and from left to right by longitude. we step # by mesh spacing distance (linear measure). we check each point # if it is inside the polygon and yield the point object, if so. # this way we produce an uniformly-spaced mesh regardless of the # latitude. latitude = north while latitude > south: longitude = west while utils.get_longitudinal_extent(longitude, east) > 0: # we use Cartesian space just for checking if a point # is inside of the polygon. x, y = self._projection(longitude, latitude) if self._polygon2d.contains(shapely.geometry.Point(x, y)): lons.append(longitude) lats.append(latitude) # move by mesh spacing along parallel... longitude, _, = geodetic.point_at(longitude, latitude, 90, mesh_spacing) # ... and by the same distance along meridian in outer one _, latitude = geodetic.point_at(west, latitude, 180, mesh_spacing) lons = numpy.array(lons) lats = numpy.array(lats) return Mesh(lons, lats, depths=None)
def test_international_date_line(self): self.assertEqual(utils.get_longitudinal_extent(-178.3, 177.7), -4) self.assertEqual(utils.get_longitudinal_extent(177.7, -178.3), 4) self.assertEqual(utils.get_longitudinal_extent(95, -180 + 94), 179) self.assertEqual(utils.get_longitudinal_extent(95, -180 + 96), -179)
def test_negative(self): self.assertEqual(utils.get_longitudinal_extent(20, 10), -10) self.assertEqual(utils.get_longitudinal_extent(-10, -15), -5)
def test_positive(self): self.assertEqual(utils.get_longitudinal_extent(10, 20), 10) self.assertEqual(utils.get_longitudinal_extent(-120, 30), 150)