Commit a5aed23a authored by dagal's avatar dagal

sql fixes

parent db723898
......@@ -30,8 +30,9 @@ class Stop():
# self.routes = set()
self.connections = set()
# self.freq = 0
# self.name = name
(self.x, self.y) = (lat, lng)
self.name = name
(self.lat, self.lng) = (lat, lng)
#(self.x, self.y) = (lat, lng)
def connect(self, stop):
stop.connections.add(self.id)
......@@ -87,7 +88,7 @@ class Network():
self.stops[stop.id] = stop
# self.stops_by_name[stop.name] = stop
(stop.x, stop.y, _, _) = utm.from_latlon(stop.x, stop.y)
(stop.x, stop.y, _, _) = utm.from_latlon(stop.lat, stop.lng)
for s in self.find_near_stops(stop, 100):
s.connect(stop)
......@@ -160,21 +161,19 @@ class Network():
key=lambda t: (t.route, t.direction, t.start_time, t.end_time)):
line = trip.get_line()
c = 1
if (line, trip.start_time) in tripsByDay[day]:
continue
if line in tripCounter:
c = tripCounter[line]
tripCounter[line] = c+1
tripCounter[line] += 1
else:
tripCounter[line] = 1
tripsByDay[day][(line, trip.start_time)] = c
tripsByDay[day][(line, trip.start_time)] = tripCounter[line]
if prefix:
mystr = "INSERT INTO journey (line_id, start_time) VALUES ('%s', to_timestamp(%d) at time zone 'UTC');\n" % (line, day*seconds_day + trip.start_time)
mystr = "INSERT INTO journey (line_id, journey_id, start_time) VALUES ('%s', %d, to_timestamp(%d) at time zone 'UTC');\n" % (line, tripCounter[line], day*seconds_day + trip.start_time)
fj.write(mystr.encode("utf-8"))
#print trip.get_line() + ": "
......@@ -246,17 +245,18 @@ def parse_gtfs(file_in, file_out, file_freqs = None, network = Network(), prefix
for stop_time in t.GetStopTimes():
s = sched.GetStop(stop_time.stop_id)
if prefix and s.stop_id not in network.stops:
mystr = "INSERT INTO stop (id, name, lat, lon) VALUES ('%s', '%s', %f, %f);\n" % (s.stop_id, s.stop_name, s.stop_lat, s.stop_lon)
fstops.write(mystr.encode("utf-8"))
stop = network.add_stop(Stop(s.stop_id, lat=s.stop_lat, lng=s.stop_lon))
stop = network.add_stop(Stop(s.stop_id, name=s.stop_name, lat=s.stop_lat, lng=s.stop_lon))
trip.add_stop(parse_time(stop_time.arrival_time), parse_time(stop_time.departure_time), stop)
network.add_trip(trip)
if prefix:
for i,stop_id in enumerate(sorted(network.stops.keys())):
s = network.stops[stop_id]
mystr = "INSERT INTO stop (id, ctr_id, name, lat, lon) VALUES ('%s', %d, '%s', %f, %f);\n" % (s.id, i+1, s.name, s.lat, s.lng)
fstops.write(mystr.encode("utf-8"))
#s.name = None
flines.close()
fstops.close()
......@@ -366,10 +366,13 @@ def is_better_stop(candidate_stop, next_stops):
def main(argv):
#n_traj = 0
#n_traj = 1
n_traj = 10000
#n_traj = 1000000
#prefix = None
prefix = "emt"
n_traj = 0
#n_traj = 10
#n_traj = 100000
#n_traj = 10000000
#change_probs = [0.50, 0.90, 0.95, 0.98, 1.0]
#change_probs = [0.98, 0.98, 0.99, 1.0]
switch_weights = [10, 10, 5, 1]
......@@ -377,12 +380,12 @@ def main(argv):
lengths = collections.Counter()
#print("BEGIN TRANSACTION;")
#network = parse_gtfs("madrid_emt.zip", "madrid_emt.dat", prefix="emt")
#network = parse_gtfs("madrid_emt.zip", "madrid_emt.dat", prefix=prefix)
#network = parse_gtfs("madrid_crtm.zip", "madrid_bus.dat", network=network)
#print("COMMIT;")
#sys.exit(0)
network = load_gtfs("madrid_emt.dat")
#tripsByStop = network.compute_trips_by_stop(prefix="emt")
#tripsByStop = network.compute_trips_by_stop(prefix=prefix)
tripsByStop = network.compute_trips_by_stop()
#sys.exit(0)
......@@ -403,7 +406,7 @@ def main(argv):
#print len(network.lines.keys())
#network.assign_freqs()
stops_dict = {key: value+1 for value, key in enumerate(sorted(network.stops.keys()))}
#tripsByDay = network.calculate_trips_by_day(stops_dict, days_cycle, prefix="emt")
#tripsByDay = network.calculate_trips_by_day(stops_dict, days_cycle, prefix=prefix)
tripsByDay = network.calculate_trips_by_day(stops_dict, days_cycle)
#sys.exit(0)
unused_stops = set(network.stops)
......@@ -501,12 +504,13 @@ def main(argv):
unused_stops.difference_update(trajectory)
sys.stdout.write(",".join(map(lambda (l,s,t): "%s:%s:%s" % (l,str(stops_dict[s]),str(t)), zip(lines, trajectory, times))))
sys.stdout.write("\n")
sys.stdout.write("\n")
i+=1
if unused_stops:
sys.stderr.write("\nWARNING: " + str(len(unused_stops)) + " Unused stops\n")
sys.stderr.write("\nSKIPPED: " + str(skip_bad) + "\n")
......
DROP TABLE IF EXISTS stop CASCADE;
CREATE TABLE stop
(
id varchar primary key,
ctr_id integer unique,
name varchar,
lat float,
lon float
);
DROP TABLE IF EXISTS line CASCADE;
CREATE TABLE line
(
id varchar primary key,
short_name varchar,
long_name varchar
);
DROP TABLE IF EXISTS line_stop CASCADE;
CREATE TABLE line_stop
(
line_id varchar references line (id) on delete cascade,
stop_id varchar references stop (id) on delete cascade,
seq integer,
primary key (line_id, stop_id)
);
DROP TABLE IF EXISTS journey CASCADE;
CREATE TABLE journey
(
line_id varchar references line (id) on delete cascade,
journey_id integer,
start_time timestamp without time zone,
primary key (line_id, journey_id)
);
--skipping stop_time
DROP TABLE IF EXISTS stage CASCADE;
CREATE TABLE stage
(
trip_id integer,
num_stage integer,
line_id varchar,
journey_id integer,
board_stop varchar,
alight_stop varchar,
primary key (trip_id, num_stage),
foreign key (line_id, journey_id) references journey on delete cascade,
foreign key (line_id, board_stop) references line_stop on delete cascade,
foreign key (line_id, alight_stop) references line_stop on delete cascade
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment