-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a navigator that walks along specified points.
- Loading branch information
1 parent
dfa4f78
commit 2102682
Showing
5 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- coding: utf-8 -*- | ||
import logger | ||
import gpxpy | ||
import gpxpy.gpx | ||
import json | ||
from cell_workers.utils import distance, i2f, format_dist | ||
from human_behaviour import sleep | ||
from step_walker import StepWalker | ||
from pgoapi.utilities import f2i | ||
|
||
|
||
class PathNavigator(object): | ||
def __init__(self, bot): | ||
self.bot = bot | ||
self.api = bot.api | ||
self.config = bot.config | ||
self.ptr = 0 | ||
self.points = self.load_path() | ||
|
||
|
||
def load_path(self): | ||
if self.config.navigator_path_file == None: | ||
raise RuntimeError('You need to specify a path file (json or gpx)') | ||
|
||
if self.config.navigator_path_file.endswith('.json'): | ||
return self.load_json(self.config.navigator_path_file) | ||
elif self.config.navigator_path_file.endswith('.gpx'): | ||
return self.load_gpx(self.config.navigator_path_file) | ||
|
||
def load_json(self, file): | ||
with open(file) as data_file: | ||
return json.load(data_file) | ||
|
||
def load_gpx(self, file): | ||
gpx_file = open(file, 'r') | ||
gpx = gpxpy.parse(gpx_file) | ||
|
||
if len(gpx.tracks) == 0: | ||
raise RuntimeError('GPX file does not cotain a track') | ||
|
||
points = [] | ||
track = gpx.tracks[0] | ||
for segment in track.segments: | ||
for point in segment.points: | ||
points.append({"lat": point.latitude, "lng": point.longitude}) | ||
|
||
return points; | ||
|
||
def take_step(self): | ||
point = self.points[self.ptr] | ||
lat = float(point['lat']) | ||
lng = float(point['lng']) | ||
|
||
if self.config.walk > 0: | ||
step_walker = StepWalker( | ||
self.bot, | ||
self.config.walk, | ||
lat, | ||
lng | ||
) | ||
|
||
is_at_destination = False | ||
if step_walker.step(): | ||
is_at_destination = True | ||
|
||
else: | ||
self.api.set_position(lat, lng) | ||
|
||
dist = distance( | ||
self.api._position_lat, | ||
self.api._position_lng, | ||
lat, | ||
lng | ||
) | ||
|
||
if dist <= 1 or (self.config.walk > 0 and is_at_destination): | ||
if (self.ptr + 1) == len(self.points): | ||
self.ptr = 0 | ||
if self.config.navigator_path_mode == 'linear': | ||
self.points = list(reversed(self.points)) | ||
else: | ||
self.ptr += 1 | ||
|
||
return [lat, lng] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ flask==0.11.1 | |
socketIO_client==0.7.0 | ||
eventlet==0.19.0 | ||
universal-analytics-python==0.2.4 | ||
gpxpy==1.1.1 |