[GIS] How to extract style data from DXF when converting to geoJSON

dxfgdalgeojsonleafletogr2ogr

I am using ogr2ogr (GDAL) to convert DXF files (from autoCAD) to geoJSONS for usage inside Leaflet.js
The issue is that a lot of information, like line stroke size and colors are not kept during the conversion.

I would like to extract them, so that I can use them later with the geoJSON, even if I have to implement that manually. How can I do this ?

My only option for the moment is parsing myself the DXF and look for colors but there must be a better way

Best Answer

Have a look at the attributes. GDAL can read the styles from DXF:

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

Layer name: entities
Geometry: Unknown (any)
Feature Count: 4036
Extent: (-174.786500, -1163.622000) - (1769.214000, 204.378100)
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) = PAPER
  SubClasses (String) = (null)
  ExtendedEntity (String) = (null)
  Linetype (String) = CONTINUOUS
  EntityHandle (String) = (null)
  Text (String) = (null)
  Style = PEN(c:#00ffff,p:"1.0g")
  LINESTRING (1644.348 -1051.956 0,1763.214 -1051.956 0)

However, styles are not written into GeoJSON automatically but you can do it with the -sql parameter:

ogr2ogr -f geojson -dialect sqlite -sql "select geometry, ogr_style from entities" style.json jcsample.dxf

Check the result:

ogrinfo style.json -ro -al 
INFO: Open of `style.json'
      using driver `GeoJSON' successful.

Layer name: OGRGeoJSON
Geometry: Unknown (any)
Feature Count: 4036
Extent: (-174.786500, -1163.622000) - (1769.214000, 204.378100)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
OGR_STYLE: String (0.0)
OGRFeature(OGRGeoJSON):0
  OGR_STYLE (String) = PEN(c:#00ffff,p:"1.0g")
  Style = PEN(c:#00ffff,p:"1.0g")
  LINESTRING (1644.348 -1051.956 0,1763.214 -1051.956 0)

It is somehow odd that now Style goes both into OGR_STYLE and into Style, but at least you can get those. OGR_STYLE is documented in http://www.gdal.org/ogr_feature_style.html.