Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Using shapely package for Python how to export the resulting shapely objects such as buffer to a DXF file?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

Shapely doesn't directly support exporting to DXF - it supports export to Well Known Text (WKT), Well Known Binary (WKB), Numpy arrays and GeoJSON objects (interoperation from the Shapely manual). As such you need a package that can transform from one of these formats to DXF.

I'd suggest OGR as the way to go for my money. The easiest method would be to simply export your shapely geometries to a GeoJSON file through Python using shapely.geometry.mapping(obj), e.g.

from shapely.geometry import mapping
import json
open("buffer.geojson", "wb").write(json.dumps(mapping(buffer_obj)))

Then simply use the ogr2ogr utility to transform the GeoJSON to a DXF file, e.g.

ogr2ogr -f DXF buffer.dxf buffer.geojson

Then, if you're keen you can look up the GDAL/OGR Python bindings and do it within a single script. Hope this helps!

share|improve this answer
    
Thanks a lot. You gave many ideas which are very useful. I would like to try OGR through Python. This a good answer. –  Developer Feb 23 '12 at 13:37
1  
As explained in gis.stackexchange.com/a/7744/390 there's a mismatch between the GeoJSON and DXF formats. Looks like you'll want to have a DXF template into which to stuff your lines. –  sgillies Feb 23 '12 at 15:50
1  
Just to mention in execution of the provided code an error come up: "write requires string/buffer". So I changed it as: write(str(mapping(buffer_obj))) and it is OK now. I use Python 2.72 BTW. –  Developer Feb 23 '12 at 17:14
    
and I also had to replace () with [] in the produced GeoJSON file to bypass another error caused using ogr2ogr.exe. Those may be changes in software or I'm missing something obvious! Anyway, with these two tricks it produced a DXF file successfully. –  Developer Feb 23 '12 at 17:33
    
However DXF file may have some issues since I could not open it by different software such as LibreCAD, SketchUp etc. –  Developer Feb 23 '12 at 17:59

BTW, if you appreciate Shapely, you may also appreciate Fiona. The Fiona example in https://gist.github.com/1886782 could be adapted to convert a shapefile to DXF.

with fiona.collection("file.shp", "r") as source:

    with fiona.collection(
            "file.dxf",
            "w",
            driver="DXF",
            schema=source.schema,
            ) as sink:

        # Do whatever you like to records
        results = some_function(source)

        sink.writerecords(results)

It coughs up a lot of warnings about mismatch between GIS simple feature and DXF data models but does write a file of entities.

share|improve this answer
    
Thanks for introducing Fiona and also thanks for your interesting package Shapely. You are surely doing good job. –  Developer Feb 23 '12 at 17:43
    
@sgillies That's awesome. If you need any further contributors please give me a shout! Also, any plans for GDAL? –  om_henners Feb 23 '12 at 22:34
    
BTW is there any pre-compiled for windows (Python 2.72) release of Fiona? –  Developer Feb 24 '12 at 2:39
    
@Developer: Not till 1.0. –  sgillies Feb 24 '12 at 14:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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