1

Is this possible to extract layers of a DXF file into multiple files using ogr2ogr command line ?

Let us say my DXF file consists of 2 layers : ROAD and HOME.

I want my output to be some kml files.

So I run the following command :

ogr2ogr -f "KML" plan.kml plan.dxf -s_srs EPSG:27562 -t_srs EPSG:4326

Which works very well.

QUESTION

But how can I specify I want multiples files in output ? Or should I programmatically code this using a loop and extract each layer individually ?

1

To address this issue, ogr2ogr command line does not provide out of the box function to quickly create multiple output (for each layer) for the input file.

SOLUTION

I came up with a solution (using PHP in my case) which is split in 2 steps :

  1. Use ogrinfo to get all the layers
  2. Retrieve all the layer, and for each layer use ogr2ogr

DETAIL

I first launch ogrinfo plan.dxf -sql "SELECT DISTINCT Layer FROM entities" (all my dxf files begin with entities element wrapper). You will need to extract only the name of the layers (in php, for each lines, I used the following regex : /=\s([\w|\s]+)$/u, u flag because sometimes my layers names can contain accents).

Then, once I have my array of layers, for each layers I apply this command : ogr2ogr -f "KML" <layer_name>-fond.kml fond.dxf -s_srs EPSG:27562 EPSG:4326 -where "Layer='<layer_name>'", with <layer_name> to be replaced by the name of the current layer name in your loop.

To inject the layer name, I used PHP string injection :

exec("ogr2ogr -f \"KML\" $layerName-fond.kml fond.dxf -s_srs EPSG:27562 -t_srs EPSG:4326 -where \"Layer='$layerName'\"");

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.