def test_addAndGetLocation(self): self.db.addLocations([location(45, 5, 150)]) self.assertEqual(self.db.getLocation(0, 0), None) storedLocation = self.db.getLocation(45, 5) self.assertEqual(storedLocation.latitude, 45) self.assertEqual(storedLocation.longitude, 5) self.assertEqual(storedLocation.elevation, 150)
def getLocation(self, latitude, longitude): cur = self.connection.cursor() cur.execute( "select elevation from location where latitude = ? and longitude = ?", (latitude, longitude)) elevationTuple = cur.fetchone() cur.close() if elevationTuple == None: return None else: return location(latitude, longitude, elevationTuple[0])
def normalizeAllCoordinates(self): for elevation in self.getAllElevations(): cur = self.connection.cursor() cur.execute( "select latitude, longitude, elevation from location where elevation = ?", [elevation]) results = cur.fetchall() cur.close() locationsAtElevation = list( map(lambda result: location(result[0], result[1], result[2]), results)) self.normalizeCoordinates(locationsAtElevation)
def test_getStableLocatiosnInZone(self): swCorner = location(0, 0) neCorner = swCorner.moveNorth(200).moveEast(200) manyLocations = location.getAlignedLocationsInZone(swCorner, neCorner) self.assertEqual(9, len(manyLocations)) singleLocation = location.getAlignedLocationsInZone( swCorner.moveNorth(1).moveEast(1), neCorner.moveNorth(-1).moveEast(-1)) self.assertEqual(1, len(singleLocation)) self.assertEqual(singleLocation[0].latitude, manyLocations[4].latitude) self.assertEqual(singleLocation[0].longitude, manyLocations[4].longitude)
def test_normalizeLocation(self): duingt = location(45.831381, 6.205998, 450) normalizedDuingt = duingt.normalize() rio = location(-22.951564, -43.210753, 493) normalizedRio = rio.normalize() self.db.addLocations([rio, duingt]) self.db.normalizeCoordinates([rio]) self.assertEqual(self.db.getLocation(rio.latitude, rio.longitude), None) self.assertNotEqual( self.db.getLocation(normalizedRio.latitude, normalizedRio.longitude), None) self.assertNotEqual( self.db.getLocation(duingt.latitude, duingt.longitude), None) self.db.normalizeAllCoordinates() self.assertEqual( self.db.getLocation(duingt.latitude, duingt.longitude), None) self.assertNotEqual( self.db.getLocation(normalizedDuingt.latitude, normalizedDuingt.longitude), None)
def test_getNearestLocationOfDuingt(self): duingt = location(45.835343, 6.205914) nearestAlignedLocations = duingt.getNearestAlignedLocations() self.assertEqual(len(nearestAlignedLocations), 4) self.assertEqual( 2, len( list( filter(lambda l: l.latitude < duingt.latitude, nearestAlignedLocations)))) self.assertEqual( 2, len( list( filter(lambda l: l.latitude > duingt.latitude, nearestAlignedLocations)))) self.assertEqual( 2, len( list( filter(lambda l: l.longitude < duingt.longitude, nearestAlignedLocations)))) self.assertEqual( 2, len( list( filter(lambda l: l.longitude > duingt.longitude, nearestAlignedLocations)))) for nearestLocation in nearestAlignedLocations: self.assertAlmostEquals(nearestLocation.latitude, duingt.latitude, 2) self.assertAlmostEquals(nearestLocation.longitude, duingt.longitude, 2)
def test_getNearestLocationOfRio(self): rio = location(-22.955044, -43.215391) nearestAlignedLocations = rio.getNearestAlignedLocations() self.assertEqual(len(nearestAlignedLocations), 4) self.assertEqual( 2, len( list( filter(lambda l: l.latitude < rio.latitude, nearestAlignedLocations)))) self.assertEqual( 2, len( list( filter(lambda l: l.latitude > rio.latitude, nearestAlignedLocations)))) self.assertEqual( 2, len( list( filter(lambda l: l.longitude < rio.longitude, nearestAlignedLocations)))) self.assertEqual( 2, len( list( filter(lambda l: l.longitude > rio.longitude, nearestAlignedLocations)))) for nearestLocation in nearestAlignedLocations: self.assertAlmostEquals(nearestLocation.latitude, rio.latitude, 2) self.assertAlmostEquals(nearestLocation.longitude, rio.longitude, 2)
def test_pointOnNorthPoleIsTenThousandKilometerFromEquator(self): self.assertEqual( location(90, 0).distanceFromEquatorInMeter(), 10000000)
def test_getNearestLocationOfZeroZero(self): zeroZero = location(0, 0) nearestAlignedLocations = zeroZero.getNearestAlignedLocations() self.assertEqual(len(nearestAlignedLocations), 1) self.assertEqual(nearestAlignedLocations[0].latitude, 0) self.assertEqual(nearestAlignedLocations[0].longitude, 0)
def test_pointOnEquatorIsZeroMeterFromEquator(self): self.assertEqual(location(0, 0).distanceFromEquatorInMeter(), 0)
def test_MoveEastFromBordeauxToValence(self): start = location(45, 0) finish = start.moveEast(400000) self.assertEqual(finish.latitude, start.latitude) self.assertGreater(finish.longitude, 4.9) self.assertLess(finish.longitude, 5.1)
def test_MoveNorthFromEquatorToNorthPole(self): start = location(0, 38.2) finish = start.moveNorth(10000000) self.assertEqual(finish.longitude, start.longitude) self.assertEqual(finish.latitude, 90)
def test_pointOnFortyFiveDegreeSouthIsMinusFiveThousandKilometerFromEquator( self): self.assertEqual( location(-45, 0).distanceFromEquatorInMeter(), -5000000)
def downloadLocations(self, locations): return map(lambda l: location(l.latitude, l.longitude, self.computeFakeAltitude(l)), locations)
import json import geoTools from geoTools import openElevation from geoTools.locationStorage import locationDatabase from geoTools.geoCoordinates import location from geoTools.elevationDownload import elevationManager db = locationDatabase("./bin/locations.db") #db.normalizeAllCoordinates() manager = elevationManager(db, openElevation.openElevationClient()) locations = manager.getLocationsFromZone(location(45.855830, 6.196099), location(45.761282, 6.343284)) print(json.dumps(locations, default=lambda o: o.__dict__))