Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created September 17, 2021 00:11
Show Gist options
  • Save ThomasG77/37d680b8e49134f16f4b4e65e5692f93 to your computer and use it in GitHub Desktop.
Save ThomasG77/37d680b8e49134f16f4b4e65e5692f93 to your computer and use it in GitHub Desktop.

VRT to merge vector data

Intro

Adopte le VRT, cela changera ta vie. Exemple: les IRIS par territoire. Je veux une sortie WGS 84. Avec le VRT, je ne crée pas de fichier immédiatement

Recipe

Retrieve data and uncompress

Download

for i in $(cat<< EOF
ftp://Iris_GE_ext:eeLoow1gohS1Oot9@ftp3.ign.fr/IRIS-GE_2-0__SHP_LAMB93_FXX_2021-01-01.7z
ftp://Iris_GE_ext:eeLoow1gohS1Oot9@ftp3.ign.fr/IRIS-GE_2-0__SHP_RGAF09UTM20_R01_2021-01-01.7z
ftp://Iris_GE_ext:eeLoow1gohS1Oot9@ftp3.ign.fr/IRIS-GE_2-0__SHP_RGAF09UTM20_R02_2021-01-01.7z
ftp://Iris_GE_ext:eeLoow1gohS1Oot9@ftp3.ign.fr/IRIS-GE_2-0__SHP_UTM22RGFG95_R03_2021-01-01.7z
ftp://Iris_GE_ext:eeLoow1gohS1Oot9@ftp3.ign.fr/IRIS-GE_2-0__SHP_RGR92UTM40S_R04_2021-01-01.7z
ftp://Iris_GE_ext:eeLoow1gohS1Oot9@ftp3.ign.fr/IRIS-GE_2-0__SHP_RGM04UTM38S_R06_2021-01-01.7z
EOF
);
do wget $i;
done;

Uncompress

for i in *.7z;
  do 7z x $i;
done;

Manipulate with GDAL utilities

Merge with VRT

SHAPE_ENCODING=ISO-8859-1 \
ogrmerge.py -single -o iris_ge_france_entiere_wgs84.vrt IRIS-GE_2-0__SHP_*/*/1_*/*/IRIS_GE.SHP -overwrite_ds -t_srs "EPSG:4326" -f VRT -nln iris_ge_france_entiere_wgs84

Convert to GPKG

ogr2ogr -f GPKG iris_ge_france_entiere_wgs84.gpkg iris_ge_france_entiere_wgs84.vrt -nln iris_ge_wgs84 -nlt PROMOTE_TO_MULTI

Export to PostGIS table

Set variable

export PGHOST=localhost
export PGPORT=5432
export PGDATABASE=my_database
export PGUSER=my_password
export PGPASSWORD=my_user

Create schema

psql -c "CREATE SCHEMA IF NOT EXISTS administratif"

Load to database in the "right schema"

ogr2ogr -f "PostGreSQL" \
        PG:"host="$PGHOST" port="$PGPORT" dbname="$PGDATABASE" user="$PGUSER" password="$PGPASSWORD"" \
        iris_ge_france_entiere_wgs84.vrt iris_ge_france_entiere_wgs84 \
        -lco OVERWRITE=yes \
        -lco SCHEMA=administratif \
        -lco SPATIAL_INDEX=GIST \
        -lco GEOMETRY_NAME=the_geom \
        --config PG_USE_COPY YES \
        -nlt PROMOTE_TO_MULTI \
        -nln iris_ge_wgs84 \
        -lco FID=fid \
        -a_srs "EPSG:4326"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment