-
Notifications
You must be signed in to change notification settings - Fork 11
/
sqlite_to_spatialite.sql
459 lines (412 loc) · 19.2 KB
/
sqlite_to_spatialite.sql
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
-- PRAGMA foreign_keys=ON;
PRAGMA synchronous=OFF;
-- PRAGMA journal_mode=MEMORY;
-- PRAGMA default_cache_size=10000;
-- PRAGMA locking_mode=EXCLUSIVE;
-- The old way of loading spatialite
-- SELECT load_extension('libspatialite.so');
-- The new way
-- See https://www.gaia-gis.it/fossil/libspatialite/wiki?name=mod_spatialite
SELECT load_extension('mod_spatialite');
-- 2018-06-08 added the .so extension because module stopped loading
-- I'm leaving the original line in as well
SELECT load_extension('mod_spatialite.so');
SELECT InitSpatialMetadata(1);
/*
--#UPDATE OBSTACLE_OBSTACLE SET geometry = SetSrid(geometry, 4326);
--#SELECT RecoverGeometryColumn('OBSTACLE_OBSTACLE','geometry',4326,'POINT','XY');
--#
--# UPDATE APT_RWY SET geometry = SetSrid(geometry, 4326);
--# SELECT RecoverGeometryColumn('APT_RWY','geometry',4326,'LINESTRING');
--# SELECT ASTEXT(geometry) from APT_RWY;
*/
--#Obstacles
SELECT AddGeometryColumn( 'OBSTACLE_OBSTACLE' , 'obstacleGeom', 4326, 'POINT', 'XY');
SELECT CreateSpatialIndex( 'OBSTACLE_OBSTACLE' , 'obstacleGeom' );
UPDATE OBSTACLE_OBSTACLE
SET obstacleGeom = MakePoint(
CAST (obstacle_longitude AS DOUBLE),
CAST (obstacle_latitude AS DOUBLE),
4326);
--AIR ROUTE TRAFFIC CONTROL CENTER FACILITIES AND COMMUNICATIONS (AFF)
SELECT AddGeometryColumn( 'AFF_AFF1' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'AFF_AFF1' , 'geometry' );
UPDATE AFF_AFF1
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
SELECT AddGeometryColumn( 'AFF_AFF3' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'AFF_AFF3' , 'geometry' );
UPDATE AFF_AFF3
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--Airports (APT)
--#The airport reference points
SELECT AddGeometryColumn( 'APT_APT' , 'referenceGeom' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'APT_APT' , 'referenceGeom' );
UPDATE APT_APT
SET referenceGeom = MakePoint(
CAST (apt_longitude AS DOUBLE),
CAST (apt_latitude AS DOUBLE),
4326);
--#Runway lines
SELECT AddGeometryColumn( 'APT_RWY' , 'runwayGeom', 4326, 'LINESTRING');
SELECT CreateSpatialIndex( 'APT_RWY' , 'runwayGeom' );
UPDATE APT_RWY
SET runwayGeom = LineFromText('LINESTRING('||base_longitude||' '||base_latitude||','||reciprocal_longitude||' '||reciprocal_latitude||')', 4326)
WHERE
base_longitude IS NOT '0'
AND
base_latitude IS NOT '0'
AND
reciprocal_longitude IS NOT '0'
AND
reciprocal_latitude IS NOT '0';
--#Base end points
SELECT AddGeometryColumn( 'APT_RWY' , 'baseGeom', 4326, 'POINT');
SELECT CreateSpatialIndex( 'APT_RWY' , 'baseGeom' );
UPDATE APT_RWY
SET baseGeom = MakePoint(
CAST (base_longitude AS DOUBLE),
CAST (base_latitude AS DOUBLE),
4326);
--#Base displaced threshold
SELECT AddGeometryColumn( 'APT_RWY' , 'baseDisplacedThresholdGeom' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'APT_RWY' , 'baseDisplacedThresholdGeom' );
UPDATE APT_RWY
set baseDisplacedThresholdGeom = MakePoint(
CAST (base_displaced_threshold_longitude AS DOUBLE),
CAST (base_displaced_threshold_latitude AS DOUBLE),
4326);
--#Reciprocal end points
SELECT AddGeometryColumn( 'APT_RWY' , 'reciprocalGeom' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'APT_RWY' , 'reciprocalGeom' );
UPDATE APT_RWY
SET reciprocalGeom = MakePoint(
CAST (reciprocal_longitude AS DOUBLE),
CAST (reciprocal_latitude AS DOUBLE),
4326);
--#Reciprocal displaced threshold
SELECT AddGeometryColumn( 'APT_RWY' , 'reciprocalDisplacedThresholdGeom' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'APT_RWY' , 'reciprocalDisplacedThresholdGeom' );
UPDATE APT_RWY
SET reciprocalDisplacedThresholdGeom = MakePoint(
CAST (reciprocal_displaced_threshold_longitude AS DOUBLE),
CAST (reciprocal_displaced_threshold_latitude AS DOUBLE),
4326);
--ARB
SELECT AddGeometryColumn( 'ARB_ARB' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'ARB_ARB' , 'geometry' );
UPDATE ARB_ARB
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--ATS
--ATS2
SELECT AddGeometryColumn( 'ATS_ATS2' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'ATS_ATS2' , 'geometry' );
UPDATE ATS_ATS2
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
-- --ATS3
-- There aren't any of these yet so I'm commenting them out
-- SELECT AddGeometryColumn( 'ATS_ATS3' , 'geometry' , 4326, 'POINT');
-- SELECT CreateSpatialIndex( 'ATS_ATS3' , 'geometry' );
-- UPDATE ATS_ATS3
-- SET geometry = MakePoint(
-- CAST (longitude AS DOUBLE),
-- CAST (latitude AS DOUBLE),
-- 4326);
--AWOS Stations
SELECT AddGeometryColumn( 'AWOS_AWOS1' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'AWOS_AWOS1' , 'geometry' );
UPDATE AWOS_AWOS1
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--AWY
--AWY2
SELECT AddGeometryColumn( 'AWY_AWY2' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'AWY_AWY2' , 'geometry' );
UPDATE AWY_AWY2
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--AWY3
SELECT AddGeometryColumn( 'AWY_AWY3' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'AWY_AWY3' , 'geometry' );
UPDATE AWY_AWY3
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--COM
SELECT AddGeometryColumn( 'COM_COM' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'COM_COM' , 'geometry' );
UPDATE COM_COM
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--Fixes (FIX)
SELECT AddGeometryColumn( 'FIX_FIX1' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'FIX_FIX1' , 'geometry' );
UPDATE FIX_FIX1
set geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--Flight Service Stations (FSS)
SELECT AddGeometryColumn( 'FSS_FSS' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'FSS_FSS' , 'geometry' );
UPDATE FSS_FSS
set geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--Holding Patterns (HPF)
SELECT AddGeometryColumn( 'HPF_HP1' , 'fixGeometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'HPF_HP1' , 'fixGeometry' );
UPDATE HPF_HP1
set fixGeometry = MakePoint(
CAST (longitude_of_the_associated_fix AS DOUBLE),
CAST (latitude_of_the_associated_fix AS DOUBLE),
4326);
SELECT AddGeometryColumn( 'HPF_HP1' , 'navaidGeometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'HPF_HP1' , 'navaidGeometry' );
UPDATE HPF_HP1
set navaidGeometry = MakePoint(
CAST (longitude_of_the_associated_navaid AS DOUBLE),
CAST (latitude_of_the_associated_navaid AS DOUBLE),
4326);
--ILS (ILS)
SELECT AddGeometryColumn( 'ILS_ILS2' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'ILS_ILS2' , 'geometry' );
UPDATE ILS_ILS2
set geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
SELECT AddGeometryColumn( 'ILS_ILS3' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'ILS_ILS3' , 'geometry' );
UPDATE ILS_ILS3
set geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
SELECT AddGeometryColumn( 'ILS_ILS4' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'ILS_ILS4' , 'geometry' );
UPDATE ILS_ILS4
set geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
SELECT AddGeometryColumn( 'ILS_ILS5' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'ILS_ILS5' , 'geometry' );
UPDATE ILS_ILS5
set geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--Military Training Routes (MTR)
SELECT AddGeometryColumn( 'MTR_MTR5' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'MTR_MTR5' , 'geometry' );
UPDATE MTR_MTR5
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--Navaids (NAV)
SELECT AddGeometryColumn( 'NAV_NAV1' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'NAV_NAV1' , 'geometry' );
UPDATE NAV_NAV1
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--Parachute Jump Areas (PJA)
SELECT AddGeometryColumn( 'PJA_PJA1' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'PJA_PJA1' , 'geometry' );
UPDATE PJA_PJA1
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--STARs and SIDs (SSD)
SELECT AddGeometryColumn( 'SSD_SSD' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'SSD_SSD' , 'geometry' );
UPDATE SSD_SSD
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--STARs and SIDs (STARDP)
SELECT AddGeometryColumn( 'STARDP_STARDP' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'STARDP_STARDP' , 'geometry' );
UPDATE STARDP_STARDP
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--Towers
SELECT AddGeometryColumn( 'TWR_TWR1' , 'airport_reference_pointGeometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'TWR_TWR1' , 'airport_reference_pointGeometry' );
UPDATE TWR_TWR1
SET airport_reference_pointGeometry = MakePoint(
CAST (airport_reference_point_longitude AS DOUBLE),
CAST (airport_reference_point_latitude AS DOUBLE),
4326);
SELECT AddGeometryColumn( 'TWR_TWR1' , 'airport_surveillance_radarGeometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'TWR_TWR1' , 'airport_surveillance_radarGeometry' );
UPDATE TWR_TWR1
SET airport_surveillance_radarGeometry = MakePoint(
CAST (airport_surveillance_radar_longitude AS DOUBLE),
CAST (airport_surveillance_radar_latitude AS DOUBLE),
4326);
SELECT AddGeometryColumn( 'TWR_TWR1' , 'direction_finding_antennaGeometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'TWR_TWR1' , 'direction_finding_antennaGeometry' );
UPDATE TWR_TWR1
SET direction_finding_antennaGeometry = MakePoint(
CAST (longitude_of_direction_finding_antenna AS DOUBLE),
CAST (latitude_of_direction_finding_antenna AS DOUBLE),
4326);
SELECT AddGeometryColumn( 'TWR_TWR7' , 'airportGeometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'TWR_TWR7' , 'airportGeometry' );
UPDATE TWR_TWR7
SET airportGeometry = MakePoint(
CAST ( airport_longitude AS DOUBLE),
CAST ( airport_latitude AS DOUBLE),
4326);
--Weather Locations (WXL)
SELECT AddGeometryColumn( 'WXL_WXL' , 'geometry' , 4326, 'POINT');
SELECT CreateSpatialIndex( 'WXL_WXL' , 'geometry' );
UPDATE WXL_WXL
SET geometry = MakePoint(
CAST (longitude AS DOUBLE),
CAST (latitude AS DOUBLE),
4326);
--#Create airway lines
--Military training routes
CREATE TABLE
MTR_MTRLINES (unique_id
, route_identifier
, route_type
, geometry
);
INSERT INTO MTR_MTRLINES
SELECT
mtr_mtr5.route_type || mtr_mtr5.route_identifier AS unique_id
, mtr_mtr5.route_identifier
, mtr_mtr5.route_type
, 'linestring( ' || GROUP_CONCAT(mtr_mtr5.longitude
|| ' '
|| mtr_mtr5.latitude)
|| ' )' AS geometry
FROM
mtr_mtr5
GROUP BY
unique_id
ORDER BY
CAST(mtr_mtr5.record_sort_sequence_number_segment_sequence_number_for_this_po AS REAL)
;
SELECT
AddGeometryColumn( 'MTR_MTRLINES' , 'airwayGeom' , 4326, 'LINESTRING');
SELECT
CreateSpatialIndex( 'MTR_MTRLINES' , 'airwayGeom' );
UPDATE MTR_MTRLINES
SET
airwayGeom = LineFromText(geometry, 4326)
;
--Airway segments
--First create a table
CREATE TABLE
AWY_AWYSEGMENTS (airway_designation
, airway_type
, airway_point_sequence_number
, navaid_facility_fix_name
, point_to_point_minimum_enroute_altitude_mea
, navaid_facility_fix_latitude
, navaid_facility_fix_longitude
, navaid_facility_fix_latitude2
, navaid_facility_fix_longitude2
);
--use a transaction to ensure it all gets done (or nothing is changed)
-- begin transaction
--Insert data into that new table with rows consisting of data from two separate tables (AWY1 and AWY2)
INSERT INTO AWY_AWYSEGMENTS
SELECT
awy1.airway_designation
, awy1.airway_type
, awy1.airway_point_sequence_number
, awy2.navaid_facility_fix_name
, awy1.point_to_point_minimum_enroute_altitude_mea
, awy2.latitude
, awy2.longitude
, awy2a.latitude
, awy2a.longitude
FROM
awy_awy1 AS awy1
JOIN
awy_awy2 AS awy2
ON
awy1.airway_point_sequence_number = awy2.airway_point_sequence_number
AND
awy1.airway_designation = awy2.airway_designation
AND
awy1.airway_type = awy2.airway_type
JOIN
awy_awy2 AS awy2a
ON
CAST (awy1.airway_point_sequence_number AS REAL) + 10 = CAST(awy2a.airway_point_sequence_number AS REAL)
AND
awy1.airway_designation = awy2a.airway_designation
AND
awy1.airway_type = awy2a.airway_type
WHERE
-- awy1.airway_designation = 'J1'
-- AND
awy2.longitude != ''
AND
awy2.latitude != ''
AND
awy2.longitude != '0'
AND
awy2.latitude != '0'
AND
awy2a.longitude != ''
AND
awy2a.latitude != ''
AND
awy2a.longitude != '0'
AND
awy2a.latitude != '0'
ORDER BY
awy1.airway_point_sequence_number
;
-- Make some lines from the segments
SELECT
AddGeometryColumn( 'AWY_AWYSEGMENTS' , 'airwayGeom' , 4326, 'LINESTRING');
SELECT
CreateSpatialIndex( 'AWY_AWYSEGMENTS' , 'airwayGeom' );
UPDATE AWY_AWYSEGMENTS
SET
airwayGeom = LineFromText('LINESTRING( '
|| navaid_facility_fix_longitude
||' '
|| navaid_facility_fix_latitude
||','
|| navaid_facility_fix_longitude2
||' '
|| navaid_facility_fix_latitude2
||' )'
, 4326)
;
VACUUM;