Friday 19 April 2019

gdal - Convert multi-layer DWG into several KML or geoJSON files (one per layer)


I have some big DWG files, with 5 to 50 layers, which I draw and geolocated using AutoCAD (with some external modules). I need to convert them to KML or geoJSON so that the JS library can load them and display them on the map.


I used AutoCAD to export my .DWG into .DXF, then I installed GDAL and used ogr2ogr to convert those DXFs into geoJSON. It works, but the generated geoJSON is very big (more than 10 megabytes) and it loads forever once I try to display it into a google map or a leaflet map.


What I would like to do, is to have a separated geoJSON/KML for every layer of my DWG/DXF file, so that I can load and display each layer separately which hopefully would increase performance.


I am trying to have an automated workflow, because as I have a lot of files, it would take too much time to take every one of them and split layers manually.



Any idea ?



Answer



I ended up making a node.js script which uses ogr2ogr through the command line.


It calls ogr2ogr through a spawned child process to keep it async.


ogr2ogr -f GeoJSON -s_srs epsg:2154 -t_srs epsg:4326 dest_file.json source_file.dxf

Then it reads the new geoJSON file into node, groups features in featureCollection by Layer name, wraps each group into a geoJSON container, and writes each wrapped group to a separate ".json" file.


Code is available here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...