3

I've just started trying to use Fiona to write out shapefiles. I found the example code below, which seems to work fine

from shapely.geometry import mapping, Polygon
import fiona

# Here's an example Shapely geometry
poly = Polygon([(0, 0), (0, 1), (1, 1), (0, 0)])

# Define a polygon feature geometry with one attribute
schema = {
    'geometry': 'Polygon',
    'properties': {'id': 'int'},
}

# Write a new Shapefile
with fiona.open('my_shp2.shp', 'w', 'ESRI Shapefile', schema) as c:
    ## If there are multiple geometries, put the "for" loop here
    c.write({
        'geometry': mapping(poly),
        'properties': {'id': 123},
    })

However, if I alter this to define a projection for the output shapefile, by changing the open call to:

from fiona.crs import from_epsg
with fiona.open('my_shp2.shp', 'w', 'ESRI Shapefile', schema, crs=from_epsg(3405)) as c:

then Python hangs for a while, and then crashes with the following error:

Python(12985,0x7fff72746180) malloc: *** error for object 0x7fce4aa2c930: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[IPythonQtConsoleApp] WARNING | kernel restarted

Does anyone have any idea what's going on here? Am I doing something crazy, is my computer misconfigured somehow, or is this a bug?

I'm running this on OS X 10.8.5, and I've been using GDAL/OGR on this machine for a long time with no problems. The full stack trace, in case that helps, is available here.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
robintw
  • 4,006
  • 11
  • 40
  • 60
  • Can you use any CRS? Like 4326 (geographic, WGS84) or 32631 (projected, WGS84 UTM 31North)? I'm trying to rule out a software check that doesn't like your polygon coordinates compared to the CRS. They're wildly out of range for EPSG:3405. – mkennedy Jun 04 '14 at 22:27
  • I've just checked and it fails with both of the CRS you suggested. I assumed it wouldn't check the co-ordinates - I actually found this bug when working on a far more complex bit of code, but just simplified it down for this question. – robintw Jun 05 '14 at 10:07
  • I'm guessing--try to make a crs before using it in the open call? outcrs=from_epsg(3405) – mkennedy Jun 05 '14 at 17:12
  • Same error, unfortunately. – robintw Jun 05 '14 at 19:42
  • Works fine for me on Linux (at least to epsg:4326), so you aren't doing something crazy. Can you narrow it down to the opening or the writing, or the closing (which happens when the 'with' context handler ends). Try it with a plain c=fiona.open(....) call then see if that's crashing or the c.write... – Spacedman Jun 06 '14 at 08:01
  • I can't reproduce the crash here: Fiona 1.0.5 and GDAL 1.10 (homebrew) on OS X 10.9.3. IPythonQtConsoleApp? Are you using Fiona in QGIS? Could you try running the script in a simpler environment, like just the system Python and GDAL libraries? – sgillies Jun 06 '14 at 14:45

2 Answers2

3

This turned out to be an issue with the GDAL installation on my computer, and an incompatability between the various versions of GDAL, its supporting libraries (such as PROJ and GEOS) and Python.

Removing all traces of GDAL and its libraries, and then reinstalling them using the same compiler as used to compile and install Python (in this case, brew) fixed all of the problems.

robintw
  • 4,006
  • 11
  • 40
  • 60
  • I'm having the same problem, can you take a long at this post and see if you know what wrong? http://gis.stackexchange.com/questions/119696/writing-shapefile-with-projection-defined-crashes-fiona-part-two – Tristan Forward Oct 24 '14 at 22:38
1

Providing a Environment Variable to the GDAL data directory solved it.

import os
os.environ["GDAL_DATA"] = 'C:\Python27\Lib\site-packages\osgeo\data\gdal'

import fiona
from fiona.crs import from_epsg

schema = {'geometry': 'Point', 'properties': {'X':'float'}}
shp = fiona.open('F:/temp/asdf.shp', 'w', driver='ESRI Shapefile',crs=from_epsg(4326),
                  schema=schema)
brawn
  • 23
  • 6