2

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

4

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.

  • I could extract all the info I needed by using ogr2ogr -f geojson -dialect sqlite -sql "select , ogr_style from entities". What I do not understand, is why by simply putting "" I don't get all the columns ? – Rayjax Sep 11 '15 at 15:28
  • Probably because only a few vector formats support styles. MapInfo, dxf, KML, JUMP jml, may be others. GeoJSON does not and for most users styles would be useless. In GIS styles tend to be per layer not per feature. BTW, perhaps KML would be better for you. – user30184 Sep 11 '15 at 15:43
  • Very interesting. KML does support styles ? Anyway in Leaflet, importing KMLs is done through Omnivore plugin, which I assume converts it to geoJSON to be readed by the map, so I was trying to skip one step by having geoJSON directly. I ended up using you solution (plus the projection parameters you told me in the other question) and it works well, the geoJSON contains the hexa, so I extract it manually upon map display and apply it to the layers – Rayjax Sep 11 '15 at 17:39

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.