1 | -- Drop all primary keys and indexes to improve load speed.
|
---|
2 | ALTER TABLE nodes DROP CONSTRAINT pk_nodes;
|
---|
3 | ALTER TABLE ways DROP CONSTRAINT pk_ways;
|
---|
4 | ALTER TABLE way_nodes DROP CONSTRAINT pk_way_nodes;
|
---|
5 | ALTER TABLE relations DROP CONSTRAINT pk_relations;
|
---|
6 | ALTER TABLE relation_members DROP CONSTRAINT pk_relation_members;
|
---|
7 | DROP INDEX idx_node_tags_node_id;
|
---|
8 | DROP INDEX idx_nodes_geom;
|
---|
9 | DROP INDEX idx_way_tags_way_id;
|
---|
10 | DROP INDEX idx_way_nodes_node_id;
|
---|
11 | DROP INDEX idx_relation_tags_relation_id;
|
---|
12 | DROP INDEX idx_ways_bbox;
|
---|
13 | DROP INDEX idx_ways_linestring;
|
---|
14 |
|
---|
15 | -- Comment these out if the COPY files include bbox or linestring column values.
|
---|
16 | SELECT DropGeometryColumn('ways', 'bbox');
|
---|
17 | SELECT DropGeometryColumn('ways', 'linestring');
|
---|
18 |
|
---|
19 | -- Import the table data from the data files using the fast COPY method.
|
---|
20 | \copy users FROM 'users.txt'
|
---|
21 | \copy nodes FROM 'nodes.txt'
|
---|
22 | \copy node_tags FROM 'node_tags.txt'
|
---|
23 | \copy ways FROM 'ways.txt'
|
---|
24 | \copy way_tags FROM 'way_tags.txt'
|
---|
25 | \copy way_nodes FROM 'way_nodes.txt'
|
---|
26 | \copy relations FROM 'relations.txt'
|
---|
27 | \copy relation_tags FROM 'relation_tags.txt'
|
---|
28 | \copy relation_members FROM 'relation_members.txt'
|
---|
29 |
|
---|
30 | -- Add the primary keys and indexes back again (except the way bbox index).
|
---|
31 | ALTER TABLE ONLY nodes ADD CONSTRAINT pk_nodes PRIMARY KEY (id);
|
---|
32 | ALTER TABLE ONLY ways ADD CONSTRAINT pk_ways PRIMARY KEY (id);
|
---|
33 | ALTER TABLE ONLY way_nodes ADD CONSTRAINT pk_way_nodes PRIMARY KEY (way_id, sequence_id);
|
---|
34 | ALTER TABLE ONLY relations ADD CONSTRAINT pk_relations PRIMARY KEY (id);
|
---|
35 | ALTER TABLE ONLY relation_members ADD CONSTRAINT pk_relation_members PRIMARY KEY (relation_id, sequence_id);
|
---|
36 | CREATE INDEX idx_node_tags_node_id ON node_tags USING btree (node_id);
|
---|
37 | CREATE INDEX idx_nodes_geom ON nodes USING gist (geom);
|
---|
38 | CREATE INDEX idx_way_tags_way_id ON way_tags USING btree (way_id);
|
---|
39 | CREATE INDEX idx_way_nodes_node_id ON way_nodes USING btree (node_id);
|
---|
40 | CREATE INDEX idx_relation_tags_relation_id ON relation_tags USING btree (relation_id);
|
---|
41 |
|
---|
42 | -- Comment these out if the COPY files include bbox or linestring column values.
|
---|
43 | SELECT AddGeometryColumn('ways', 'bbox', 4326, 'GEOMETRY', 2);
|
---|
44 | SELECT AddGeometryColumn('ways', 'linestring', 4326, 'GEOMETRY', 2);
|
---|
45 |
|
---|
46 | -- Comment these out if the COPY files include bbox or linestring column values.
|
---|
47 | -- Update the bbox column of the way table.
|
---|
48 | UPDATE ways SET bbox = (
|
---|
49 | SELECT Envelope(Collect(geom))
|
---|
50 | FROM nodes JOIN way_nodes ON way_nodes.node_id = nodes.id
|
---|
51 | WHERE way_nodes.way_id = ways.id
|
---|
52 | );
|
---|
53 | -- Update the linestring column of the way table.
|
---|
54 | UPDATE ways w SET linestring = (
|
---|
55 | SELECT ST_MakeLine(c.geom) AS way_line FROM (
|
---|
56 | SELECT n.geom AS geom
|
---|
57 | FROM nodes n INNER JOIN way_nodes wn ON n.id = wn.node_id
|
---|
58 | WHERE (wn.way_id = w.id) ORDER BY wn.sequence_id
|
---|
59 | ) c
|
---|
60 | )
|
---|
61 |
|
---|
62 | -- Index the way bounding box column.
|
---|
63 | CREATE INDEX idx_ways_bbox ON ways USING gist (bbox);
|
---|
64 | CREATE INDEX idx_ways_linestring ON ways USING gist (linestring);
|
---|
65 |
|
---|
66 | -- Perform database maintenance due to large database changes.
|
---|
67 | VACUUM ANALYZE;
|
---|