4

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
1

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.

  • Preliminary support for HATCH was added 4 years ago trac.osgeo.org/gdal/log/trunk/gdal/ogr/ogrsf_frmts/dxf/…. The history of this and other DXF driver sources for sure reveals how and when the interpretation of closed polyline has been changed. – user30184 Jan 26 '15 at 13:25
  • Thanks! Using HATCH instead of closed polylines threw the right POLYGON output. However, I should report the documentation mismatch to gdal-dev – aibars Jan 26 '15 at 15:31
1

With the latest GDAL dev build from GisInternals you can now transform dxf into shape and closed lines into polygons in one step:

ogr2ogr -nlt polygon testpoly.shp test.dxf
ogrinfo testpoly.shp
INFO: Open of `testpoly.shp'
      using driver `ESRI Shapefile' successful.
1: testpoly (Polygon)

This will find its way into GDAL 2.0, not 1.11.2 which is coming up soon.

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.