[GIS] Converting DXF to ESRI Shapefile using ogr2ogr

dxfogr2ogrogrinfoshapefile

I like to convert this DXF file to ESRI Shapefiles. I tried the following command:

ogr2ogr -f "ESRI Shapefile" test.shp Umweltzone-Neu-Ulm.dxf              

… which output the following warnings and errors:

Warning 6: Normalized/laundered field name: 'ExtendedEntity' to 'ExtendedEn'
Warning 6: Normalized/laundered field name: 'EntityHandle' to 'EntityHand'
ERROR 1: Attempt to write non-polygon (LINESTRING) geometry to POLYGON type shapefile.
ERROR 1: Unable to write feature 1 from layer entities.
ERROR 1: Terminating translation prematurely after failed
translation of layer entities (use -skipfailures to skip errors)

Inspecting the file with ogrinfo does not give a helpful advice for me:

ogrinfo -so -al Umweltzone-Neu-Ulm.dxf 
INFO: Open of `Umweltzone-Neu-Ulm.dxf'
      using driver `DXF' successful.

Layer name: entities
Geometry: Unknown (any)
Feature Count: 3
Extent: (573038.389500, 5359699.934800) - (576023.432710, 5361356.286500)
Layer SRS WKT:
(unknown)
Layer: String (0.0)
SubClasses: String (0.0)
ExtendedEntity: String (0.0)
Linetype: String (0.0)
EntityHandle: String (0.0)
Text: String (0.0)

Also, from the Extent I am guessing that the EPSG code is different to 4326. But which?

What is the correct command to perform the conversion?

Related

Best Answer

I guessed the SRS to EPSG:25832 (UTM 32N) by looking at the DXF file with QGIS.

With ogr2ogr:

ogr2ogr -f GeoJSON umweltzone.geojson Umweltzone-Neu-Ulm.dxf -limit 1 -s_srs EPSG:25832 -t_srs EPSG:4326
  • -f GeoJSON output format
  • umweltzone.geojson output file name
  • Umweltzone-Neu-Ulm.dxf input file name
  • -limit 1 output only one feature per layer
  • -s_srs EPSG:25832 source srs
  • -t_srs EPSG:4326 target srs

you can create a geojson file. Looking at the source file, it shows there are three features: a point, a linestring and a polygon.
Limiting the feature (and knowing the polygon is the first one) gives you the polygon. You can omit the limit parameter and you'll get all three features.

edit: with -f 'ESRI Shapefile' instead of GeoJson you will get a shapefile

edit: to create a Shapefile for each feature type you have to run ogr2ogr for each type

ogr2ogr -f "ESRI Shapefile" umweltzone_point.shp Umweltzone-Neu-Ulm.dxf -nlt Point  -s_srs EPSG:25832 -t_srs EPSG:4326 -skipfailures
ogr2ogr -f "ESRI Shapefile" umweltzone_line.shp Umweltzone-Neu-Ulm.dxf -nlt LINESTRING  -s_srs EPSG:25832 -t_srs EPSG:4326 -skipfailures
ogr2ogr -f "ESRI Shapefile" umweltzone_polygon.shp Umweltzone-Neu-Ulm.dxf -nlt POLYGON  -s_srs EPSG:25832 -t_srs EPSG:4326 -skipfailures

You have to use the -skipfailures parameter, because it is not possible to write different geometry types to one shapefile.

Related Question