58 lines
1.5 KiB
Text
58 lines
1.5 KiB
Text
|
#!/usr/bin/env bash
|
||
|
set -eau -o pipefail
|
||
|
|
||
|
ARCEP_WWW="https://www.data.gouv.fr/fr/datasets/le-marche-du-haut-et-tres-haut-debit-fixe-deploiements/"
|
||
|
|
||
|
if [ "$#" -ne 2 ]; then
|
||
|
echo "Usage: ingest path-to-arcep-ipe-csv-file path-to-generated-db"
|
||
|
echo "ARCEP file can be downloaded here: ${ARCEP_WWW}"
|
||
|
echo ""
|
||
|
exit 1
|
||
|
fi
|
||
|
fullIpePath=$(realpath "${1}")
|
||
|
fullDbPath=$(realpath "${2}")
|
||
|
|
||
|
tmpSql=$(mktemp)
|
||
|
clean_tmp () {
|
||
|
rm "${tmpSql}"
|
||
|
}
|
||
|
trap clean_tmp EXIT
|
||
|
|
||
|
echo "[+] Ingesting IPE data from ${fullIpePath}"
|
||
|
echo ""
|
||
|
cat > "${tmpSql}" <<EOF
|
||
|
.separator ","
|
||
|
.import ${fullIpePath} arcep
|
||
|
EOF
|
||
|
|
||
|
sqlite3 "${fullDbPath}" < "${tmpSql}"
|
||
|
|
||
|
echo "[+] Ingesting spatial data."
|
||
|
|
||
|
cat > "${tmpSql}" <<EOF
|
||
|
SELECT load_extension("mod_spatialite");
|
||
|
SELECT InitSpatialMetaData();
|
||
|
|
||
|
-- Despite the SELECT, we're actually creating a new ImmeubleGeoPoint
|
||
|
-- column here. The spatialite API is a bit weird...
|
||
|
SELECT AddGeometryColumn('arcep','ImmeubleGeoPoint',4326,'POINT');
|
||
|
|
||
|
-- The geodata is expressed in RGF93 (SRID 2154). We need to project
|
||
|
-- it to WSG84 to display it on a OSM map.
|
||
|
UPDATE arcep SET ImmeubleGeoPoint =
|
||
|
MakePoint(
|
||
|
CAST(x as DOUBLE),
|
||
|
CAST(y as DOUBLE),
|
||
|
4326);
|
||
|
EOF
|
||
|
sqlite3 "${fullDbPath}" < "${tmpSql}"
|
||
|
|
||
|
echo "[+] Creating Rtree index (spatial index). We're almost done."
|
||
|
cat > "${tmpSql}" <<EOF
|
||
|
SELECT load_extension("mod_spatialite");
|
||
|
SELECT CreateSpatialIndex('arcep','ImmeubleGeoPoint');
|
||
|
EOF
|
||
|
sqlite3 "${fullDbPath}" < "${tmpSql}"
|
||
|
|
||
|
echo "[+] SQLite database generated at ${fullDbPath}"
|