File: create_grid_3857.sql

package info (click to toggle)
osmcoastline 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 812 kB
  • sloc: cpp: 2,834; sh: 659; sql: 199; makefile: 11
file content (51 lines) | stat: -rw-r--r-- 2,005 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
------------------------------------------------------------------------------
--
-- Create grid for splitting up Mercator polygons.
--
------------------------------------------------------------------------------

-- speed up spatialite processing
PRAGMA journal_mode = OFF;
PRAGMA synchronous = OFF;
PRAGMA temp_store = MEMORY;
PRAGMA cache_size = 5000000;

-- make sure we have the SRID in the spatial_ref_sys table
SELECT InsertEpsgSrid(3857) WHERE (SELECT count(*) FROM spatial_ref_sys WHERE srid = 3857) = 0;

-- clean up if this is left over from previous run
SELECT DiscardGeometryColumn('grid', 'geometry') FROM geometry_columns WHERE f_table_name='grid';
DROP TABLE IF EXISTS  grid;

CREATE TABLE tmp_square_grid (id INTEGER PRIMARY KEY AUTOINCREMENT);
SELECT AddGeometryColumn('tmp_square_grid', 'geometry', 3857, 'MULTIPOLYGON', 'XY');

INSERT INTO tmp_square_grid (geometry)
    SELECT ST_SquareGrid(
                -- slightly smaller than the full extent so we don't get an
                -- extra set of tiles at the top and right boundary
                BuildMbr(-20037508.34, -20037508.34, 20037508.0, 20037508.0, 3857),
                -- split into 64x64 tiles
                2*20037508.34/64);

.elemgeo tmp_square_grid geometry tmp_square_grid_elem id_new id;

CREATE TABLE grid (OGC_FID INTEGER PRIMARY KEY AUTOINCREMENT);
SELECT AddGeometryColumn('grid', 'geometry', 3857, 'POLYGON', 'XY');

INSERT INTO grid (geometry)
    SELECT ST_Intersection(
                -- 50 units overlap on each side of the tile
                ST_Expand(geometry, 50.0),
                -- make sure there are no overlaps at the sides
                BuildMbr(-20037508.34, -20037508.34, 20037508.34, 20037508.34, 3857))
        FROM tmp_square_grid_elem;

SELECT CreateSpatialIndex('grid', 'geometry');

SELECT DiscardGeometryColumn('tmp_square_grid', 'geometry');
DROP TABLE tmp_square_grid;

SELECT DiscardGeometryColumn('tmp_square_grid_elem', 'geometry');
DROP TABLE tmp_square_grid_elem;