VRT to merge vector data
This example demonstrates how to merge multiple vector datasets into a single unified dataset using the Virtual Dataset (VRT) format, specifically for French IRIS geographic zones. It shows the workflow of combining shapefiles from different territories and projections into one WGS 84 layer without creating an intermediate file, then converting the result to GeoPackage and PostGIS formats. The code uses the `ogrmerge.py` GDAL utility to create the VRT, `ogr2ogr` for format conversions, and psql commands for database setup.
AI-generated descriptionVRT 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"