Skip to content

Commit

Permalink
Removed Location class
Browse files Browse the repository at this point in the history
The `Location` class has been replaced with a Python dictionary storing
`latitude` and `longitude` key values. This solution is simpler and
more maintainable.
  • Loading branch information
jamestaylr committed Jun 12, 2015
1 parent e27a69d commit bdd414f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 47 deletions.
2 changes: 1 addition & 1 deletion ABOUT.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ A sample configuration file looks like this:

**Loading locations**

The code will then try to load locations from `locations.json` in the root `src` folder. This is written as JSON in accordance to the `Location` module.
The code will then try to load locations from `locations.json` in the root `src` folder. This is written as JSON with `latitude` and `longitude` keys.

A default `locations.json` should look like this:

Expand Down
3 changes: 1 addition & 2 deletions src/autonomous.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/usr/bin/python
import time, threading, json, logging, tornado.websocket, modules.calc, math, modules.utils, modules.log, socket, sys
from modules.server import ServerThread
from modules.location import Location
from modules.calc import direction_to_point
from modules.control_thread import StoppableThread

# Variables and constants
data = {'category': 'data', 'timestamp': 0, 'location': Location(0, 0),
data = {'category': 'data', 'timestamp': 0, 'location': { "latitude": 0, "longitude": 0 },
'heading': 0, 'speed': 0, 'wind_dir': 0, 'roll': 0, 'pitch': 0,
'yaw': 0}

Expand Down
26 changes: 13 additions & 13 deletions src/modules/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
point_proximity_radius = 5

def direction_to_point(current_point, target_point):
a = math.radians(current_point.latitude)
b = math.radians(target_point.latitude)
d = math.radians(target_point.longitude - current_point.longitude)
a = math.radians(current_point['latitude'])
b = math.radians(target_point['latitude'])
d = math.radians(target_point['longitude'] - current_point['longitude'])

y = math.sin(d) * math.cos(b)
x = math.cos(a) * math.sin(b) - math.sin(a) * math.cos(b) * math.cos(d)
Expand All @@ -24,24 +24,24 @@ def get_heading_angle(heading, current_point, target_point):
return heading - angle

def point_proximity(current_point, target_point):
a = math.sin(math.radians(current_point.latitude))
b = math.sin(math.radians(target_point.latitude))
c = math.cos(math.radians(current_point.latitude))
d = math.cos(math.radians(target_point.latitude))
a = math.sin(math.radians(current_point['latitude']))
b = math.sin(math.radians(target_point['latitude']))
c = math.cos(math.radians(current_point['latitude']))
d = math.cos(math.radians(target_point['latitude']))

e = a * b + c * d * math.cos((math.radians(target_point.longitude - current_point.longitude)))
e = a * b + c * d * math.cos((math.radians(target_point['longitude'] - current_point['longitude'])))
f = math.acos(e)
distance = f * 6371 * 1000

return (distance <= point_proximity_radius)

def distance(point1, point2):
a = math.sin(math.radians(point1.latitude))
b = math.sin(math.radians(point2.latitude))
c = math.cos(math.radians(point1.latitude))
d = math.cos(math.radians(point2.latitude))
a = math.sin(math.radians(point1['latitude']))
b = math.sin(math.radians(point2['latitude']))
c = math.cos(math.radians(point1['latitude']))
d = math.cos(math.radians(point2['latitude']))

e = a * b + c * d * math.cos((math.radians(point2.longitude - point1.longitude)))
e = a * b + c * d * math.cos((math.radians(point2['longitude'] - point1['longitude'])))
f = math.acos(e)

return f * 6371 * 1000
19 changes: 0 additions & 19 deletions src/modules/location.py

This file was deleted.

16 changes: 4 additions & 12 deletions src/modules/utils.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
#!/usr/bin/python
import json, logging, configparser, modules.calc, time, os, sys
from modules.location import Location
from datetime import datetime

def getJSON(obj):
return json.dumps(obj, default=lambda o: o.__dict__, sort_keys=True, indent=4)

def location_decoder(obj):
return Location(obj['latitude'], obj['longitude'])

def setup_locations(target_locations, boundary_locations):
try:
with open('locations.json', 'r') as myfile:
json_data = json.loads(myfile.read().replace('\n', ''))

i = []
j = []

for location in json_data['target_locations']:
target_locations.append(location_decoder(location))
i.append(location_decoder(location).__str__())
target_locations.append({"latitude": location["latitude"], "longitude": location["longitude"]})
for location in json_data['boundary_locations']:
boundary_locations.append(location_decoder(location))
j.append(location_decoder(location).__str__())
boundary_locations.append({"latitude": location["latitude"], "longitude": location["longitude"]})

logging.info("Loaded the following target locations: %s" % i)
logging.info("Loaded the following boundary locations: %s" % j)
logging.info("Loaded the following target locations: %s" % target_locations)
logging.info("Loaded the following boundary locations: %s" % boundary_locations)

except IOError:
logging.error('The locations JSON file could not be found!')
Expand Down

0 comments on commit bdd414f

Please sign in to comment.