def process(self, feature): """Do the work""" # get fromfeature speed = None if self.mode == MODE_DRIVING: mode = 'car' elif self.mode == MODE_WALKING: mode = 'walk' if self.walk_speed: speed = self.walk_speed elif self.mode == MODE_TRANSIT: mode = 'transit' elif self.mode == MODE_BICYCLING: mode = 'bike' if self.bike_speed: speed = self.bike_speed tm = {} tm[mode] = {} if speed: tm[mode]['speed'] = speed if self.mode == MODE_TRANSIT: tm[mode]['frame']= {} tm[mode]['frame']['date'] = self.date tm[mode]['frame']['time'] = self.time logging.debug(mode) sources = [] fromid = feature.attributes[self.idcolumn] point = feature.geometry.asPoint() attributes = feature.attributes sources.append(dict(id=attributes[self.idcolumn], lat=point.y(), lng=point.x(), tm=tm)) feat_dict = dict() for time in self.radii: config = dict(sources=[sources[0]], elevation=False, polygon=dict(serializer='geojson', pointReduction=True, values=[time*60], minPolygonHoleSize=1000000000) ) request = Request(API_ENDPOINT, cfg=quote(json.dumps(config)), key=self.key, _='1444398161936') servicearea = request.json['data'] if len(servicearea) != 3: print "FEJL, makker. len: {}".format(len(servicearea)) feat_dict[time] = servicearea return (fromid, feat_dict)
def process(self, feature): """Do the work. How is the work split out? We have NxM combinations. API can only take smaller number of to-coords at a time. """ geometry = feature.geometry attributes = feature.attributes # get fromfeature point = geometry.asPoint() fromcoord = '{},{}'.format(point.y(), point.x()) # build api request values = [] routes = [] for tocoords in batch(self.tocoords, COORD_BATCH_SIZE): try: mot = MODE2GG_MOT[self.mode] request = Request( 'http://viamap.dk', 'router', fromcoord=fromcoord, tocoords='+'.join(tocoords), mot=mot, traveltime=self.traveltime, timevalue=self.timevalue, zoomlevel=self. routelinetype, # The zoomlevel refers to the polyline. decodepolyline=1) logging.debug("url %s" % str(request.url)) distances = request.json if self.unit is UNIT_METERS: batchvalues = [d['routedmeters'] for d in distances] if self.unit is UNIT_MINUTES: batchvalues = [d['travelseconds'] / 60 for d in distances] if self.format == FORMAT_ROUTES: batch_routes = [d['routepolyline'] for d in distances] if self.format == FORMAT_LINES: batch_routes = [[ d['routepolyline'][0], d['routepolyline'][-1] ] for d in distances] except Exception as e: logging.warn(e) batchvalues = ['error' for i in tocoords] values = values + batchvalues if self.format == FORMAT_ROUTES or self.format == FORMAT_LINES: routes = routes + batch_routes fromid = attributes[self.fromidcolumn] if self.format == FORMAT_ROUTES or self.format == FORMAT_LINES: return (fromid, zip(values, routes)) else: return (fromid, values)
def __init__(self, base, *segments, **params): """Perform a request in each language and return a list of responses. Positional arguments become url segments appended to the base url, while keyword arguments turn into query parameters. """ self.url = self.buildurl(base, *segments, **params) logging.debug('URl is {}'.format(self.url)) self.raw = self.process() self.json = self._parsejson(self.raw)
def stop(self): """Attempts to gracefully stop worker Forcefully terminates otherwise.""" logging.debug('Worker: trying to stop gracefully') if not self.running: logging.debug('Worker was not running') return try: self.worker.kill() if not self.thread.wait(settings.KILL_TIMEOUT): raise KillTimeoutError() except KillTimeoutError: logging.warn('Worker: kill timed out, force terminating...') self.thread.terminate() self.thread.wait() except Exception as e: logging.warn('Worker: terminate failed!?') logging.warn(e)
def kill(self): """Flag worker to die on next iteration""" logging.debug('[WORKER] flagged for suicide') self.suicide = True
def handlekilled(self): logging.debug('[WORKER] killed')
def handlecompleted(self, result): """Slot that saves result, useful for testing""" logging.debug('[WORKER] completed') self.result = result
def handlefinished(self): logging.debug('[WORKER] finished') self.running = False