2018-09-25 14:20:41 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Load geometries from GeoJSON to Postgres
|
|
|
|
# - assume postgres connection details are set in the environment using PGUSER, PGHOST etc.
|
2022-03-29 09:48:27 -04:00
|
|
|
|
2018-09-25 14:20:41 -04:00
|
|
|
: ${1?"Usage: $0 ./path/to/mastermap/dir"}
|
|
|
|
|
|
|
|
mastermap_dir=$1
|
|
|
|
|
|
|
|
# Create 'geometry' record with
|
|
|
|
# id: <polygon-guid>,
|
|
|
|
# source_id: <toid>,
|
|
|
|
# geom: <geom>
|
2022-03-29 09:48:27 -04:00
|
|
|
|
2022-04-08 06:33:30 -04:00
|
|
|
echo "Creating temporary geometries table for input data..."
|
|
|
|
psql -c "CREATE TABLE IF NOT EXISTS release_geometries (
|
|
|
|
geometry_id serial PRIMARY KEY,
|
|
|
|
source_id varchar(30),
|
|
|
|
geometry_geom geometry(GEOMETRY, 3857)
|
|
|
|
);"
|
|
|
|
|
2022-03-29 09:48:27 -04:00
|
|
|
echo "Copy geometries to db..."
|
2018-09-25 14:20:41 -04:00
|
|
|
find $mastermap_dir -type f -name '*.3857.csv' \
|
2018-09-30 16:23:19 -04:00
|
|
|
-printf "$mastermap_dir/%f\n" | \
|
2018-09-25 14:20:41 -04:00
|
|
|
parallel \
|
2022-04-08 06:33:30 -04:00
|
|
|
cat {} '|' psql -c "\"COPY release_geometries ( geometry_geom, source_id ) FROM stdin WITH CSV HEADER;\""
|
|
|
|
|
|
|
|
# Copy release_geometries into existing geometries table
|
2022-04-08 07:11:56 -04:00
|
|
|
psql -c "INSERT INTO geometries ( geometry_geom, source_id ) SELECT geometry_geom, source_id FROM release_geometries;"
|
2018-09-25 14:20:41 -04:00
|
|
|
|
|
|
|
# Delete any duplicated geometries (by TOID)
|
2022-03-29 09:48:27 -04:00
|
|
|
echo "Delete duplicate geometries..."
|
2022-03-28 10:06:23 -04:00
|
|
|
psql -c "DELETE FROM geometries a USING (
|
2018-09-25 14:20:41 -04:00
|
|
|
SELECT MIN(ctid) as ctid, source_id
|
|
|
|
FROM geometries
|
|
|
|
GROUP BY source_id
|
|
|
|
HAVING COUNT(*) > 1
|
|
|
|
) b
|
|
|
|
WHERE a.source_id = b.source_id
|
|
|
|
AND a.ctid <> b.ctid;"
|