Cartopy: Python crashing with Segmentation Fault: 11 when plotting some – but not all – maps

Created on 2 May 2017  Â·  8Comments  Â·  Source: SciTools/cartopy

The description of this issue follows on from a question posted on StackOverflow (http://stackoverflow.com/questions/43649159/python-3-4-crashes-when-producing-some-but-not-all-cartopy-maps-with-segment) which received a couple of upvotes but did not receive any answers.

I am trying to install Cartopy on Mac OSX El Capitan 10.11.6 running on an iMac.

In order to avoid having lots of competing package installers and to ensure that any installed software can be easily removed without affecting the underlying operating system, I use Fink to install Unix software. All the installed software is installed at /sw and so it is relatively painless to remove all installed software should the need arise (very rare). As a result, I am keen to continue using Fink where possible.

Before installing cartopy 0.14.2, I installed the following using fink:

$ fink install libgeos3.6.1
$ fink install libproj9    # Installed version 4.9.2-3
$ fink install gdal2
$ fink install gdal2-dev

(Originally, I had used fink to install python 3.6 but more recently, I downloaded the latest python installer from python.org).

I created a virtual Python 3.6 environment using venv and activated the environment as usual. I then installed the following packages using pip:

$ pip install cython        # Successfully installed cython-0.25.2
$ pip install numpy         # Successfully installed numpy-1.12.1
$ pip install shapely       # Successfully installed shapely-1.5.17.post1
$ pip install pyshp         # Successfully installed pyshp-1.2.10
$ pip install pandas        # Successfully installed pandas-0.19.2 python-dateutil-2.6.0 pytz-2017.2 six-1.10.0
$ pip install six           # Requirement already satisfied: six in ./env34/lib/python3.4/site-packages
$ pip install matplotlib    # Successfully installed cycler-0.10.0 matplotlib-2.0.0 pyparsing-2.2.0
$ pip install pillow        # Successfully installed olefile-0.44 pillow-4.1.0
$ pip install pyepsg        # Successfully installed pyepsg-0.3.1
$ pip install scipy         # Successfully installed scipy-0.19.0
$ pip install OWSLib        # Successfully installed OWSLib-0.14.0 pyproj-1.9.5.1 requests-2.13.0
$ pip install mock          # Successfully installed mock-2.0.0 pbr-3.0.0
$ pip install nose          # Successfully installed nose-1.3.7
$ pip install pep8          # Successfully installed pep8-1.7.0

Also installed:

$ pip install jupyter    # Successfully installed MarkupSafe-1.0 appnope-0.1.0 backports-abc-0.5 bleach-2.0.0 decorator-4.0.11 entrypoints-0.2.2 html5lib-0.999999999 ipykernel-4.6.1 ipython-6.0.0 ipython-genutils-0.2.0 ipywidgets-6.0.0 jedi-0.10.2 jinja2-2.9.6 jsonschema-2.6.0 jupyter-1.0.0 jupyter-client-5.0.1 jupyter-console-5.1.0 jupyter-core-4.3.0 mistune-0.7.4 nbconvert-5.1.1 nbformat-4.3.0 notebook-5.0.0 pandocfilters-1.4.1 pexpect-4.2.1 pickleshare-0.7.4 prompt-toolkit-1.0.14 ptyprocess-0.5.1 pygments-2.2.0 pyzmq-16.0.2 qtconsole-4.3.0 simplegeneric-0.8.1 terminado-0.6 testpath-0.3 tornado-4.5.1 traitlets-4.3.2 typing-3.6.1 wcwidth-0.1.7 webencodings-0.5.1 widgetsnbextension-2.0.0

And, finally, I installed cartopy:

$ pip install --global-option=build_ext --global-option="-I/sw/opt/libgeos3.6.1/include/" --global-option="-L/sw/opt/libgeos3.6.1/lib/" cartopy # Successfully installed cartopy-0.14.2

When I opened Python at the command-line (or in a Jupyter notebook), I was able to run the following with no problems:

import matplotlib
matplotlib.use("TkAgg")
cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.Mollweide())
ax.stock_img()
plt.show()

This code also worked correctly:

import os
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from cartopy import config
import cartopy.crs as ccrs

fig = plt.figure(figsize=(8, 12))

# get the path of the file. It can be found in the repo data directory.
fname = os.path.join(config["repo_data_dir"],
                     'raster', 'sample', 'Miriam.A2012270.2050.2km.jpg'
                     )
img_extent = (-120.67660000000001, -106.32104523100001, 13.2301484511245, 30.766899999999502)
img = plt.imread(fname)

ax = plt.axes(projection=ccrs.PlateCarree())
plt.title('Hurricane Miriam from the Aqua/MODIS satellite\n'
          '2012 09/26/2012 20:50 UTC')

# set a margin around the data
ax.set_xmargin(0.05)
ax.set_ymargin(0.10)

# add the image. Because this image was a tif, the "origin" of the image is in the
# upper left corner
ax.imshow(img, origin='upper', extent=img_extent, transform=ccrs.PlateCarree())
ax.coastlines(resolution='50m', color='black', linewidth=1)

