[R-sig-Geo] save SpatialPolygonsDataFrame as dxf file

Milan Cisty milan.cisty at stuba.sk
Wed Dec 2 16:59:59 CET 2015


Thank for help. I did only one small correction, because solution from Roger
produce so called hatch objects in dxf. I used nlt = "MULTILINESTRING"
switch to prevent it. Final code is:

library(sp)
p = Polygon(coords = matrix(c(1,2,2,1,1,1,1,2,2,1), ncol = 2))
p1= Polygons(list(p), ID=1)
p2=SpatialPolygons(list(p1))
p3=SpatialPolygonsDataFrame(p2, data = as.data.frame("1"))
library(rgdal)
library(gdalUtils)
writeOGR(p3, dsn = "square1", layer = "entities", driver="GMT")  #layer MUST
be entities and final GMT is then called entities
ogr2ogr("entities.gmt", "square.dxf", "entities", "DXF", nlt =
"MULTILINESTRING")

Ends with error message but square.dxf is possible to open in AutoCAD, it is
dxf 2004 file. Only little beauty bug is that polygon is not closed. And of
course, only geometry is in final dxf file.
Thanks,
Milan

-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no] 
Sent: Tuesday, December 01, 2015 8:22 PM
To: Barry Rowlingson <b.rowlingson at lancaster.ac.uk>
Cc: Milan Cisty <milan.cisty at stuba.sk>; r-sig-geo <r-sig-geo at r-project.org>
Subject: Re: [R-sig-Geo] save SpatialPolygonsDataFrame as dxf file

On Tue, 1 Dec 2015, Barry Rowlingson wrote:

> 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.

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.

> writeOGR(p3, dsn = "square.sqlite", layer = "entities", 
> driver="SQLite")
> library(gdalUtils)
> ogr2ogr("square.sqlite", "square.dxf", "entities", "DXF") dxf <- 
> readOGR("square.dxf", "entities")

works, with the error, but the DXF file can be read by OGR - but by other
software?

Roger



>
> - 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, Milan Cisty wrote:
>>
>>> 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)
>>
>>
>> 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
>>
>>> Thanks,
>>> Milan
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>> --
>> Roger Bivand
>> Department of Economics, Norwegian School of Economics, Helleveien 
>> 30, N-5045 Bergen, Norway.
>> voice: +47 55 95 93 55; fax +47 55 95 91 00
>> e-mail: Roger.Bivand at nhh.no
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Roger Bivand
Department of Economics, Norwegian School of Economics, Helleveien 30,
N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list