Skip to content

Commit

Permalink
new PolylineWalker(StepWalker) - [was #990] (#1467)
Browse files Browse the repository at this point in the history
* new PolylineWalker(StepWalker)

Refactoring in the context of the new walker/navigator concept

Fixes:
-  PolylineWalker class renamed to Polyline
-  new class PolylineWalker(StepWalker)
-  change few tests

* fixed imports
  • Loading branch information
th3w4y authored and douglascamata committed Jul 28, 2016
1 parent 47cf9db commit 9938957
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pokemongo_bot/polyline_stepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import logger
from human_behaviour import sleep
from polyline_walker import PolylineWalker
from walkers.polyline_walker import PolylineWalker


class PolylineStepper(Stepper):
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from polyline_generator import Polyline
from polyline_walker import PolylineWalker

Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import time
from itertools import chain
from math import ceil

import haversine
import polyline
import requests


class PolylineWalker(object):
class Polyline(object):

def __init__(self, origin, destination, speed):
self.DISTANCE_API_URL='https://maps.googleapis.com/maps/api/directions/json?mode=walking'
self.origin = origin
self.destination = destination
self.URL = '{}&origin={}&destination={}'.format(self.DISTANCE_API_URL,
'{},{}'.format(*self.origin),
'{},{}'.format(*self.destination))
self.polyline_points = [x['polyline']['points'] for x in
requests.get(self.DISTANCE_API_URL+'&origin='+
self.origin+'&destination='+
self.destination
).json()['routes'][0]['legs'][0]['steps']]
requests.get(self.URL).json()['routes'][0]['legs'][0]['steps']]
self.speed = float(speed)
self.points = self.get_points(self.polyline_points)
self.points = [self.origin] + self.get_points(self.polyline_points) + [self.destination]
self.lat, self.long = self.points[0][0], self.points[0][1]
self.polyline = self.combine_polylines(self.points)
self._timestamp = time.time()
Expand Down Expand Up @@ -85,9 +84,15 @@ def get_pos(self):
return self.calculate_coord(percentage_walked, *steps_dict[walked_end_step])

def calculate_coord(self, percentage, o, d):
lat = o[0]+ (d[0] -o[0]) * percentage
lon = o[1]+ (d[1] -o[1]) * percentage
return [(lat, lon)]
# If this is the destination then returning as such
if self.points[-1] == d:
return [d]
else:
# intermediary points returned with 5 decimals precision only
# this ensures ~3-50cm ofset from the geometrical point calculated
lat = o[0]+ (d[0] -o[0]) * percentage
lon = o[1]+ (d[1] -o[1]) * percentage
return [(round(lat, 5), round(lon, 5))]

def get_total_distance(self):
return ceil(sum([haversine.haversine(*x)*1000 for x in self.walk_steps()]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import haversine
import polyline

from polyline_walker import PolylineWalker

a = PolylineWalker('Poststrasse+20,Zug,CH', 'Guggiweg+7,Zug,CH', 100)
from math import ceil
from polyline_generator import Polyline
a = Polyline((47.1706378, 8.5167405), (47.1700271, 8.518072999999998), 100)
print(a.points)
print('Walking polyline: ', a.polyline)
print('Encoded level: ','B'*len(a.points))
print('Initialted with speed: ', a.speed, 'm/s')
Expand Down
25 changes: 25 additions & 0 deletions pokemongo_bot/walkers/polyline_walker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from polyline_generator import Polyline
from math import ceil
from pokemongo_bot.human_behaviour import sleep
from pokemongo_bot.cell_workers.utils import i2f
from pokemongo_bot.step_walker import StepWalker
from pokemongo_bot import logger

class PolylineWalker(StepWalker):

def __init__(self, bot, speed, initLat, initLng, destLat, destLng):
super(PolylineWalker, self).__init__(bot, speed, initLat, initLng, destLat, destLng)
self.polyline_walker = Polyline((i2f(self.api._position_lat), i2f(self.api._position_lng)),
(self.destLat, self.destLng), self.speed)
logger.log('[#] {}'.format(self.polyline_walker.URL))

def step(self):
self.polyline_walker.unpause()
sleep(1)
self.polyline_walker.pause()
cLat, cLng = self.polyline_walker.get_pos()[0]
self.api.set_position(round(cLat, 5), round(cLng, 5), 0)
self.bot.heartbeat()
if self.destLat == cLat and self.destLng == cLng:
return True

0 comments on commit 9938957

Please sign in to comment.