def inSantiago(point): santi = Point(SANTI_LAT,SANTI_LON) if kilDist(point, santi) < SANTIAGO_RADIUS: #print kilDist(point, santi) return True else: return False
def getTotalDistanceTraveled(truckId, datenum, db=WATTS_DATA_DB_KEY, ): ts = getTruckPoints(truckId, datenum, db) dist, tme = 0, 0 totalDistance = 0 first = True for t in ts: if first: dist = t.point first = False continue totalDistance += kilDist(dist, t.point) dist = t.point return totalDistance
def findStops(truckId, dateNum, db=WATTS_DATA_DB_KEY, constraint=None): if constraint is None: constraint = CONSTRAINT points = getTruckPoints(truckId, db, dateNum) numPoints = len(points) first = None last = None cluster = [] clusters = [] for i in range(0, numPoints): point = points[i] if first is None: first = point cluster.append(first) else: if kilDist(first, point) < constraint: last = point cluster.append(last) first = getCentroid(cluster) else: if len(cluster) > 1: clusters.append(cluster) last = None first = point cluster = [first] if len(cluster) > 1: clusters.append(cluster) lengths = [len(i) for i in clusters] zeros = [getNumZeros(i) for i in clusters] times = [getStopTime(i) for i in clusters] centroids = [getCentroid(i) for i in clusters] diameters = [getDiameter(i) for i in clusters] distances = getDistances(centroids, kilDist) startStops = [getStartStop(i, timeFunc=getClockTime) for i in clusters] filtered = [] for i in range(len(clusters)): if times[i] >= MIN_STOP_TIME: stop = {} stop[POINT_KEY] = centroids[i] stop[RADIUS_KEY] = diameters[i] / 2 stop[START_STOP_KEY] = startStops[i] filtered.append(stop) return filtered
def getTotalTimeOnRoad(truckId, datenum, db=WATTS_DATA_DB_KEY, ): ts = getTruckPoints(truckId, datenum, db) dist, tme = 0, 0 totalDistance = 0 totalTime = datetime.timedelta(hours=0, minutes=0, seconds=0) first = True for t in ts: if first: dist = t.point tme = t.time first = False continue curr = kilDist(dist, t.point) totalDistance += curr if curr > 0: x = getTimeDeltas(t.time) - getTimeDeltas(tme) totalTime = totalTime + x dist = t.point tme = t.time return totalTime.total_seconds() / 3600
def computeStopData(): print 'processing stops for each truck and date - this will take time, please be patient' stops = {} stats = {} stopList = [] first = None stop_id = 1 masterList = findStopsAll() for ml in masterList: addRow = True if first is None: point = (stop_id, float(ml[2]),float(ml[3])) stats[TRUCK_ID_KEY] = ml[1] stats[DATE_NUM_KEY] = ml[0] t1 = getTime(int(ml[5].split(":")[0]),int(ml[5].split(":")[1])) t2 = getTime(int(ml[6].split(":")[0]),int(ml[6].split(":")[1])) stats[TIME_KEY] = t1 stats[DURATION_KEY] = getDuration(t1,t2) stats[LAT_KEY] = point[1] stats[LON_KEY] = point[2] stats[RADIUS_KEY] = ml[4] stopList.append(stats) stops[point] = stopList first = 1 stats = {} stopList = [] else: # get the new line from the document and get the lat,long for the stop # compare to the existing stops using kildist and if its less than constraint # if yes, then it means they are the same stop. # add to the list under the stop entry without incrementing stop ID # add the truck id, time, duration, datenum # if no, then it means its a differnet stop # add a new entry with an id. # add the truck id, datenum, stop time, and duration oldPoint = {} newPoint = {} newPoint[LAT_KEY] = float(ml[2]) newPoint[LON_KEY] = float(ml[3]) keys = stops.keys() for i in keys: oldPoint[LAT_KEY] = i[1] oldPoint[LON_KEY] = i[2] if (kilDist(oldPoint,newPoint)) <= CONSTRAINT: #if yes, then it means they are the same stop. # add to the list under the stop entry without incrementing stop prop ID # add the truck id, time, duration, datenum, stop id, radius # print "same stop so adding to the list of stops" stats[TRUCK_ID_KEY] = ml[1] stats[DATE_NUM_KEY] = ml[0] t1 = getTime(int(ml[5].split(":")[0]),int(ml[5].split(":")[1])) t2 = getTime(int(ml[6].split(":")[0]),int(ml[6].split(":")[1])) stats[TIME_KEY] = t1 stats[DURATION_KEY] = getDuration(t1,t2) stats[LAT_KEY] = newPoint[LAT_KEY] stats[LON_KEY] = newPoint[LON_KEY] stats[RADIUS_KEY] = ml[4] stops[i].append(stats) stats = {} addRow = False # if all keys have been checked and no matches found then # if no, then it means its a differnet stop # add a new entry with an id. # add the truck id, datenum, stop time, and duration if addRow: stop_id += 1 pt = (stop_id, float(ml[2]),float(ml[3])) stats[TRUCK_ID_KEY] = ml[1] stats[DATE_NUM_KEY] = ml[0] t1 = getTime(int(ml[5].split(":")[0]),int(ml[5].split(":")[1])) t2 = getTime(int(ml[6].split(":")[0]),int(ml[6].split(":")[1])) stats[TIME_KEY] = t1 stats[DURATION_KEY] = getDuration(t1,t2) stats[LAT_KEY] = pt[1] stats[LON_KEY] = pt[2] stats[RADIUS_KEY] = ml[4] stopList.append(stats) stops[pt] = stopList stats = {} stopList = [] return stops
def getMetricCostBetweenStops(stopA, stopB): distance = kilDist(Point(stopA.lat, stopA.lon), Point(stopB.lat, stopB.lon)) return distance