Skip to content

Instantly share code, notes, and snippets.

@joakimsk
Last active March 14, 2018 09:37
Show Gist options
  • Save joakimsk/0b011d8f922126f5a3a801b689d769cf to your computer and use it in GitHub Desktop.
Save joakimsk/0b011d8f922126f5a3a801b689d769cf to your computer and use it in GitHub Desktop.
Merge and write DXF

Merge DXF (up to 2018) and then write DXF (2004) file

Make or update sqlite database using file1.dxf, preserve any layers and drop Z coordinate

ogr2ogr -f SQLite "merged.sqlite" "file1.dxf" -dsco SPATIALITE=YES -nln 'entities' -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');" --config OGR_ENABLE_PARTIAL_REPROJECTION TRUE --debug ON -update -append -dim XY

Repeat this step for each file to be merged.

Create DXF from sqlite database, preserving text fields using custom styling

ogr2ogr -f DXF "mergedlabel.dxf" "merged.sqlite" -dim XY -dialect SQLITE -sql "SELECT 'LABEL(f:\"Arial\",s:1.5g,t:\"SOMETHING\")' AS OGR_STYLE, geometry FROM entities" --debug ON

Verify that points have text

ogrinfo -al merged.dxf

If you want to quickly test queries, use ogrinfo

ogrinfo merged.sqlite -dialect SQLITE -sql "SELECT 'LABEL(f:\"Arial\",s:1.5g,t:\"'||text||'\")' AS OGR_STYLE FROM entities;"

Complex query using CASE to check that a row is a POINT or POINT Z before modifying things

ogr2ogr -f DXF "mergedlabel.dxf" "merged.sqlite" -dim XY -dialect SQLITE -sql "SELECT CASE WHEN ST_GeometryType(e.geometry) IN ('POINT', 'POINT Z') THEN 'pno' ELSE e.layer END AS layer, CASE WHEN ST_GeometryType(e.geometry) IN ('POINT', 'POINT Z') THEN 'LABEL(f:\"Arial\",s:2.0g,t:\"'||e.text||'\")' ELSE '' END AS OGR_STYLE, e.geometry FROM entities e" --debug ON

References

http://www.gdal.org/drv_dxf.html

https://trac.osgeo.org/gdal/ticket/5596

https://stackoverflow.com/questions/6134415/how-to-concatenate-strings-with-padding-in-sqlite

https://stackoverflow.com/questions/1294619/does-sqlite-support-any-kind-of-ifcondition-statement-in-a-select

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment