chargement de la couche communale

This commit is contained in:
MaelREBOUX 2021-11-12 16:00:07 +01:00
parent 789da8ccd9
commit 2cfe01eb74
3 changed files with 106 additions and 88 deletions

101
scripts_v2/load_communes_osm.sh Executable file
View file

@ -0,0 +1,101 @@
#! /bin/bash
# exit dès que qqch se passe mal
set -e
# sortir si "unbound variable"
#set -u
if [ -z "$1" ]
then
echo "Pas de millésime en argument --> stop"
exit 1
fi
# lecture du fichier de configuration
. config.sh
cd $rep_data
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo " Récupération des communes FR"
echo ""
echo " téléchargement"
millesimeSHP=20210101
{
wget -nc http://osm13.openstreetmap.fr/~cquest/openfla/export/communes-$millesimeSHP-shp.zip -O communes-$millesimeSHP-shp.zip
unzip -oq communes-$millesimeSHP-shp.zip
} ||
echo " fait"
echo ""
echo " chargement en base"
ogr2ogr -f "PostgreSQL" PG:"host=$DB_HOST port=$DB_PORT user=$DB_USER password=$DB_PASSWD dbname=$DB_NAME" \
communes-$millesimeSHP.shp -nln osm_communes_fr_4326 -lco GEOMETRY_NAME=the_geom -explodecollections -overwrite -where "substr(insee,0,2) IN ('22','29','35','44','56')"
# nettoyage
rm communes-$millesimeSHP.*
echo " fait"
echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo " Récupération des communes BR"
echo ""
echo " téléchargement"
{
wget -nc https://tile.openstreetmap.bzh/data/br/osm_br_municipalities.geojson
} ||
echo " fait"
echo ""
echo " chargement en base"
ogr2ogr -f "PostgreSQL" PG:"host=$DB_HOST port=$DB_PORT user=$DB_USER password=$DB_PASSWD dbname=$DB_NAME" \
osm_br_municipalities.geojson -nln osm_communes_br_4326 -lco GEOMETRY_NAME=the_geom -overwrite
echo " fait"
echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo " Chargement de la couche osm_communes"
echo ""
PGPASSWORD=$DB_PASSWD $PSQL -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c \
"TRUNCATE TABLE osm_communes ;"
PGPASSWORD=$DB_PASSWD $PSQL -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c \
"WITH comm_multi AS (
SELECT
fr.insee,
fr.nom,
ST_Multi(ST_Union(fr.the_geom)) AS the_geom
FROM osm_communes_fr_4326 fr
GROUP BY fr.insee, fr.nom
)
INSERT INTO osm_communes
SELECT
fr.insee,
fr.nom AS name_fr,
br.name_br,
ST_Transform(fr.the_geom,2154)
FROM comm_multi fr, osm_communes_br_4326 br
WHERE ST_Intersects(br.the_geom,fr.the_geom) AND fr.nom = br.name
ORDER BY fr.insee ;"
PGPASSWORD=$DB_PASSWD $PSQL -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c \
"VACUUM FULL osm_communes ;"
echo " fait"
echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo " F I N "

View file

@ -1,51 +0,0 @@
#! /bin/bash
# exit dès que qqch se passe mal
set -e
# sortir si "unbound variable"
#set -u
if [ -z "$1" ]
then
echo "Pas de millésime en argument --> stop"
exit 1
fi
# lecture du fichier de configuration
. config.sh
# ce script récupère une couche des communes de France et la charge dans la base de données
cd $rep_data
millesimeSHP=20210101
# récupérer la couche communales OSM
# https://www.data.gouv.fr/fr/datasets/decoupage-administratif-communal-francais-issu-d-openstreetmap/
wget http://osm13.openstreetmap.fr/~cquest/openfla/export/communes-$millesimeSHP-shp.zip -O communes-$millesimeSHP-shp.zip
unzip -o communes-$millesimeSHP-shp.zip
ogr2ogr -f "PostgreSQL" PG:"host=$DB_HOST user=$DB_USER password=$DB_PASSWD dbname=$DB_NAME" \
communes-$millesimeSHP.shp -nln osm_communes_4326 -lco GEOMETRY_NAME=the_geom -explodecollections -overwrite
# passer la couche de WGS84 en Lambert93
PGPASSWORD=$DB_PASSWD $PSQL -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "TRUNCATE TABLE osm_communes ;"
PGPASSWORD=$DB_PASSWD $PSQL -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "
INSERT INTO osm_communes
SELECT
ogc_fid,
insee,
nom,
wikipedia,
surf_ha,
ST_Transform(ST_SetSRID(the_geom,4326),2154) AS the_geom
FROM osm_communes_4326
WHERE left(insee,2) IN ('22','29','35','44','56')
ORDER BY insee ASC ;"
PGPASSWORD=$DB_PASSWD $PSQL -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "VACUUM FULL osm_communes;"
# nettoyage
rm communes-$millesimeSHP.*

View file

@ -228,54 +228,22 @@ CREATE VIEW phase_2_pk_secteur_4326 AS
ALTER TABLE phase_2_pk_secteur_4326 OWNER to redadeg;
-- les polygones des communes source OSM France
-- la couche des communes source OSM
DROP TABLE IF EXISTS osm_communes CASCADE ;
CREATE TABLE osm_communes
(
gid serial,
insee character varying(80),
nom character varying(80),
wikipedia character varying(80),
surf_ha numeric,
insee character varying(5),
name_fr text,
name_br text,
the_geom geometry,
CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'POLYGON'::text OR geometrytype(the_geom) = 'MULTIPOLYGON'::text),
CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 2154),
CONSTRAINT osm_communes_pkey PRIMARY KEY (gid)
CONSTRAINT osm_communes_pkey PRIMARY KEY (insee)
);
CREATE INDEX sidx_osm_communes_the_geom ON osm_communes USING gist(the_geom);
ALTER TABLE osm_communes OWNER to redadeg;
-- la couche avec les info langue minoritaire
DROP TABLE IF EXISTS osm_municipalities CASCADE ;
CREATE TABLE osm_municipalities
(
id serial,
osm_id bigint,
type text,
admin_level text,
name text,
name_fr text,
name_br text,
source_name_br text,
admincode text,
postcode text,
wikidata text,
surf_ha numeric,
x numeric,
y numeric,
the_geom geometry,
CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'POLYGON'::text OR geometrytype(the_geom) = 'MULTIPOLYGON'::text),
CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 2154),
CONSTRAINT osm_municipalities_pkey PRIMARY KEY (id)
);
CREATE INDEX osm_municipalities_geom_idx ON osm_municipalities USING gist(the_geom);
CREATE INDEX osm_municipalities_admincode_idx ON osm_municipalities(admincode);
ALTER TABLE osm_municipalities OWNER to redadeg;
-- la couche qui contient les lignes des routes venant de OSM
DROP TABLE IF EXISTS osm_roads CASCADE ;