2

I am trying to convert a .csv file to .dxf using ogr2org from the command line using the following command:

ogr2ogr -f DXF control.dxf opua.vrt

This is the .vrt file I am using:

<OGRVRTDataSource>
  <OGRVRTLayer name="opua">
    <SrcDataSource>opua.csv</SrcDataSource>
    <GeometryType>wkbPoint</GeometryType>
    <LayerSRS>WGS84</LayerSRS>
    <GeometryField encoding="PointFromColumns" x="X" y="Y" z="Z"/>
    <Field name="id" type="String"/>
    <Field name="description"/>
  </OGRVRTLayer>
</OGRVRTDataSource>

The .csv file contains these columns: point#,x,y,z,description. Using the suggestion found here (with the modified command above) produces a .dxf file with properly coordinated points points but no point number or description and two error messages:

ERROR 1: DXF layer does not support arbitrary field creation, field 'id' not created.
ERROR 1: DXF layer does not support arbitrary field creation, field 'description' not created.

I need to know what to do with these two errors to get my point numbers and descriptions to show up next to the points in the .dxf output.

3
  • According to this rather elderly response from Frank, DXF does not support arbitrary GIS attributes.
    – scruss
    Jun 4, 2016 at 20:41
  • @scruss Perhaps there is a way to include text labels as explained here. I don't know enough to determine if it would work in this instance.
    – Joshua
    Jun 4, 2016 at 21:00
  • Also, it seems possible from this response but I don't know how to apply to my script.
    – Joshua
    Jun 4, 2016 at 23:05

1 Answer 1

0

You must convert the "description" field of your csv file into label annotation following the DGAL OGR Feature Style specification http://www.gdal.org/ogr_feature_style.html. See a text label example from the section 2.1

A text label, taking the text string from the "text_string"
attribute field: "LABEL(f:"Times New Roman",s:12pt,t:{text_string})"

It is not obvious how such OGR Style string can be build from CSV so it is best to give an example.

Step 1: Have data. Save the following as "opua.csv"

point#,x,y,z,description
1,0,0,122,name_1
2,1,1,222,name_2

Step 2: Make VRT. Reusing your VRT as such:

<OGRVRTDataSource>
  <OGRVRTLayer name="opua">
    <SrcDataSource>opua.csv</SrcDataSource>
    <GeometryType>wkbPoint</GeometryType>
    <LayerSRS>WGS84</LayerSRS>
    <GeometryField encoding="PointFromColumns" x="X" y="Y" z="Z"/>
    <Field name="id" type="String"/>
    <Field name="description"/>
  </OGRVRTLayer>
</OGRVRTDataSource>

Step 3: Use some SQL magic

Test with ogrinfo

ogrinfo -dialect sqlite -sql "SELECT 'LABEL(f:\"Arial\",s:1.5g,t:'||description||')' AS OGR_STYLE, geometry FROM opua" opua.vrt

Result:

INFO: Open of `opua.vrt'
      using driver `OGR_VRT' successful.

Layer name: SELECT
Geometry: Point
Feature Count: 2
Extent: (0.000000, 0.000000) - (1.000000, 1.000000)
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"]]
Geometry Column = GEOMETRY
OGR_STYLE: String (0.0)
OGRFeature(SELECT):0
  OGR_STYLE (String) = LABEL(f:"Arial",s:1.5g,t:name_1)
  Style = LABEL(f:"Arial",s:1.5g,t:name_1)
  POINT (0 0 122)

OGRFeature(SELECT):1
  OGR_STYLE (String) = LABEL(f:"Arial",s:1.5g,t:name_2)
  Style = LABEL(f:"Arial",s:1.5g,t:name_2)
  POINT (1 1 222)

Looks good, try conversion into DXF

ogr2ogr -f DXF -dialect sqlite -sql "SELECT 'LABEL(f:\"Arial\",s:1.5g,t:'||description||')' AS OGR_STYLE, geometry FROM opua" opua.dxf opua.vrt

An error message "ERROR 1: DXF layer does not support arbitrary field creation, field 'OGR_STYLE' not created." appears but it is misleading in this context. OGR_STYLE does not get created but it is still converted into DXF style. Test it

ogrinfo opua.dxf entities
INFO: Open of `opua.dxf'
      using driver `DXF' successful.

Layer name: entities
Geometry: Unknown (any)
Feature Count: 2
Extent: (0.000000, 0.000000) - (1.000000, 1.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:AcDbMText
  ExtendedEntity (String) = (null)
  Linetype (String) = (null)
  EntityHandle (String) = 20000
  Text (String) = name_1
  Style = LABEL(f:"Arial",t:"name_1",s:1.5g,c:#000000)
  POINT (0 0 122)

OGRFeature(entities):1
  Layer (String) = 0
  SubClasses (String) = AcDbEntity:AcDbMText
  ExtendedEntity (String) = (null)
  Linetype (String) = (null)
  EntityHandle (String) = 20001
  Text (String) = name_2
  Style = LABEL(f:"Arial",t:"name_2",s:1.5g,c:#000000)
  POINT (1 1 222)

At least GDAL finds the label from DXF. You can also notice that point geometries were stored and XYZ. However, I do not know DXF and I am not sure if label is stored correctly. By opening the DXF file I could see that label tests are stored into MTEXT like here:

MTEXT
  5
20000
  8
0
100
AcDbEntity
100
AcDbMText
 40
1.5
  1
name_1
 10
0
 20
0
 30
122
  0
3
  • Unfortunately using that command did not populate labels. It also failed to generate "z" values for the points. Thank you though.
    – Joshua
    Jun 6, 2016 at 21:47
  • Sorry, I was thinking that you were dealing with GPX :). I will find how to modify the command for DXF.
    – user30184
    Jun 7, 2016 at 5:04
  • ...Using your suggestion above I was able to create labels on the coordinates but not points. However this will work as a solution because I can run my .vrt and command to create points and yours to create labels and simply merge one DXF into the other. I tried to create multiple labels with points in one go but only received a litany of errors. P.S... I also upvoted your reply which is much appreciated but my upvote will not be visible until I have 15 reputation points.
    – Joshua
    Jun 8, 2016 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.