Skip to content

Instantly share code, notes, and snippets.

@joakimsk
Last active March 4, 2018 05:30
Show Gist options
  • Save joakimsk/03cee3182a9149a121a7205e3c26dc4c to your computer and use it in GitHub Desktop.
Save joakimsk/03cee3182a9149a121a7205e3c26dc4c to your computer and use it in GitHub Desktop.
Script to merge geographical data using ogr2ogr
#!/bin/bash
# Merge accepted files into one geojson using ogr2ogr. Supports DXF 2004 and later, Geojson and more
# http://www.gdal.org/ogr_formats.html
# This is a bit slow, but npm install geojson-merge seems to have trouble with big files
# Includes option to enable partial reprojection, dropping invalid coordinates, and also reprojects with custom PROJ.4 string
# Drops Z axis using -dim XY, but accepts Z axis features.
for f in *.dxf
do
if [ ! -f merged.geojson ]; then
ogr2ogr --config OGR_ENABLE_PARTIAL_REPROJECTION TRUE --config CPL_DEBUG ON -s_srs "+proj=sterea +lat_0=34.2 +lon_0=39.15 +x_0=28 +y_0=-42 +a=6378249.2 +b=6356515 +units=m +no_defs" -t_srs EPSG:4326 -a_srs EPSG:4326 -dim XY -dialect SQLITE -sql "SELECT e.layer, e.text, e.geometry FROM entities e WHERE ST_GeometryType(e.geometry) IN ('POINT', 'POINT Z', 'LINESTRING', 'LINESTRING Z', 'MULTILINESTRING', 'MULTILINESTRING Z', 'POLYGON', 'POLYGON Z', 'MULTIPOLYGON', 'MULTIPOLYGON Z');" -f "GeoJSON" merged.geojson "$f" -nln 'entities'
else
ogr2ogr --config OGR_ENABLE_PARTIAL_REPROJECTION TRUE --config CPL_DEBUG ON -s_srs "+proj=sterea +lat_0=34.2 +lon_0=39.15 +x_0=28 +y_0=-42 +a=6378249.2 +b=6356515 +units=m +no_defs" -t_srs EPSG:4326 -a_srs EPSG:4326 -dim XY -dialect SQLITE -sql "SELECT e.layer, e.text, e.geometry FROM entities e WHERE ST_GeometryType(e.geometry) IN ('POINT', 'POINT Z', 'LINESTRING', 'LINESTRING Z', 'MULTILINESTRING', 'MULTILINESTRING Z', 'POLYGON', 'POLYGON Z', 'MULTIPOLYGON', 'MULTIPOLYGON Z');" -f "GeoJSON" -update -append merged.geojson "$f" -nln 'entities'
fi
done
# Usng GDAL Errors, many different submodules report errors using --config CP_DEBUG ON
# DXF OGRCT SQLITE DWG OGR GDAL GEOJSON GDALVectorTranslate OGR2SQLITE
# DXF: Unsupported HATCH boundary line type:4
# See more useful config options here https://trac.osgeo.org/gdal/wiki/ConfigOptions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment