The data is a subset of the table of cities used by the Geocoder API.
First import the base table: https://drive.google.com/file/d/0B6sNSm2Xp0hnWUY1X0FqNUZxOGc/view?usp=sharing
Then create some overviews. We'll assume the detail of the base table is good for zoom level 9, so the base table will be used for all levels greater than or equal to 9. A simplistic approach will be used: for each level less than 9 a fourth of the points will be randomly sampled to maintain the same point density on screen. We'll create overviews only for 1 zoom level out of 2. (heads up: the overview tables owner should be the database user)
CREATE TABLE cities_subset_ov8 AS
SELECT * FROM cities_subset WHERE random()<0.25;
CREATE TABLE cities_subset_ov6 AS
SELECT * FROM cities_subset_ov8 WHERE random()<0.25*0.25;
CREATE TABLE cities_subset_ov4 AS
SELECT * FROM cities_subset_ov6 WHERE random()<0.25*0.25;
CREATE TABLE cities_subset_ov2 AS
SELECT * FROM cities_subset_ov4 WHERE random()<0.25*0.25;
We will use:
- cities_subset for zoom level >= 9
- cities_subset_ov8 for 9 > zoom level >= 7
- cities_subset_ov6 for 7 > zoom level >= 5
- cities_subset_ov4 for 5 > zoom level >= 3
- cities_subset_ov2 for 3 > zoom level
Now we can create a view to combine all overviews:
CREATE VIEW cities_subset_ov AS
SELECT * ,9 AS min_zoom FROM cities_subset
UNION ALL
SELECT *, 7 AS min_zoom FROM cities_subset_ov8
UNION ALL
SELECT *, 5 AS min_zoom FROM cities_subset_ov6
UNION ALL
SELECT *, 3 AS min_zoom FROM cities_subset_ov4
UNION ALL
SELECT *, 0 AS min_zoom FROM cities_subset_ov2;
Now we need some metadata about the overviews. We'll add a table ov_tables
to the user database. We'll do something very simple at the moment and not bother about schemas, etc. (TODO: should we store some info in the metadata database?)
CREATE TABLE ov_tables(base_table text, overview_table text, min_zoom integer);
INSERT INTO ov_tables(base_table, overview_table, min_zoom)
VALUES ('cities_subset', 'cities_subset', 9);
INSERT INTO ov_tables(base_table, overview_table, min_zoom)
VALUES ('cities_subset', 'cities_subset_ov8', 7);
INSERT INTO ov_tables(base_table, overview_table, min_zoom)
VALUES ('cities_subset', 'cities_subset_ov6', 5);
INSERT INTO ov_tables(base_table, overview_table, min_zoom)
VALUES ('cities_subset', 'cities_subset_ov4', 3);
INSERT INTO ov_tables(base_table, overview_table, min_zoom)
VALUES ('cities_subset', 'cities_subset_ov2', 0);
Using materialized views instead of tables will have any advantage?