Revision 11443
Added by Paul Sarando about 11 years ago
geovalidate.sql | ||
---|---|---|
72 | 72 |
Created Nov 2012 |
73 | 73 |
*/ |
74 | 74 |
|
75 |
BEGIN; |
|
76 |
|
|
77 |
TRUNCATE geoscrub; |
|
78 |
|
|
79 |
DROP INDEX IF EXISTS "geoscrub_geom_gist"; |
|
80 |
DROP INDEX IF EXISTS "geoscrub_geog_gist"; |
|
81 |
DROP INDEX IF EXISTS "geoscrub_countrystd_idx"; |
|
82 |
DROP INDEX IF EXISTS "geoscrub_stateprovincestd_idx"; |
|
83 |
DROP INDEX IF EXISTS "geoscrub_countystd_idx"; |
|
84 |
|
|
85 |
----------------------------- |
|
86 |
-- put everything together -- |
|
87 |
----------------------------- |
|
88 |
|
|
89 |
-- reconstitute the whole dang thang |
|
90 |
|
|
91 |
-- populate geoscrub from vegbien_geoscrub data |
|
92 |
INSERT INTO geoscrub |
|
93 |
SELECT decimallatitude, decimallongitude, |
|
94 |
country, stateprovince, county, |
|
95 |
countryid, stateprovinceid, countyid, |
|
96 |
countrystd, stateprovincestd, countystd |
|
97 |
FROM vegbien_geoscrub v |
|
98 |
LEFT JOIN vcountry USING (country) |
|
99 |
LEFT JOIN vstate USING (countryid, stateprovince) |
|
100 |
LEFT JOIN vcounty USING (countryid, stateprovinceid, county); |
|
101 |
-- SELECT 1707970 |
|
102 |
-- Time: 26172.154 ms |
|
103 |
|
|
104 |
-- try to squeeze out a few more direct stateprovince mappings to gadm2 |
|
105 |
UPDATE geoscrub |
|
106 |
SET stateprovincestd = name_1 |
|
107 |
FROM (SELECT DISTINCT name_0, name_1 FROM gadm2) gadm |
|
108 |
WHERE country IS NOT NULL |
|
109 |
AND stateprovince IS NOT NULL |
|
110 |
AND countrystd = name_0 |
|
111 |
AND stateprovincestd IS NULL |
|
112 |
AND stateprovince = name_1; |
|
113 |
-- UPDATE 0 |
|
114 |
-- Time: 3630.973 ms |
|
115 |
|
|
116 |
-- try to squeeze out a few more direct county mappings to gadm2 |
|
117 |
UPDATE geoscrub |
|
118 |
SET countystd = name_2 |
|
119 |
FROM (SELECT DISTINCT name_0, name_1, name_2 FROM gadm2) gadm |
|
120 |
WHERE country IS NOT NULL |
|
121 |
AND stateprovince IS NOT NULL |
|
122 |
AND county IS NOT NULL |
|
123 |
AND countrystd = name_0 |
|
124 |
AND stateprovincestd = name_1 |
|
125 |
AND countystd IS NULL |
|
126 |
AND county = name_2; |
|
127 |
-- UPDATE 69 |
|
128 |
-- Time: 3982.715 ms |
|
129 |
|
|
130 |
COMMIT; |
|
131 |
|
|
75 | 132 |
-- generate point geometries from coordinates |
76 |
SELECT AddGeometryColumn('public', 'geoscrub', 'geom', 4326, 'POINT', 2 ); |
|
77 | 133 |
UPDATE geoscrub |
78 | 134 |
SET geom = ST_SetSRID(ST_Point(decimallongitude, decimallatitude), 4326); |
79 | 135 |
-- ... create point geographies too ... |
80 | 136 |
-- note that this excludes (leaves null) any occurrences with coordinates |
81 | 137 |
-- that are not valid decimal degrees |
82 |
ALTER TABLE geoscrub ADD COLUMN geog geography(POINT, 4326); |
|
83 | 138 |
UPDATE geoscrub |
84 | 139 |
SET geog = geom::geography |
85 | 140 |
WHERE geom && ST_MakeBox2D(ST_Point(-180, -90), ST_Point(180, 90)); |
... | ... | |
103 | 158 |
|
104 | 159 |
VACUUM ANALYZE geoscrub; |
105 | 160 |
|
106 |
ALTER TABLE geoscrub ADD COLUMN latlonvalidity int; |
|
107 |
|
|
108 | 161 |
-- -1 if missing one or both coordinates |
109 | 162 |
UPDATE geoscrub o |
110 | 163 |
SET latlonvalidity = -1 |
... | ... | |
129 | 182 |
|
130 | 183 |
VACUUM ANALYZE geoscrub; |
131 | 184 |
|
132 |
ALTER TABLE geoscrub ADD COLUMN countryvalidity int; |
|
133 |
|
|
134 | 185 |
-- -1 if missing country name |
135 | 186 |
UPDATE geoscrub o |
136 | 187 |
SET countryvalidity = -1 |
... | ... | |
203 | 254 |
|
204 | 255 |
VACUUM ANALYZE geoscrub; |
205 | 256 |
|
206 |
ALTER TABLE geoscrub ADD COLUMN stateprovincevalidity int; |
|
207 |
|
|
208 | 257 |
-- -1 if missing stateprovince name |
209 | 258 |
UPDATE geoscrub o |
210 | 259 |
SET stateprovincevalidity = -1 |
... | ... | |
271 | 320 |
-- validate county/parish -- |
272 | 321 |
----------------------------- |
273 | 322 |
|
274 |
ALTER TABLE geoscrub ADD COLUMN countyvalidity int; |
|
275 |
|
|
276 | 323 |
-- -1 if missing county name |
277 | 324 |
UPDATE geoscrub o |
278 | 325 |
SET countyvalidity = -1 |
... | ... | |
333 | 380 |
-- Look up GADM place names based purely on coordinates for all points |
334 | 381 |
-- |
335 | 382 |
|
336 |
ALTER TABLE geoscrub ADD COLUMN pointcountry text; |
|
337 |
ALTER TABLE geoscrub ADD COLUMN pointstateprovince text; |
|
338 |
ALTER TABLE geoscrub ADD COLUMN pointcounty text; |
|
339 | 383 |
-- note: only using simplified geometry now, for speed reasons |
340 | 384 |
UPDATE geoscrub o |
341 | 385 |
SET pointcountry = c.name_0, |
Also available in: Unified diff
Initial checkin of geoscrub install SQL files.
Added install.*.sql files that will do initial table creation for all
required tables.
Added a truncate.vegbien_geoscrub.sql script that will clear tables related to
data downloaded in load-geoscrub-input.sh.
Moved all SQL statements that modify the final geoscrub table into the
geovalidate.sql script.
Modified the geonames.sql and geovalidate.sql scripts to assume all
tables have already been created by the install scripts.