【问题标题】:Is there some clashes between rasterio and shapely?rasterio 和 shapely 之间是否存在一些冲突?
【发布时间】:2021-09-09 23:11:01
【问题描述】:

我尝试使用需要 shapely 模块的 natcap.invest 模块,并在我的项目中使用 rasterio 模块。如果我只导入 natcap.invest 模块,它可以成功运行。但是如果我同时导入 natcap.invest 和 rasterio,它似乎无法工作。

[case1] 当我 import rasterio before 运行主代码时,运行主代码时报:GEOSGeom_createLinearRing_r返回一个NULL指针。

[case2]当我import rasterio 之后我运行主代码成功,导入rasterio模块时报:ImportError: DLL load failed: the given module is not found.

代码如下:

import logging
import sys
import numpy as np
import natcap.invest.hydropower.hydropower_water_yield
import natcap.invest.utils

import xarray as xr
import rasterio                      # case 1



# main code
LOGGER = logging.getLogger(__name__)
root_logger = logging.getLogger()

handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(
    fmt=natcap.invest.utils.LOG_FMT,
    datefmt='%m/%d/%Y %H:%M:%S ')
handler.setFormatter(formatter)
logging.basicConfig(level=logging.INFO, handlers=[handler])

args = {
    'biophysical_table_path': 'F:/biophysical_table_gura.csv',
    'depth_to_root_rest_layer_path': 'F:/depth_to_root_restricting_layer_gura.tif',
    'do_scarcity_and_valuation': False,
    'eto_path': 'F:/reference_ET_gura.tif',
    'lulc_path': 'F:/land_use_gura.tif',
    'pawc_path': 'F:/plant_available_water_fraction_gura.tif',
    'precipitation_path': 'F:/precipitation_gura.tif',
    'results_suffix': 'temp1',
    'seasonality_constant': '5',
    'sub_watersheds_path': '',
    'watersheds_path': 'F:/watershed_gura.shp',
    'workspace_dir': 'F:/OUTPUT',
}

if __name__ == '__main__':
    natcap.invest.hydropower.hydropower_water_yield.execute(args)     # where error of case 1 occurs.




import rasterio                   # case 2     # where error of case 2 occurs.

有没有什么方案可以让我同时成功导入和使用这两个模块?

【问题讨论】:

  • 我猜主要原因是rasterio和shapely不兼容,但我不知道如何处理。

标签: python python-import python-xarray shapely rasterio


【解决方案1】:

如果两个模块导入不同版本的 GEOS 库,可能会出现问题。确保从同一个源安装这两个模块,或者从同一个 GEOS 库的源构建它们。

文档中提供了有关如何执行此操作的更多信息:https://shapely.readthedocs.io/en/latest/project.html#installing-shapely

内置发行版

内置发行版是没有或没有的用户的唯一选择 不知道如何使用他们平台的编译器和 Python SDK,以及 对于不想打扰的用户来说,这是一个不错的选择。

Linux、OS X 和 Windows 用户可以通过 GEOS 获得 Shapely Wheels 包含在最新版本的 pip 的 Python 包索引中 (8+):

$ pip install shapely

Shapely 可通过 apt 等系统包管理工具获得, yum 和 Homebrew,也由流行的 Python 提供 Canopy 和 Anaconda 等发行版。如果你使用 Conda 包 要安装 Shapely,请务必使用 conda-forge 频道。

Windows 用户还有另一个不错的安装选项:轮子 发表于https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely。 这些可以通过指定整个 URL 使用 pip 安装。

来源分布

如果您想从源代码构建 Shapely 以与其他 依赖于 GEOS 的模块(例如 cartopy 或 osgeo.ogr)或想要 使用与项目中包含的不同版本的 GEOS 您应该首先安装 GEOS 库、Cython 和 Numpy 您的系统(使用 apt、yum、brew 或其他方式),然后直接 pip 忽略二元轮。

$ pip install shapely --no-binary shapely

如果您已将 GEOS 安装到标准位置,geos-config 程序将用于获取编译器和链接器选项。如果 geos-config 不在您的可执行文件中,可以使用 GEOS_CONFIG 环境变量,例如:

$ GEOS_CONFIG=/path/to/geos-config pip install shapely

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2014-04-25
    • 1970-01-01
    相关资源
    最近更新 更多