def test_point_assign_coordinates(self): point = Point(self.lat + 10, self.lon + 10, self.alt + 10) point.latitude = self.lat point.longitude = self.lon point.altitude = self.alt self.assertEqual(point[0], self.lat) self.assertEqual(point[1], self.lon) self.assertEqual(point[2], self.alt) self.assertEqual(self.coords, tuple(point)) self.assertEqual(point.latitude, self.lat) self.assertEqual(point.longitude, self.lon) self.assertEqual(point.altitude, self.alt)
def test_format(self): """ format_degrees """ self.assertEqual( format_degrees(Point.parse_degrees('-13', '19', 0)), "-13 19\' 0.0\"" )
def test_reverse_with_mode_areas(self): """ Here.reverse using mode parameter 'retrieveAreas'. """ res = self.reverse_run( {"query": Point(40.753898, -73.985071), "mode": "retrieveAreas"}, {} ) self.assertIn("Theater District-Times Square", res.address)
def test_reverse_with_language_en(self): """ Here.reverse using point and language parameter to get an English response """ res = self.reverse_run( {"query": Point(40.753898, -73.985071), "language": "en-US"}, {} ) self.assertIn("United States", res.address)
def test_reverse_with_language_de(self): """ Here.reverse using point and language parameter to get a non-English response """ res = self.reverse_run( {"query": Point(40.753898, -73.985071), "language": "de-DE"}, {} ) self.assertIn("Vereinigte Staaten", res.address)
def test_point_str_simple(self): """ Point() str """ for each in ("%s,%s", "%s %s", "%s;%s"): point = Point(each % (self.lat, self.lon)) self.assertEqual(point.longitude, self.lon) self.assertEqual(point.latitude, self.lat) self.assertEqual(point.altitude, 0)
def test_reverse_with_include_country_code(self): res = self.reverse_run( { "query": Point(40.753898, -73.985071), "include_country_code": True }, {}, ) assert res.raw["address"].get("countryRegionIso2", 'missing') == 'US'
def test_reverse(self): location = self.reverse_run( {"query": Point(40.75376406311989, -73.98489005863667)}, { "latitude": 40.75376406311989, "longitude": -73.98489005863667 }, ) self.assertIn("new york", location.address.lower())
def test_reverse_point(self): """ Photon.reverse Point """ result = self.reverse_run( {"query": Point(45.7733105, 4.8869339)}, {"latitude": 45.7733105, "longitude": 4.8869339} ) self.assertIn("France", result.address)
def test_reverse_with_culture_de(self): res = self.reverse_run( { "query": Point(40.753898, -73.985071), "culture": "DE" }, {}, ) assert "Vereinigte Staaten von Amerika" in res.address
async def test_reverse_point(self): location = await self.reverse_run( {"query": Point(40.753898, -73.985071)}, { "latitude": 40.75376406311989, "longitude": -73.98489005863667 }, ) assert 'New York' in location.address
def test_viewbox(self): res = self.geocode_run( {"query": "Maple Street"}, {}, ) self.assertFalse(50 <= res.latitude <= 52) self.assertFalse(-0.15 <= res.longitude <= -0.11) for viewbox in [((52, -0.11), (50, -0.15)), [Point(52, -0.11), Point(50, -0.15)], (("52", "-0.11"), ("50", "-0.15"))]: self.geocode_run({ "query": "Maple Street", "viewbox": viewbox }, { "latitude": 51.5223513, "longitude": -0.1382104 })
def test_reverse_point(self): """ Mapzen.reverse Point """ self.reverse_run( {"query": Point(40.75376406311989, -73.98489005863667)}, { "latitude": 40.75376406311989, "longitude": -73.98489005863667 })
def test_reverse(self): known_addr = '1067 6th Ave, New York, NY 10018, United States' known_coords = (40.75376406311989, -73.98489005863667) result = self.geocoder.reverse(Point(40.753898, -73.985071)) addr, coords = result self.assertTrue(result.raw is not None) self.assertEqual(str_coerce(addr), known_addr) self.assertAlmostEqual(coords[0], known_coords[0], delta=self.delta_exact) self.assertAlmostEqual(coords[1], known_coords[1], delta=self.delta_exact)
def measure(self, a, b): a, b = Point(a), Point(b) lat1, lng1 = radians(degrees=a.latitude), radians(degrees=a.longitude) lat2, lng2 = radians(degrees=b.latitude), radians(degrees=b.longitude) sin_lat1, cos_lat1 = sin(lat1), cos(lat1) sin_lat2, cos_lat2 = sin(lat2), cos(lat2) delta_lng = lng2 - lng1 cos_delta_lng, sin_delta_lng = cos(delta_lng), sin(delta_lng) d = atan2(sqrt((cos_lat2 * sin_delta_lng) ** 2 + (cos_lat1 * sin_lat2 - sin_lat1 * cos_lat2 * cos_delta_lng) ** 2), sin_lat1 * sin_lat2 + cos_lat1 * cos_lat2 * cos_delta_lng) return self.RADIUS * d
def test_reverse_with_culture_en(self): res = self.reverse_run( { "query": Point(40.753898, -73.985071), "culture": "EN" }, {}, ) assert "United States" in res.address
def test_reverse_point(self): """ PickPoint.reverse Point """ location = self.reverse_run( {"query": Point(40.75376406311989, -73.98489005863667)}, {"latitude": 40.75376406311989, "longitude": -73.98489005863667} ) self.assertIn("New York", location.address)
def test_get_geolocation_address(self, mock_googlemaps): client = mock_googlemaps.Client.return_value client.geocode.return_value = [ {"geometry": {"location": {"lat": 45, "lng": -75}}} ] location = Geocoding().get_from_address(ADDRESS) self.assertEqual(location, Point(45.0, -75.0)) client.geocode.assert_called_once_with(address=ADDRESS)
def test_reverse_with_culture_en(self): """ Bing.reverse using point and culture parameter to get an english response """ res = self.reverse_run( {"query": Point(40.753898, -73.985071), "culture": "EN"}, {}, ) self.assertIn("United States", res.address)
def test_reverse_with_culture_de(self): """ Bing.reverse using point and culture parameter to get a non english response """ res = self.reverse_run( {"query": Point(40.753898, -73.985071), "culture": "DE"}, {}, ) self.assertIn("Vereinigte Staaten von Amerika", res.address)
def choose_listeners(self, pokemon, spawntype, center): self.open_db() longest = geopy.distance.distance(kilometers=16) hexgon = "POLYGON((" + ",".join([ "%g %g" % (point.latitude, point.longitude) for point in [ longest.destination(center, angle) for angle in range(0, 361, 60) ] ]) + "))" dbh = self.db.cursor() fmt = "select t2.discordid, t2.pm_channel, t1.distance, ST_AsText(t1.coord) from {me} as t1, {master} as t2 where t2.on_off = 'y' and MBRContains(ST_GeomFromText('{hexgon}'), t1.coord) and t1.settingid = t2.settingid and dexno={dexno} and t1.spawntype = {spawntype}" sql = fmt.format(me=self.table_name, master=self.master_table, hexgon=hexgon, dexno=self.pokemons[pokemon], spawntype=spawntype) dbh.execute(sql) users = [] user_loc = Point() while True: row = dbh.fetchone() if row == None: break discordid, pm_channel, distance, coord = row lat_lng = [ float(value) for value in coord.replace("POINT(", "").replace( ")", "").split(" ") ] user_loc.latitude = lat_lng[0] user_loc.longitude = lat_lng[1] dt = geopy.distance.vincenty(center, user_loc) if dt.m <= distance: users.append((discordid, pm_channel, center, dt.m)) pass pass dbh.close() self.db.commit() self.close_db() return users
def test_reverse_point_radius_10(self): """ Here.reverse Point with radius """ # needs more testing res = self.reverse_run( {"query": Point(40.753898, -73.985071), "radius": 10, "exactly_one": False}, {"latitude": 40.753898, "longitude": -73.985071} ) self.assertGreater(len(res), 5)
async def test_reverse(self): location = await self.reverse_run( {"query": Point(40.75376406311989, -73.98489005863667)}, { "latitude": 40.75376406311989, "longitude": -73.98489005863667 }, skiptest_on_failure=True, # sometimes the result is empty ) assert "new york" in location.address.lower()
def _format_bounding_box(bbox, output_format="%(lat1)s,%(lon1)s,%(lat2)s,%(lon2)s"): """ Transform bounding box boundaries to a string matching `output_format` from the following formats: - [Point(lat1, lon1), Point(lat2, lon2)] - [[lat1, lon1], [lat2, lon2]] - ["lat1,lon1", "lat2,lon2"] It is guaranteed that lat1 <= lat2 and lon1 <= lon2. """ if len(bbox) != 2: raise GeocoderQueryError("Unsupported format for a bounding box") p1, p2 = bbox p1, p2 = Point(p1), Point(p2) return output_format % dict(lat1=min(p1.latitude, p2.latitude), lon1=min(p1.longitude, p2.longitude), lat2=max(p1.latitude, p2.latitude), lon2=max(p1.longitude, p2.longitude))
def test_reverse_wkid(self): """ ArcGIS.reverse with non-default WKID """ known_addr = '1065 6th Ave, New York, New York 10018, USA' known_coords = (4976084.454557315, -8235967.638346817) addr, coords = self.geocoder.reverse(Point(40.753898, -73.985071), wkid=102100) self.assertEqual(str_coerce(addr), known_addr) self.assertAlmostEqual(coords[0], known_coords[0], delta=self.delta_inexact) self.assertAlmostEqual(coords[1], known_coords[1], delta=self.delta_inexact)
def test_reverse_with_include_country_code(self): """ Bing.reverse using point and include country-code in the response """ res = self.reverse_run( {"query": Point(40.753898, -73.985071), "include_country_code": True}, {}, ) self.assertEqual(res.raw["address"].get("countryRegionIso2", 'missing'), 'US')
async def test_reverse_with_maxresults_5(self): res = await self.reverse_run( { "query": Point(40.753898, -73.985071), "maxresults": 5, "exactly_one": False }, {} ) assert len(res) == 5
def test_point_setitem(self): """ Point.__setitem__ """ point = Point(self.lat + 10, self.lon + 10, self.alt + 10) for each in (0, 1, 2): point[each] = point[each] - 10 self.assertEqual(point[0], self.lat) self.assertEqual(point[1], self.lon) self.assertEqual(point[2], self.alt)
async def test_reverse_limit(self): res = await self.reverse_run( { "query": Point(40.753898, -73.985071), "limit": 5, "exactly_one": False }, {} ) assert len(res) == 5
def destination(self, point, bearing, distance=None): point = Point(point) lat1 = point.latitude lon1 = point.longitude azi1 = bearing if distance is None: distance = self if isinstance(distance, Distance): distance = distance.kilometers if not (isinstance(self.geod, Geodesic) and self.geod.a == self.ELLIPSOID[0] and self.geod.f == self.ELLIPSOID[2]): self.geod = Geodesic(self.ELLIPSOID[0], self.ELLIPSOID[2]) r = self.geod.Direct(lat1, lon1, azi1, distance, Geodesic.LATITUDE | Geodesic.LONGITUDE) return Point(r['lat2'], r['lon2'])
def parse_json(json_string): results = [] listings = json.loads(json_string) for listing in listings[0]: lat = float(listing['Latitude']) long_ = float(listing['Longitude']) if lat >= bottom and lat <= top and long_ >= left and long_ <= right: p = Point(lat,long_) results.append((p,listing)) return results
def test_reverse_point(self): """ Photon.reverse Point """ self.reverse_run({"query": Point(45.7733105, 4.8869339)}, { "address": "Rue du 8 Mai 1945, 69100, Villeurbanne, Auvergne-Rhône-Alpes, France", "latitude": 45.7733105, "longitude": 4.8869339 })
def test_point_format(self): """ Point.format() """ point = Point("51 19m 12.9s N, 0 1m 24.95s E") self.assertEqual(point.format(), "51 19m 12.9s N, 0 1m 24.95s E") point = Point("51 19m 12.9s N, -1 1m 24.95s E, 15000m") self.assertEqual(point.format(), "51 19m 12.9s N, 1 1m 24.95s W, 15.0km") # TODO # point = Point("51 19m 12.9s N, -0 1m 24.95s E") # self.assertEqual(point.format(), "51 19m 12.9s N, 0 1m 24.95s W") # TODO # with self.assertRaises(ValueError): # # Z is not a valid direction # Point("51 19m 12.9s Z, 0 1m 24.95s E") with self.assertRaises(ValueError): Point("gibberish")
def test_point_format_altitude(self): """ Point.format() includes altitude """ point = Point(latitude=41.5, longitude=81.0, altitude=2.5) self.assertEqual(point.format(), "41 30m 0s N, 81 0m 0s E, 2.5km") self.assertEqual(point.format_decimal(), "41.5, 81.0, 2.5km") self.assertEqual(point.format_decimal('m'), "41.5, 81.0, 2500.0m") point = Point(latitude=41.5, longitude=81.0) self.assertEqual(point.format_decimal(), "41.5, 81.0") self.assertEqual(point.format_decimal('m'), "41.5, 81.0, 0.0m")
def test_point_format(self): """ Point.format() """ point = Point("51 19m 12.9s N, 0 1m 24.95s E") self.assertEqual(point.format(), "51 19m 12.9s N, 0 1m 24.95s E")
def test_format(self): """ format_degrees """ self.assertEqual(format_degrees(Point.parse_degrees("-13", "19", 0)), "-13 19' 0.0\"")