Esempio n. 1
0
def get_trip_attributes(trip_id):
	"""Return the attributes of a stored trip necessary 
		for the construction of a new trip object.
		This now includes the vehicle report times and positions."""
	c = cursor()
	c.execute(
		"""
			SELECT
				block_id,
				direction_id,
				route_id,
				vehicle_id,
				(ST_DumpPoints(ST_Transform(orig_geom,4326))).geom,
				unnest(times)
			FROM {trips}
			WHERE trip_id = %(trip_id)s
		""".format(**conf['db']['tables']),
		{ 'trip_id':trip_id }
	)
	vehicle_records = []
	for (bid, did, rid, vid, WGS84geom, epoch_time ) in c:
		# only consider the last three variables, as the rest are 
		# the same for every record
		WGS84geom = loadWKB(WGS84geom,hex=True)
		# Vehicle( epoch_time, longitude, latitude)
		vehicle_records.append( Vehicle( epoch_time, WGS84geom.x, WGS84geom.y ) )
	result = {
		'block_id': bid,
		'direction_id': did,
		'route_id': rid,
		'vehicle_id': vid,
		'points': vehicle_records
	}
	return result
Esempio n. 2
0
def get_trip_attributes(trip_id):
    """Return the attributes of a stored trip necessary 
		for the construction of a new trip object.
		This now includes the vehicle report times and positions."""
    c = cursor()
    c.execute(
        """
			SELECT
				block_id,
				direction_id,
				route_id,
				vehicle_id,
				(ST_DumpPoints(ST_Transform(orig_geom,4326))).geom,
				unnest(times)
			FROM {trips}
			WHERE trip_id = %(trip_id)s
		""".format(**conf['db']['tables']), {'trip_id': trip_id})
    vehicle_records = []
    for (bid, did, rid, vid, WGS84geom, epoch_time) in c:
        # only consider the last three variables, as the rest are
        # the same for every record
        WGS84geom = loadWKB(WGS84geom, hex=True)
        # Vehicle( epoch_time, longitude, latitude)
        vehicle_records.append(Vehicle(epoch_time, WGS84geom.x, WGS84geom.y))
    result = {
        'block_id': bid,
        'direction_id': did,
        'route_id': rid,
        'vehicle_id': vid,
        'points': vehicle_records
    }
    return result
Esempio n. 3
0
	def __init__(self,db_record):
		self.db_uid        = db_record.uid
		self.osm_id        = db_record.way_id
		self.name          = db_record.name
		self.from_id       = db_record.node_1
		self.to_id         = db_record.node_2
		self.forward_count = db_record.f
		self.reverse_count = db_record.r
		self.geometry      = loadWKB( db_record.geom, hex=True )
Esempio n. 4
0
def get_trip_attributes(trip_id):
	"""Return the attributes of a stored trip necessary 
		for the construction of a new trip object.
		This now includes the vehicle report times and positions."""
	c = cursor()
	c.execute(
		"""
			SELECT
				block_id,
				direction_id,
				route_id,
				vehicle_id,
				(ST_DumpPoints(orig_geom)).geom,
				(ST_DumpPoints(ST_Transform(orig_geom,4326))).geom,
				unnest(times)
			FROM {trips}
			WHERE trip_id = %(trip_id)s
		""".format(**conf['db']['tables']),
		{ 'trip_id':trip_id }
	)
	points = []
	for (bid, did, rid, vid, geom, wgs84geom, time ) in c:
		# only consider the last three variables, as the rest are 
		# the same for every record
		wgs84geom = loadWKB(wgs84geom,hex=True)
		points.append({
			'geom':loadWKB(geom,hex=True),
			'time':time,
			'lat': wgs84geom.y,
			'lon': wgs84geom.x
		})
	result = {
		'block_id':bid,
		'direction_id':did,
		'route_id':rid,
		'vehicle_id':vid,
		'points':points
	}
	return result
Esempio n. 5
0
def get_route_geom(direction_id, trip_time):
    """Get the geometry of a direction or return None. This is meant to be a 
		backup in case map-matching is going badly. Direction geometries must be 
		supplied manually. If all goes well this returns a shapely geometry in
		the local projection. Else, None."""
    c = cursor()
    # get the uid of the relevant direction entry
    uid = get_direction_uid(direction_id, trip_time)
    if not uid: return None
    # now find the geometry
    c.execute(
        """
			SELECT 
				route_geom
			FROM {directions} 
			WHERE uid = %(uid)s;
		""".format(**conf['db']['tables']), {'uid': uid})
    geom, = c.fetchone()
    if geom: return loadWKB(geom, hex=True)
    else: return None
Esempio n. 6
0
def get_route_geom(direction_id, trip_time):
	"""Get the geometry of a direction or return None. This is meant to be a 
		backup in case map-matching is going badly. Direction geometries must be 
		supplied manually. If all goes well this returns a shapely geometry in
		the local projection. Else, None."""
	c = cursor()
	# get the uid of the relevant direction entry
	uid = get_direction_uid(direction_id,trip_time)
	if not uid: return None
	# now find the geometry
	c.execute(
		"""
			SELECT 
				route_geom
			FROM {directions} 
			WHERE uid = %(uid)s;
		""".format(**conf['db']['tables']),
		{ 'uid':uid }
	)
	geom, = c.fetchone()
	if geom: return loadWKB(geom,hex=True)
	else: return None
