Python 3.4 crashes when producing some – but not all – Cartopy maps with segmentation fault 11

∥☆過路亽.° 提交于 2019-12-01 19:14:34
user1718097

Finally managed to make some progress so I'll summarise my solution. It may not resolve all issues but it did fix the problem I was having initially.

I posted an issue on the Cartopy GitHub page (https://github.com/SciTools/cartopy/issues/879) where QuLogic suggested a solution to stop the segmentation fault by reinstalling shapely using:

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

This indeed stopped the segmentation fault 11 but running the 'problem' code then produced an error suggesting that the geos_c couldn't be found, even though it was present. The exact error was:

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'].

It seems that the code insisted in looked for this library in a predefined location and refused to look at the locations where fink had installed the library even though I had added to location to my .bash_profile file. The solution was to create a symbolic link in that predefined location that pointed to the fink installed library. Hope that makes sense. (See Jace Browning at OSError geos_c could not be found when Installing Shapely).

So here's a summary of my entire solution, in case it helps someone else. Any suggestions for improvements would be welcomed.

  1. For the record, my setup is a standard (not admin) account on Mac OS 10.11.6 (El Capitan) running on an iMac. I do, however, also have access to an admin account if necessary.

  2. Installed version Python 3.6 using the installer provided at python.org

  3. As admin, used fink to install gdal2, gdal2-dev, libproj9, libgeos3.6.1. (Also used fink to install a version of python3.6, gdal-py36, freetype, freetype219, cairo, gsl, sqlite3 and libspatialite7 but not sure if these packages are absolutely necessary.)

  4. Python 3.6 was installed in /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6. Created a virtual enviroment (called venv36) using -m venv as follows:

At command-line:

$ mkdir <name_of_directory_for_virtual_env>
$ cd <name_of_directory_for_virtual_env>
$ /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 -m venv venv36
  1. In user account, used nano to edit the .bash_profile file to include paths to locations where fink installed libgeos3.6.1:

At command-line:

$ cd
$ nano .bash_profile

Added the following lines to the .bash_profile file and saved (ctrl-O):

GEOS_CONFIG="/sw/opt/libgeos3.6.1/bin/geos-config"; export GEOS_CONFIG
GEOS_DIR="/sw/opt/libgeos3.6.1"; export GEOS_DIR
  1. Activated the virtual environment and pip installed required packages. The pandas and jupyter packages are optional but why wouldn't you want to install them?

At the command-line:

$ cd <path_to_virtual_environment>
$ source venv36/bin/activate

(venv36) $ pip install cython
(venv36) $ pip install numpy
(venv36) $ pip install --no-binary :all: shapely
(venv36) $ pip install pyshp
(venv36) $ pip install pyproj
(venv36) $ pip install six
(venv36) $ pip install matplotlib

(venv36) $ export CPLUS_INCLUDE_PATH=/sw/include/gdal2/
(venv36) $ export C_INCLUDE_PATH=/sw/include/gdal2/
(venv36) $ pip install gdal
(venv36) $ pip install pillow
(venv36) $ pip install pyepsg
(venv36) $ pip install scipy
(venv36) $ pip install OWSLib
(venv36) $ pip install mock nose pep8
(venv36) $ pip install pandas
(venv36) $ pip install jupyter

(venv36) $ 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
  1. Finally, added a symbolic link in /opt/local/lib/ (which is where cartopy – or some other package – insists on looking for libgeos) that points to the libgeos library installed by fink (called libgeos_c.1.dylib). It may be necessary to create the /opt/local/lib pathway (or bits of it) if it doesn't already exist.

Then, at the command-line:

$ cd /opt/local/lib
$ sudo ln -s /sw/opt/libgeos3.6.1/lib/libgeos_c.1.dylib libgeos_c.dylib

That's it. In the activated virtual environment, open jupyter-notebook. Make sure the first line contains the following if you want the maps to be drawn in the notebook:

%matplotlib inline

Then add the following to the next cell:

import cartopy
import matplotlib.pyplot as plt

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 code is run, it will likely produce a warning (Failed CDLL(/Library/Frameworks/GEOS.framework/Versions/Current/GEOS)) but it should, hopefully, still produce the following image:

So, that's it. Hope it helps. Any comments or suggestions for improvements would be appreciated.

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
    
  2. 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.

  3. 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.


As user1718097 mentions above, there is an issue in Cartopy's GitHub repo with this information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!