[GIS] OGR DXF Closed Polyline Conversion

dxflineogr2ogrpolygon

I'm trying to convert closed polylines into polygons using ogr2ogr from dxf to mssql. Here It says that polylines are converted into POLYGON if they are closed, but I always get the type LINESTRING instead of POLYGON in the geometry type column. I also tried to convert DXF into Shapefile and into a MySQL database with same results. Converting Shapefiles with polygons into databases works well, so I think the problem is in the gdal dxf driver.

Can someone explain to me what I am doing wrong?

Here is the command I'm running

ogr2ogr -update -append -f MSSQL "MSSQL:server=.\SQLEXPRESS;database=testdb;trusted_connection=yes" test.dxf

Best Answer

You should send a small example of such DXF file. It looks like DXF driver really does not behave as documented. I made a simple test by digitizing a closing triangle with four coordinates as a polygon and as a linestring

POLYGON (( 616 442, 804 551, 764 387, 616 442 ))
LINESTRING ( 616 442, 804 551, 764 387, 616 442 )

I made a roundtrip by saving geometry into shapefile -> convert shapefile into DXF with ogr2ogr -> checking the DXF file with ogrinfo. If DXF driver behaves as documented both DXF files should contain a polygon. However, that is not the case with the closed linestring

ogrinfo -al line_test.dxf
INFO: Open of `line_test.dxf'
      using driver `DXF' successful.

Layer name: entities
Geometry: Unknown (any)
Feature Count: 1
Extent: (616.000000, 387.000000) - (804.000000, 551.000000)
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)
OGRFeature(entities):0
  Layer (String) = 0
  SubClasses (String) = AcDbEntity:AcDbPolyline
  ExtendedEntity (String) = (null)
  Linetype (String) = (null)
  EntityHandle (String) = 20000
  Text (String) = (null)
  Style = PEN(c:#000000)
  LINESTRING (616 442,804 551,764 387,616 442)

Unfortunately this does not solve your problem but only confirms it. Moreover, the documentation of DXF driver seems to be wrong in other places as well. It claims that GDAL is writing polygons as LWPOLYLINE. However, from the DXF which I converted from the polygon shape I can find that entity HATCH is used:

This is how GDAL creates a polygon:

ENTITIES
  0
HATCH
  5
20000
  8
0
100
AcDbEntity
100
AcDbHatch

This is how a closed linestring was defined:

ENTITIES
  0
LWPOLYLINE
  5
20000
  8
0
100
AcDbEntity
100
AcDbPolyline
 70
1
 90
4

For me it feels like you don't do anything wrong and you should ask from the gdal-dev mailing list if you have found a bug in the DXF driver or in the documentation. Attach a small DXF sample, a closed triangle as I used in my test should be perfect.

Related Question