Skip to content

save SpatialPolygonsDataFrame as dxf file

4 messages · Milan Cisty, Barry Rowlingson, Roger Bivand

#
Hello,
I would like to save SpatialPolygonsDataFrame as dxf file. Sorry if I am
doing trivial mistake, but please what is wrong on this:

p = Polygon(coords = matrix(c(1,2,2,1,1,1,2,2), ncol = 2))    #this is
square 1x1
p1= Polygons(list(p), ID=1)
p2=SpatialPolygons(list(p1))
p3=SpatialPolygonsDataFrame(p2, data = as.data.frame("1"))
writeOGR(p3, dsn = "square.dxf", layer = "entities", driver="DXF")

and last command gave me following message:
Error in writeOGR(p3, dsn = "square.dxf", layer = "entities", driver =
"DXF") : 
  Creating Name field failed
(some dxf file was produced, but it has nothing drawn in it)
Thanks,
Milan
#
On Tue, 1 Dec 2015, Milan Cisty wrote:

            
No idea, nor can I find any example of how to use the file creation 
driver. This isn't a driver in active use anywhere really. I think that 
the issue is that DXF doesn't want attributes associated with geometries - 
to judge from copying p3 to GRASS and writing with v.out.dxf. writeOGR 
needs attributes.

Roger

  
    
#
ogr2ogr on the command line does a better job. Having saved the square
as a shapefile I can do this:

$ ogr2ogr -f "DXF" output.dxf foo.shp
ERROR 1: DXF layer does not support arbitrary field creation, field
'Foo' not created.

- an error, but a file is created with features in it... ogrinfo tells me:

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

Layer name: entities
Geometry: Unknown (any)
Feature Count: 1
Extent: (1.000000, 1.000000) - (2.000000, 2.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:AcDbHatch
  ExtendedEntity (String) = (null)
  Linetype (String) = (null)
  EntityHandle (String) = 20000
  Text (String) = SOLID
  Style = BRUSH(fc:#000000)
  POLYGON ((1 1,1 2,2 2,2 1,1 1))

It even reads back into R successfully:

 > dx = readOGR("output.dxf","entities")
OGR data source with driver: DXF
Source: "output.dxf", layer: "entities"
with 1 features
It has 6 fields


there may be some magic incantation to writeOGR to do this, but until
a wizard appears you might be able to get by by saving as shapefile
(or other) and converting on the command line.

Barry
On Tue, Dec 1, 2015 at 5:41 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
#
On Tue, 1 Dec 2015, Barry Rowlingson wrote:

            
Right, thanks! Maybe gdalUtils::ogr2ogr? I got this far, but didn't check 
to see whether a file was there. So I guess writeOGR is receiving an error 
internally from the compiled code and gives up, while ogr2ogr creates the 
file and reports an error later on.
works, with the error, but the DXF file can be read by OGR - but by other 
software?

Roger