1

I have four different folders with Ordnance Survey-Shapefiles from different regions.

enter image description here

I want to merge all these files theme by theme (for example all buildings to just one shapefile). The problem is that these are loads of shapefiles and all have different names like SS-Buildings.shp, ST_Buildings.shp and so on.

Has anyone else before managed to merge these files from different regions automatically (ogr2ogr / processing-toolbox?)?

Any ideas are very welcome.

What I found so far is this question: Batch merging shapefiles from different folders, different subfolders

The problem with the OS-files is that the different shapefiles from one topic like "Buildings" don't have the same name:

enter image description here

(Here I put together all "buildings"-shapefiles in one folder manually )

So I have to find a way to merge the files ignoring the first three characters of the filename and merging them together from different subfolders... seems to be pretty challenging

2

Just use a recursive For Loop within a batchfile. Cut off the first three characters of the filename and merge them together:

@echo off

set OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"

SETLOCAL ENABLEDELAYEDEXPANSION

rem set your folder which contains the shapefiles and change to this folder:

set verzeichnis=C:\YourShapefileFolder\ 

cd %verzeichnis%

rem recursive for loop 
FOR /R %%b IN (*.shp) DO (

    rem get filename without path
    set var=%%~nxb
    rem cut off the first three characters and merge the files together

    echo merging now: !var! to !var:~3! 

    if NOT exist "!var:~3!" (ogr2ogr -f "ESRI Shapefile" "!var:~3!" "%%~fnxb") else (ogr2ogr -append -update -f "Esri Shapefile" "!var:~3!" "%%~fb" )

)

Echo -------------------------------------
Echo merging done!
Echo -------------------------------------

PAUSE

I just gave it a try and it works on my Win7-Laptop.

https://gist.github.com/neogis-de/6fcca708020dda4a5fca

1

On a Linux box you can do the following to extract and merge OS VectorMap District

for i in *.zip                                                 
do
  x=`echo $i | sed 's/^vmdvec_//;s/.zip$//'`
  unzip -n -d $x $i '*/data/*'
  (cd $x;mv OS\ VectorMap\ District\ \(ESRI\ Shape\ File\)\ */data/* .)
done

Which extracts the data dir from the zip file and moves the shapefiles to a directory without spaces in it to make life easier.

Then for each shapefile in the first directory (hp in my case but any of them will do) append all the files that share it's name in the sub folders.

for i in hp/HP_*.shp
do
  a=${i##*/HP_}

  for k in */*_$a
  do
    ogr2ogr -append $a $k
  done
 done

For completeness I'll note here that this fails for the whole country as it exceeds the max record number for a shapefile in which case you will want to do:

for i in hp/HP_*.shp
do
a=${i##*/HP_}
b=${a%.shp}
for k in */*_$a
do
ogr2ogr -t_srs epsg:27700 -a_srs epsg:27700 -append -f "PostgreSQL" PG:"host=localhost user=ian dbname=vectormapdistrict  schemas=vecmapdist" -nlt PROMOTE_TO_MULTI -lco spatialindex=off -nln $b $k
done
done
  • Thanks for this idea.But i want to automatically merge all 24 themes from different regions to 24 merged layers, not only the buildings. – Dr.Doedel_23cm Sep 4 '15 at 10:01
  • Loop through the first folder to get the type names – Ian Turton Sep 4 '15 at 13:42
  • As a warning to others if you try this for the whole country it fails when the Shapefile gets too large and the index overflows :-( but would work to a DB – Ian Turton Sep 4 '15 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.