Esempio n. 7
0
 def __init__(self, stop_id, projected_geom_hex):
     # stop_id is the int UID of the stop, which can be associated with the
     # given stop_id through the stops table
     self.id = stop_id
     self.geom = loadWKB(projected_geom_hex, hex=True)
Esempio n. 8
0
	def __init__( self, stop_id, projected_geom_hex ):
		# stop_id is the int UID of the stop, which can be associated with the 
		# given stop_id through the stops table
		self.id = stop_id
		self.geom = loadWKB( projected_geom_hex, hex=True )
Esempio n. 9
0
    def match(self):
        """Match the trip to the road network, and do all the
			things that follow therefrom."""
        match = map_api.match(self.vehicles)
        if not match.is_useable:
            return db.ignore_trip(self.trip_id, 'match problem')
        self.match_confidence = match.confidence
        # store the trip geometry
        self.match_geom = match.geometry()
        # and reproject it
        self.match_geom = reproject(conf['projection'], self.match_geom)
        # simplify slightly for speed (2 meter simplification)
        self.match_geom = self.match_geom.simplify(2)
        # if the multi actually just had one line, this simplifies to a
        # linestring, which can cause problems down the road
        if self.match_geom.geom_type == 'LineString':
            self.match_geom = MultiLineString([self.match_geom])
        # store the match info and geom in the DB
        db.add_trip_match(self.trip_id, self.match_confidence,
                          dumpWKB(self.match_geom, hex=True))
        # drop vehicles that did not contribute to the match
        vehicles_used = match.vehicles_used()
        for i in reversed(range(0, len(self.vehicles))):
            if not vehicles_used[i]: del self.vehicles[i]
        # get distances of each vehicle along the match geom
        for vehicle, cum_dist in zip(self.vehicles, match.cum_distances()):
            vehicle['cum_dist'] = cum_dist
        # However, because we've simplified the line, the distances will be slightly off
        # and need correcting
        adjust_factor = self.match_geom.length / self.vehicles[-1]['cum_dist']
        for v in self.vehicles:
            v['cum_dist'] = v['cum_dist'] * adjust_factor
        # get the stops as a list of objects
        # with keys {'id':stop_id,'g':geom}
        self.stops = db.get_stops(self.direction_id, self.last_seen)
        # process the geoms
        for stop in self.stops:
            stop['geom'] = loadWKB(stop['geom'], hex=True)
        # now match stops to the trip geometry, 750m at a time
        path = self.match_geom
        traversed = 0
        # while there is more than 750m of path remaining
        while path.length > 0:
            subpath, path = cut(path, 750)
            # check for nearby stops
            for stop in self.stops:
                # if the stop is close enough
                stop_dist = subpath.distance(stop['geom'])
                if stop_dist <= conf['stop_dist']:
                    # measure how far it is along the trip
                    measure = traversed + subpath.project(stop['geom'])
                    # add it to a list of possible stop times
                    self.add_arrival(stop['id'], measure, stop_dist)
            # note what we have already traversed
            traversed += 750
        # sort stops by arrival time
        self.timepoints = sorted(self.timepoints, key=lambda k: k['time'])
        # there is more than one stop, right?
        if len(self.timepoints) > 1:
            # store the stop times
            db.store_timepoints(self.trip_id, self.timepoints)
            # Now set the service_id, which is the (local) DAY equivalent of
            # the unix epoch, which is centered on Greenwich.
            # (The service_id is distinct to a day in the local timezone)
            # First, shift the second_based epoch to local time
            tlocal = self.timepoints[0]['time'] + conf['timezone'] * 3600
            # then find the "epoch day"
            service_id = math.floor(tlocal / (24 * 3600))
            # and store it in the DB
            db.set_service_id(self.trip_id, service_id)
        else:
            db.ignore_trip(self.trip_id, 'one or fewer timepoints')
        return
cursor = conn.cursor()

print 'getting all the streets'
cursor.execute("""
	SELECT 
		-- local geometry in meters
		ST_Transform(way,32616) AS geom
	FROM gta_line
	WHERE 
		highway IS NOT NULL AND 
		highway NOT IN (
			'cycleway','service','footway','pedestrian','path','steps',
			'motorway','motorway_link','trunk','trunk_link'
		)
""")
print 'starting to generate points'

# write the output
outfile = open('street-points.csv', 'w')
outfile.write('x,y\n')

for (line_hex, ) in cursor:
    line = loadWKB(line_hex, hex=True)
    # for every 10m increment, there is a chance a point will be
    # placed somewhere on this line
    for i in range(0, int(math.ceil(line.length)), 10):
        if random.random() < 0.2:
            point = line.interpolate(random.random() * line.length)
            outfile.write(str(point.x) + ',' + str(point.y) + '\n')
outfile.close()