# mark a known place to help us geo-locate ourselves
ax.plot(-117.1625, 32.715, 'bo', markersize=7, transform=ccrs.Geodetic())
ax.text(-117, 33, 'San Diego', transform=ccrs.Geodetic())

plt.show()

However, the following code caused the kernel to crash:

import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import cartopy

ax = plt.axes(projection=cartopy.crs.PlateCarree())

ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.add_feature(cartopy.feature.LAKES, alpha=0.5)
ax.add_feature(cartopy.feature.RIVERS)

ax.set_extent([-20, 60, -40, 40])

plt.show()

When the above was entered line-by-line, the crashed occurred when entering ax.set_extent() method. If this line was omitted, the crash occurred when entering plt.show().

The error shown at the command line was:

Segmentation fault: 11

The error report generated for Apple included the following lines at the start:

Process:               Python [2022]
Path:                  /Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python
Identifier:            org.python.python
Version:               3.6.1 (3.6.1)
Code Type:             X86-64 (Native)
Parent Process:        bash [1446]
Responsible:           Terminal [1435]
User ID:               503

Date/Time:             2017-05-02 00:09:17.578 +0100
OS Version:            Mac OS X 10.11.6 (15G1421)
Report Version:        11
Anonymous UUID:        6282FE37-DD7B-FBCF-830C-DB51DD52364C


Time Awake Since Boot: 3200 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000

VM Regions Near 0:
--> 
    __TEXT                 0000000100000000-0000000100001000 [    4K] r-x/rwx SM=COW  /Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libgeos_c.1.dylib               0x0000000102edd393 GEOSisEmpty_r + 83
1   _ctypes.cpython-36m-darwin.so   0x00000001024cc237 ffi_call_unix64 + 79
2   _ctypes.cpython-36m-darwin.so   0x00000001024cd0ef ffi_call + 575
3   _ctypes.cpython-36m-darwin.so   0x00000001024c749f _ctypes_callproc + 879
4   _ctypes.cpython-36m-darwin.so   0x00000001024bf281 PyCFuncPtr_call + 321
5   org.python.python               0x000000010000e5b3 PyObject_Call + 99
6   org.python.python               0x000000010016d713 partial_call + 371

The above represents a straight-forward description of the installation process I used. However, I have also tried many different alternatives including:

  1. Using Python 2.7 and Python 3.4 instead of Python 3.6
  2. Using matplotlib 1.5.3 instead of 2.0.0
  3. Using libgeos 3.5.0 instead os 3.6.1
  4. Using pip to install:
   i. $ GEOS_CONFIG=/sw/opt/libgeos3.6.1/bin/geos-config pip install shapely
   ii. $ export CPLUS_INCLUDE_PATH=/sw/include/gdal2/
       $ export C_INCLUDE_PATH=/sw/include/gdal2/
       $ pip install gdal
  1. Installing cartopy 0.15.0 instead of 0.14.2.

The fact that some plots work as expected suggests that the installation process is not entirely incorrect and, as a result, the reported behaviour may represent a bug.

installation high

Most helpful comment

Try pip uninstall shapely; pip install --no-binary :all: shapely.

All 8 comments

Try pip uninstall shapely; pip install --no-binary :all: shapely.

Thanks very much for the quick reply. Unfortunately, the suggestion did not work. I tried both uninstalling and reinstalling shapely (but no improvement) and also uninstalling and reinstalling cartopy (but no improvement).

In the email that I received, the colons either side of 'all' were not displayed. Re-ran above with :all: in the command.
Initially, the installation of shapely failed with:

OSError: Could not find library geos_c or load any of its variants ['/Library/Frameworks/GEOS.framework/Versions/Current/GEOS', '/opt/local/lib/libgeos_c.dylib']

Therefore, I ran:

GEOS_CONFIG=/sw/opt/libgeos3.6.1/bin/geos-config pip install --no-binary :all: shapely

This successfully installed shapely-1.5.17.

However, when I tried to run the problem script, it did not crash but it produced the error:

OSError: Could not find lib geos_c or load any of its variants ['/Library/Frameworks/GEOS.framework/Versions/Current/GEOS', '/opt/local/lib/libgeos_c.dylib'].

I reinstalled cartopy (including paths to includes and libs) as follows:

$ pip install --global-option=build_ext --global-option="-I/sw/opt/libgeos3.6.1/include/" --global-option="-L/sw/opt/libgeos3.6.1/lib/" cartopy # Successfully installed cartopy-0.14.2

...but still got the same message. On my system, the libgeos_c.dylib is found at /sw/opt/libgeos3.6.1/lib/libgeos_c.dylib (which is in the path included in the pip install command).

So, the above seems to represent some progress - at least the kernel isn't crashing.

I added to following to my .bash_profile and reloaded:

GEOS_CONFIG="/sw/opt/libgeos3.6.1/bin/geos-config"; export GEOS_CONFIG
GEOS_DIR="/sw/opt/libgeos3.6.1"; export GEOS_DIR
PATH="$PATH:/sw/opt/libgeos3.6.1/include"; export PATH
PATH="$PATH:/sw/opt/libgeos3.6.1/lib"; export PATH
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/sw/opt/libgeos3.6.1/lib"; export LD_LIBRARY_PATH

