Skip to content

Commit

Permalink
Added MTC data loader; Experimentally applied Dijkstra on routesegments
Browse files Browse the repository at this point in the history
  • Loading branch information
justjkk committed May 14, 2011
1 parent 73c2e16 commit f5d5c55
Show file tree
Hide file tree
Showing 10 changed files with 22,593 additions and 5 deletions.
20 changes: 20 additions & 0 deletions mtc_dataloader/gen_routesegments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python

import csv

routesegments = []
with open('routestages.csv', 'r') as f:
reader = csv.DictReader(f, ['route_name', 'stage_name', 'sequence'])
cur_route = None
prev_stage = None
for row in reader:
if cur_route == None or cur_route != row["route_name"]:
cur_route = row["route_name"]
prev_stage = row["stage_name"]
continue
routesegments.append((cur_route, prev_stage, row["stage_name"]))
prev_stage = row["stage_name"]

with open('routesegments.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(routesegments)
60 changes: 60 additions & 0 deletions mtc_dataloader/loader.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
drop table if exists mtc_routesegments;
drop table if exists mtc_routes;
drop table if exists mtc_stages;

create table mtc_stages
(
id serial primary key,
name varchar(255) not null unique,
latitude double precision,
longitude double precision
);

create table mtc_routes
(
id serial primary key,
name varchar(255) not null unique,
source_name varchar(255) not null,
target_name varchar(255) not null,
source integer references mtc_stages,
target integer references mtc_stages
);

create table mtc_routesegments
(
id serial primary key,
route_name varchar(255) not null,
source_name varchar(255) not null,
target_name varchar(255) not null,
route integer references mtc_routes,
source integer references mtc_stages,
target integer references mtc_stages
);

-- Load data from file
\copy mtc_stages(name,latitude,longitude) from 'stages.csv' with csv
\copy mtc_routes(name,source_name,target_name) from 'routes.csv' with csv
\copy mtc_routesegments(route_name, source_name, target_name) from 'routesegments.csv' with csv

-- Mapping ids between stages, routes and routesegments
update mtc_routes set
source = (select id from mtc_stages where name = source_name),
target = (select id from mtc_stages where name = target_name);

update mtc_routesegments set
route = (select id from mtc_routes where name = route_name),
source = (select id from mtc_stages where name = source_name),
target = (select id from mtc_stages where name = target_name);

-- Adding GIS column to mtc_stages
select AddGeometryColumn('mtc_stages', 'the_geom', 4326, 'POINT', 2);
update mtc_stages set the_geom = ST_GeomFromText('POINT(' || longitude || ' ' || latitude || ')', 4326);

-- Adding GIS column to mtc_routesegments
select AddGeometryColumn('mtc_routesegments', 'the_geom', 4326, 'LINESTRING', 2);

update mtc_routesegments mrsu set the_geom = ( select ST_MakeLine(s.the_geom, t.the_geom) from mtc_stages s join mtc_stages t on s.id = source and t.id = target);

-- Adding GIS column to mtc_routes
select AddGeometryColumn('mtc_routes', 'the_geom', 4326, 'MULTILINESTRING', 2);
update mtc_routes mr set the_geom = ( select ST_Union(mrs.the_geom) from mtc_routesegments mrs where mrs.route = mr.id);
Loading

0 comments on commit f5d5c55

Please sign in to comment.