예제 #1
0
def get(data):
    if not data in coordsMemo:
        coordsX = []
        coordsY = []
        for row in data:
            trip = polylinemapper.decode(row["trippolyline"])
            delay = polylinemapper.decode(row["nextpolyline"])
            coordsX += [pair[0] for pair in trip]
            coordsX += [pair[0] for pair in delay]
            coordsY += [pair[1] for pair in trip]
            coordsY += [pair[1] for pair in delay]
        coordsMemo[data] = [coordsX, coordsY]
    return coordsMemo[data]
예제 #2
0
def unpack(data, totalCoordinates, socket):
    cX = totalCoordinates[0]
    cY = totalCoordinates[1]

    client = OSCClient()
    client.connect(("localhost", socket))

    """ Prepare and send OSC bundles to Max from a list of tuples of coordinates. """
    def bundlePolyline(coordinates, speed, polylineType, passengers, coordsX, coordsY):
        for pair in coordinates:

            # create an OSC bundle:
            bundle = OSCBundle()

            # append polylineType: "trip" or "delay" (data for in between current and next trip)
            bundle.append({'addr': "/curr", 'args': [polylineType]})

            # append min/max longX and latY to bundle:
            bundle.append({'addr': "/minX", 'args': [min(coordsX)]})
            bundle.append({'addr': "/maxX", 'args': [max(coordsX)]})
            bundle.append({'addr': "/minY", 'args': [min(coordsY)]})
            bundle.append({'addr': "/maxY", 'args': [max(coordsY)]})

            # append longX and latY to bundle
            bundle.append({'addr': "/longX", 'args': [pair[0]]})
            bundle.append({'addr': "/latY", 'args': [pair[1]]})
            
            # append start/end longX and latY of coordinates list
            xVals = [coords[0] for coords in coordinates]
            bundle.append({'addr': "/startX", 'args': [xVals[0]]})
            bundle.append({'addr': "/endX", 'args': [xVals[len(xVals) - 1]]})
            yVals = [coords[1] for coords in coordinates]
            bundle.append({'addr': "/startY", 'args': [yVals[0]]})
            bundle.append({'addr': "/endY", 'args': [yVals[len(yVals) - 1]]})

            # append passengers
            bundle.append({'addr': "/passengers", 'args': [passengers]})

            # send bundle to Max:
            client.send(bundle)

            # delay time to even out polyline steps
            time.sleep(speed)

    """ Read the next line in the data file. """
    for row in data:

        """ Parse and set time stamps in minutes (i.e. 4:02 == 242). """
        pickup = row["pickuptime"].split(" ")[1]
        pickup = pickup.split(":")
        pickup = (int(pickup[0])*60) + int(pickup[1])

        dropoff = row["dropofftime"].split(" ")[1]
        dropoff = dropoff.split(":")
        dropoff = (int(dropoff[0])*60) + int(dropoff[1])

        nextPickup = row["nextpickuptime"].split(" ")[1]
        nextPickup = nextPickup.split(":")
        nextPickup = (int(nextPickup[0])*60) + int(nextPickup[1])  

        """ Decode trippolyline. """
        latLongList = polylinemapper.decode(row["trippolyline"])
        passengers = row["passengers"]
        latLongSpeed = round((dropoff - pickup) / (2*len(latLongList)), 10)  # translate 1 minute in RT == 0.5 seconds
        bundlePolyline(latLongList, latLongSpeed, "trip", passengers, cX, cY)

        """ Decode nextpolyline. """
        nextLatLong = polylinemapper.decode(row["nextpolyline"])
        passengers = 0
        nextSpeed = round((nextPickup - dropoff) / (2*len(nextLatLong)), 10)  # translate 1 minute in RT == 0.5 seconds
        bundlePolyline(nextLatLong, nextSpeed, "delay", passengers, cX, cY)

    client.close()