Friday, 25 March 2016

Python: How to export Shapely objects to DXF file


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



Answer



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!


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...