Can someone demonstrate a simple way to write geometry data-structures from shapely into shapefiles? I am particularly interested in polygons with holes and linestrings. It would also be beneficial to stay away from arcpy (so osgeo, pyshp, etc. would all be better).
Well-known binary is a good binary exchange format that can be exchanged with plenty of GIS software, including Shapely and GDAL/OGR.
This is a tiny example of the workflow with
Update: Although the poster has accepted the GDAL/OGR answer, here is a Fiona equivalent:
(Note Windows users: you have no excuse)
I've designed Fiona to work well with Shapely. Here is a very simple example of using them together to "clean" shapefile features: https://github.com/Toblerity/Fiona/blob/master/examples/with-shapely.py.
You can also write Shapely geometries by using PyShp (since the original poster also asked about PyShp).
One way would be to convert your shapely geometry to geojson (with the shapely.geometry.mapping method) and then use my modified fork of PyShp which provides a Writer method that accepts geojson geometry dictionaries when writing to a shapefile.
If you would rather rely on the main PyShp version, I have also provided a conversion function below:
Simply copy and paste the function to your own script and call on it to convert any of your shapely geometries to a pyshp compatible shape. To save them you then simply append each resulting pyshp shape to the shapefile.Writer instance's ._shapes list (for an example see the test script at the bottom of this post).
Note however: the function will NOT handle any interior polygon holes if there are any, it simply ignores them. It is certainly possible to add that functionality to the function but I have simply not bothered yet. Suggestions or edits to improve the function are welcome :)
Here is a full standalone test script: