Contents
Ubuntu 20.04 Build S2
Build s2geometry C++ and python3
Environment
OS: Ubuntu 20.04
Python: /usr/bin/python3
Date: 2020-10-08
Now s2geometry supports Python3 officially.
Preparations
sudo apt-get install cmake libgflags-dev libgoogle-glog-dev libgtest-dev libssl-dev
sudo apt-get install python3-dev
sudo apt-get install g++
sudo apt-get install swig # to compile python3 binding
git clone https://github.com/google/s2geometry
cd s2geometry
mkdir build
cd build
cmake -DGTEST_ROOT=/usr/src/gtest ..
make # make -j4 (to speed up)
sudo make install
cmake will generate CMakeCache.txt in build dir, and CMAKE_INSTALL_PREFIX:PATH
defaults to /usr/local. So the target files like libs2.so will be installed to /usr/local/bin where is not in PATH by default.
In StackOverflow, an answer in importerror-libs2-so-cannot-open-shared-object-file-no-such-file-or-directory tells us to modify CMakeCache.txt the value of CMAKE_INSTALL_PREFIX:PATH from /usr/local to /usr . Then do the installation step again. This works.
$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pywraps2 as s2 # works here
>>> quit()
Setup a pure virtualenv for s2
install pip3 virtualenv virtualenvwrapper
sudo apt install python3-pip
pip3 install virtualenv
pip3 install virtualenvwrapper
# install path: ~/.local/bin
# warning: install path ~/.local/bin is not in PATH
Then we need modify ~/.bashrc, add following lines.
export PATH="$PATH:~/.local/bin"
export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source ~/.local/bin/virtualenvwrapper.sh
Then source ~/.bashrc we can see virtualenvwrapper prepare functions for us.
user@hostname:~$ source ~/.bashrc
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/yazheng/.virtualenvs/get_env_details
Mkvirtualenv and Install packages
mkvirtualenv s2
python -v # Python 3.8.5
pip -V # pip 20.2.3
pip install jupyter
pip install cython
pip install numpy
pip install matplotlib scikit-learn scipy Shapely folium geojson
# Preparation
sudo apt-get install libgeos-dev
sudo apt-get install libgeos++-dev
sudo apt-get install proj-bin
sudo apt-get install libproj-dev
pip install Cartopy # this requires Proj 4.9.0
[1]: pip install virtualenv
[2]: pip install virtualenvwrapper
[3]: pip install Cartopy
[4]: google-s2-python-jupyter