But no effect. Still get the same OSError described above.

Installing shapely using:

pip install --no-binary :all: shapely.

...resulted in an error when trying to import cartopy in Python, namely:

OSError: Could not find lib geos_c or load any of its variants ['/Library/Frameworks/GEOS.framework/Versions/Current/GEOS', '/opt/local/lib/libgeos_c.dylib'].

The solution seemed to be to add a symbolic link in /opt/local/lib that points to the libgeos_c.dylib installed by fink.

I have posted an outline of my work-around at:

https://stackoverflow.com/questions/43649159/python-3-4-crashes-when-producing-some-but-not-all-cartopy-maps-with-segment

I think I'm having the same error. I've installed cartopy (0.14.2) from pip (shapely was also installed from pip), and geos (3.6.1) from macports. When I run one of the samples,

import cartopy.crs as ccrs
import matplotlib.pyplot as plt

ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()

plt.show()

I get a Segmentation fault: 11. When my python crashes, the macos error report says:

Application Specific Information:
Assertion failed: (0 != cs), function GEOSCoordSeq_getSize_r, file geos_ts_c.cpp, line 3991.

(full log attached).
python3.5_2017-07-19-181216_daedalus-3.txt


I followed @QuLogic 's advice and did,

pip uninstall shapely; pip install --no-binary :all: shapely

Like @lvphj, I now get an error

Failed 'CDLL(/Library/Frameworks/GEOS.framework/Versions/Current/GEOS)'

when I import cartopy, but the plot does work now!


I was able to fix the above error by install the GEOS framework downloading this link: http://www.kyngchaos.com/files/software/frameworks/GEOS_Framework-3.3.2-1.dmg

Hey,

Just writing in to say that I ran the following command and installed the geos file linked by izkelley and it resolved the segfault issue I was having:

pip uninstall shapely; pip install --no-binary :all: shapely
http://www.kyngchaos.com/files/software/frameworks/GEOS_Framework-3.3.2-1.dmg

code in question:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.offsetbox import AnchoredText


def main():
    ax = plt.axes(projection=ccrs.PlateCarree())
    ax.set_extent([80, 170, -45, 30])

    # Put a background image on for nice sea rendering.
    ax.stock_img()

    # Create a feature for States/Admin 1 regions at 1:50m from Natural Earth
    states_provinces = cfeature.NaturalEarthFeature(
        category='cultural',
        name='admin_1_states_provinces_lines',
        scale='50m',
        facecolor='none')

    SOURCE = 'Natural Earth'
    LICENSE = 'public domain'

    ax.add_feature(cfeature.LAND)
    ax.add_feature(cfeature.COASTLINE)
    ax.add_feature(states_provinces, edgecolor='gray')

    # Add a text annotation for the license information to the
    # the bottom right corner.
    text = AnchoredText(r'$\mathcircled{{c}}$ {}; license: {}'
                        ''.format(SOURCE, LICENSE),
                        loc=4, prop={'size': 12}, frameon=True)
    ax.add_artist(text)

    plt.show()


if __name__ == '__main__':
    main()

Cheers.

I will write down my experience with this issue here, hoping it helps somebody.

How I managed to install Cartopy on MacOS:

I have MacOS 10.12.6, using a virtualenv with Python 3.7.0 (so I am not using conda).

  1. Following the requirements, install required Python libraries if you don't have them yet:
pip install numpy
pip install Cython
pip install --no-binary :all: shapely
pip install pyshp
pip install six

Obtaining the following versions in my case:

numpy==1.15.3
Cython==0.29
Shapely==1.6.4.post2
pyshp==2.0.1
six==1.11.0
  1. Also, I brew installed:
brew install proj
brew install geos

Getting versions:

proj --> Rel. 5.2.0, September 15th, 2018
geos-config --version --> 3.7.0

Also add export GEOS_DIR=/usr/local/Cellar/geos/3.7.0/ to my ~/.bash_profile.

  1. Finally, pip install Cartopy:
pip install Cartopy
Cartopy==0.17.0

So in principle, brew installing geos should be enough (I read in several places about the Kyng Chaos website, which should also be fine). Apparently, the stopper for me was

pip install --no-binary :all: shapely

What is the explanation for this?

Checking Shapely's installation instructions:

If you want to build Shapely from source for compatibility with other modules that depend on GEOS (such as cartopy or osgeo.ogr) or want to use a different version of GEOS than the one included in the project wheels you should first install the GEOS library, Cython, and Numpy on your system (using apt, yum, brew, or other means) and then direct pip to ignore the binary wheels.

Conclusion: I believe this should be included somewhere in Cartopy's installation instructions for Mac users.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

choldgraf picture choldgraf  Â·  7Comments

maxnoe picture maxnoe  Â·  5Comments

chengzhuzhang picture chengzhuzhang  Â·  8Comments

QuLogic picture QuLogic  Â·  6Comments

veenstrajelmer picture veenstrajelmer  Â·  6Comments