Skip to content

Instantly share code, notes, and snippets.

@sfkeller
Last active December 5, 2019 16:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfkeller/0a8f7d951b00ae64f389e7f3c253c94f to your computer and use it in GitHub Desktop.
Save sfkeller/0a8f7d951b00ae64f389e7f3c253c94f to your computer and use it in GitHub Desktop.
@echo off
echo %0.bat - Reading all Shapefiles from a directory into a GeoPackage file
rem Constants
set version=Version 0.1
set outfile=geopackage.gpkg
rem Checking if OGR is installed and in program path
ogr2ogr --version >nul
if not %errorlevel%==0 goto ogrnotfound
echo.
rem Options handling
if %1.==. goto processing
if "%1"=="/?" goto usage
if "%1"=="-help" goto usage
if "%1"=="-version" goto usage
if not %2.==. goto usage
rem Don't care if outfile exists.
set outfile=%1.gpkg
:processing
echo Reading from directory:
cd
for %%f in (*.shp) do (
echo Reading "%%f"
ogr2ogr -f "GPKG" "%outfile%" %%f -update -overwrite -skipfailures
if not exist "%outfile%" goto end
)
echo.
echo GeoPackage file "%outfile%" written.
goto end
:ogrnotfound
echo.
echo ERROR: Program OGR/GDAL not found (not installed or not in program path).
goto end
:usage
echo %0 %version% using this ogr2ogr version (requires OGR/GDAL).
ogr2ogr --version
echo.
echo Usage:
echo %0 -- Writes to file using default file name %outfile%
echo %0 outfile -- Writes to given outfile(.gpkg)
echo.
echo Reads all .shp files from current directory, so e.g. copy it there.
echo The file extension .gpkg is added to a given outfile name.
echo If outfile(.gpkg) does not exist, it's being created.
echo If a layer (Shapefile) in outfile(.gpkg) exists, it's being overwritten.
:end
echo on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment