Download Summary - All IT eBooks

Transcript
Chapter 6
A second table, endpoints, will hold the coordinates for the ends of the various
directed road segments. Each endpoint record will have the following fields:
• id: This is the unique ID for this endpoint
• endpoint: This is a Point geometry containing the coordinates for
this endpoint
We use a Point geometry here so that we can make
spatial queries against this table.
Finally, we are going to need a table that identifies which directed road segments
leave from a given endpoint. This table, endpoint_segments, will have the
following fields:
• id: This is the unique ID for this endpoint_segments record
• directed_segment_id: This is the record ID of a directed road segment
• endpoint_id: This is the record ID of the endpoint that this directed road
segment leaves from
These three tables will be used to store the network of directed road segments.
Let's modify our init_db.py program to create these three new tables. To do
this, add the following code to the end of this file, immediately before the call to
connection.commit():
cursor.execute("DROP TABLE IF EXISTS directed_segments")
cursor.execute("CREATE TABLE directed_segments (" +
"id SERIAL PRIMARY KEY," +
"road_segment_id INTEGER," +
"centerline GEOMETRY)")
cursor.execute("CREATE INDEX ON directed_segments USING
GIST(centerline)")
cursor.execute("DROP TABLE IF EXISTS endpoints")
cursor.execute("CREATE TABLE endpoints (" +
"id SERIAL PRIMARY KEY," +
"endpoint GEOMETRY)")
cursor.execute("CREATE INDEX ON endpoints USING GIST(endpoint)")
cursor.execute("DROP TABLE IF EXISTS endpoint_segments")
cursor.execute("CREATE TABLE endpoint_segments (" +
"id SERIAL PRIMARY KEY," +
"directed_segment_id INTEGER," +
[ 147 ]