2022-04-08 04:49:39 -04:00
|
|
|
#!/usr/bin/env bash
|
2022-04-08 07:17:53 -04:00
|
|
|
|
2022-04-14 04:27:19 -04:00
|
|
|
# Load geometries from GeoJSON to Postgres
|
|
|
|
# - assume postgres connection details are set in the environment using PGUSER, PGHOST etc.
|
|
|
|
|
|
|
|
: ${1?"Usage: $0 ./path/to/mastermap/dir"}
|
|
|
|
|
|
|
|
mastermap_dir=$1
|
|
|
|
|
|
|
|
# Create 'geometry' record with
|
|
|
|
# id: <polygon-guid>,
|
|
|
|
# source_id: <toid>,
|
|
|
|
# geom: <geom>
|
|
|
|
|
2022-04-14 04:55:58 -04:00
|
|
|
echo "Removing temp tables if previously created..."
|
|
|
|
psql -c "DROP TABLE IF EXISTS new_geometries;"
|
|
|
|
psql -c "DROP TABLE IF EXISTS release_geometries;"
|
|
|
|
|
|
|
|
echo "Creating temporary geometries table for OS release geometries..."
|
2022-04-14 04:27:19 -04:00
|
|
|
psql -c "CREATE TABLE IF NOT EXISTS release_geometries (
|
2022-04-21 05:21:22 -04:00
|
|
|
geometry_id serial,
|
|
|
|
source_id varchar(30),
|
2022-04-14 04:27:19 -04:00
|
|
|
geometry_geom geometry(GEOMETRY, 3857)
|
|
|
|
);"
|
|
|
|
|
|
|
|
echo "Copy geometries to db..."
|
|
|
|
find $mastermap_dir -type f -name '*.3857.csv' \
|
|
|
|
-printf "$mastermap_dir/%f\n" | \
|
|
|
|
parallel \
|
|
|
|
cat {} '|' psql -c "\"COPY release_geometries ( geometry_geom, source_id ) FROM stdin WITH CSV HEADER;\""
|
|
|
|
|
2022-04-14 04:55:58 -04:00
|
|
|
echo "Creating temporary geometries table for new geometries only..."
|
|
|
|
psql -c "CREATE TABLE IF NOT EXISTS new_geometries (
|
2022-04-21 05:21:22 -04:00
|
|
|
source_id varchar(30),
|
2022-04-14 04:55:58 -04:00
|
|
|
geometry_geom geometry(GEOMETRY, 3857)
|
|
|
|
);"
|
|
|
|
|
2022-04-21 05:21:22 -04:00
|
|
|
# Delete any duplicated geometries (by TOID)
|
|
|
|
echo "Delete duplicate geometries..."
|
|
|
|
psql -c "DELETE FROM release_geometries a USING (
|
|
|
|
SELECT MIN(ctid) as ctid, source_id
|
|
|
|
FROM release_geometries
|
|
|
|
GROUP BY source_id
|
|
|
|
HAVING COUNT(*) > 1
|
|
|
|
) b
|
|
|
|
WHERE a.source_id = b.source_id
|
|
|
|
AND a.ctid <> b.ctid;"
|
|
|
|
|
2022-04-14 04:55:58 -04:00
|
|
|
echo "Finding geometries that are new to this release..."
|
|
|
|
psql -c "INSERT INTO new_geometries ( source_id, geometry_geom )
|
|
|
|
SELECT source_id, geometry_geom
|
2022-04-14 05:33:38 -04:00
|
|
|
FROM release_geometries AS r
|
|
|
|
WHERE NOT EXISTS ( SELECT source_id
|
|
|
|
FROM geometries AS g
|
|
|
|
WHERE g.source_id = r.source_id);"
|