Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/gtfs_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ create table gtfs_trips (
block_id text,
shape_id text,
trip_short_name text,
wheelchair_accessible text,
-- unofficial features
trip_type text
);
Expand Down
15 changes: 8 additions & 7 deletions src/gtfs_tables_makespatial.sql
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
-- Add spatial support for PostGIS databases only

-- Drop everything first
DROP TABLE gtfs_shape_geoms CASCADE;

DROP TABLE IF EXISTS gtfs_shape_geoms CASCADE;
CREATE EXTENSION IF NOT EXISTS postgis;
BEGIN;
-- Add the_geom column to the gtfs_stops table - a 2D point geometry
SELECT AddGeometryColumn('gtfs_stops', 'the_geom', 4326, 'POINT', 2);
--SELECT AddGeometryColumn('gtfs_stops', 'the_geom', 4326, 'POINT', 2);
ALTER TABLE gtfs_stops ADD COLUMN the_geom geometry(POINT,4326);

-- Update the the_geom column
UPDATE gtfs_stops SET the_geom = ST_SetSRID(ST_MakePoint(stop_lon, stop_lat), 4326);

-- Create spatial index
CREATE INDEX "gtfs_stops_the_geom_gist" ON "gtfs_stops" using gist ("the_geom" gist_geometry_ops);
--CREATE INDEX "gtfs_stops_the_geom_gist" ON "gtfs_stops" using gist ("the_geom" gist_geometry_ops);

-- Create new table to store the shape geometries
CREATE TABLE gtfs_shape_geoms (
shape_id text
);

-- Add the_geom column to the gtfs_shape_geoms table - a 2D linestring geometry
SELECT AddGeometryColumn('gtfs_shape_geoms', 'the_geom', 4326, 'LINESTRING', 2);

--SELECT AddGeometryColumn('gtfs_shape_geoms', 'the_geom', 4326, 'LINESTRING', 2);
ALTER TABLE gtfs_shape_geoms ADD COLUMN the_geom geometry(LINESTRING,4326);
-- Populate gtfs_shape_geoms
INSERT INTO gtfs_shape_geoms
SELECT shape.shape_id, ST_SetSRID(ST_MakeLine(shape.the_geom), 4326) As new_geom
Expand All @@ -32,6 +33,6 @@ SELECT shape.shape_id, ST_SetSRID(ST_MakeLine(shape.the_geom), 4326) As new_geom
GROUP BY shape.shape_id;

-- Create spatial index
CREATE INDEX "gtfs_shape_geoms_the_geom_gist" ON "gtfs_shape_geoms" using gist ("the_geom" gist_geometry_ops);
--CREATE INDEX "gtfs_shape_geoms_the_geom_gist" ON "gtfs_shape_geoms" using gist ("the_geom" gist_geometry_ops);

COMMIT;
14 changes: 7 additions & 7 deletions src/import_gtfs_to_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def handleVals(self,row,cols):
class StopTimesHandler(SpecialHandler):
@staticmethod
def timeToSeconds(text):
h,m,s = map(int,text.split(":"))
h,m,s = list(map(int,text.split(":")))
return h*60*60 + m*60 + s

@staticmethod
Expand Down Expand Up @@ -144,7 +144,7 @@ def import_file(fname, tablename, handler, COPY=True):
handler = SpecialHandler()

reader = csv.reader(f,dialect=csv.excel);
header = handler.handleCols([c.strip() for c in reader.next()]);
header = handler.handleCols([c.strip() for c in next(reader)]);
cols = ",".join(header);

defaultVal = 'NULL';
Expand Down Expand Up @@ -199,8 +199,8 @@ def import_file(fname, tablename, handler, COPY=True):
handlers['frequencies'] = FrequenciesHandler();

if len(sys.argv) not in (2,3):
print "Usage: %s gtfs_data_dir [nocopy]" % sys.argv[0]
print " If nocopy is present, then uses INSERT instead of COPY."
print("Usage: %s gtfs_data_dir [nocopy]" % sys.argv[0])
print(" If nocopy is present, then uses INSERT instead of COPY.")
sys.exit()

dirname = sys.argv[1]
Expand All @@ -209,11 +209,11 @@ def import_file(fname, tablename, handler, COPY=True):

useCopy = not ("nocopy" in sys.argv[2:])

print "begin;"
print("begin;")

for fname in fnames:
for statement in import_file(dirname+"/"+fname+".txt","gtfs_"+fname,
handlers[fname],useCopy):
print statement;
print(statement);

print "commit;"
print("commit;")