mac os 安装 s2geometry + pywarps2

mac os 安装 s2geometry + pywarps2

因python项目需要使用s2geometry,安装遇到一些问题与解决的记录。

1. 安装s2geometry

如果没有cmake需要先安装cmake

brew install cmake

1.1 安装gtest

安装之前需要先安装gtest

git clone https://github.com/google/googletest.git
cd googletest
mkdir build
cd build
cmake -DCMAKE_CXX_COMPILER="c++" -DCMAKE_CXX_FLAGS="-std=c++11 -stdlib=libc++" -DCMAKE_INSTALL_PREFIX=/usr ..
make
sudo make install

1.2 安装swig

brew install swig

先安装swig后,下一步安装s2geometry会自动将pywraps2的module装到python site-package里。

1.3 安装s2geometry

安装前检查一下python3版本号,现在测试python3.7可以成功,但python3.8会提示"Undefined symbols for architecture x86_64"错误。

git clone https://github.com/google/s2geometry.git
cd s2geometry
mkdir build
cd build
## -D 参数根据自己系统安装的相关路进行定义
cmake -DOPENSSL_ROOT_DIR=/usr/local/Cellar/openssl@1.1/1.1.1g -DGTEST_ROOT=/usr/src/googletest/googletest -DCMAKE_INSTALL_PREFIX=/usr ..
make
sudo make install

OPENSSL_ROOT_DIR 和 GTEST_ROOT 可以根据自己的安装路径修改。

另外需要注意看见以下两条输出日志确认pywraps2安装成功:

-- Installing: /usr/lib/python3.7/site-packages/_pywraps2.so
-- Installing: /usr/lib/python3.7/site-packages/pywraps2.py

2. 安装python相关依赖

## 先通过brew安装相关依赖
brew install proj geos

pip install jupyter
pip install cython
pip install numpy

## 安装Cartopy不能直接安装,会报错找不到proj_api.h
## 解决方法参考自:https://github.com/SciTools/cartopy/issues/1288
export CFLAGS="-I/usr/local/include" 
export LDFLAGS="-L/usr/local/lib" 
pip install git+https://github.com/snowman2/cartopy.git
pip install matplotlib scikit-learn scipy Shapely folium geojson

3. 代码测试

import folium
import pywraps2 as s2

# create a rect in s2
region_rect = s2.S2LatLngRect(
        s2.S2LatLng.FromDegrees(48.831776, 2.222639),
        s2.S2LatLng.FromDegrees(48.902839, 2.406))

# ask s2 to create a cover of this rect
coverer = s2.S2RegionCoverer()
coverer.set_min_level(10)
coverer.set_max_level(30)
coverer.set_max_cells(60)
covering = coverer.GetCovering(region_rect)
print([c.ToToken() for c in covering])

# create a map
map_osm = folium.Map(location=[48.86, 2.3],zoom_start=12, tiles='Stamen Toner')

# get vertices from rect to draw them on map
rect_vertices = []
for i in [0, 1, 2, 3, 0]:
    vertex = region_rect.GetVertex(i)
    rect_vertices.append([vertex.lat().degrees(), vertex.lng().degrees()])
    
# draw the cells
style_function = lambda x: {'weight': 1, 'fillColor':'#eea500'}
for cellid in covering:
    cell = s2.S2Cell(cellid)
    vertices = []
    for i in range(0, 4):
        vertex = cell.GetVertex(i)
        latlng = s2.S2LatLng(vertex)
        vertices.append([latlng.lng().degrees(),
                         latlng.lat().degrees()])
        
    gj = folium.GeoJson({ "type": "Polygon", "coordinates": [vertices]}, style_function=style_function)
    gj.add_children(folium.Popup(cellid.ToToken()))
    gj.add_to(map_osm)
    
# warning PolyLine is lat,lng based while GeoJSON is not
ls = folium.PolyLine(rect_vertices, color='red', weight=2)
ls.add_children(folium.Popup("shape"))
ls.add_to(map_osm)
    
map_osm

4. 相关参考

S2 Geometry Library building
Google S2 with Python & Jupyter
How to install GTest on Mac OS X with homebrew?
S2 compatible with python3?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值