windows环境下安装Python的Rtree包

时间:2022-06-23 01:47:54

python提供的一个第三方包Rtree包能够实现R树查询、删除、增添的各种操作。然而版主在windows环境 (win 10, python3.5)下安装Rtree包的时候出现了问题。直接在cmd中输入pip install Rtree后,会出现一下错误:

Collecting Rtree
  Using cached Rtree-0.8.2.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\dellpc\AppData\Local\Temp\pip-build-zq00u5gn\Rtree\setup.py", line 4, in <module>
        import rtree
      File "C:\Users\dellpc\AppData\Local\Temp\pip-build-zq00u5gn\Rtree\rtree\__init__.py", line 1, in <module>
        from .index import Rtree
      File "C:\Users\dellpc\AppData\Local\Temp\pip-build-zq00u5gn\Rtree\rtree\index.py", line 6, in <module>
        from . import core
      File "C:\Users\dellpc\AppData\Local\Temp\pip-build-zq00u5gn\Rtree\rtree\core.py", line 101, in <module>
        raise OSError("could not find or load spatialindex_c.dll")
    OSError: could not find or load spatialindex_c.dll

如图所示:

 windows环境下安装Python的Rtree包

当然,这个错误并不是版主没有使用pip3导致的,错误提示是少了spatialindex_c.dll这个文件。于是版主顺藤摸瓜,发现Rtree包是基于libspatialindex开发的,在安装Rtree之前必须先安装libspatialindex。关于libspatialindex,除了官网的英文外,这里有一个中文翻译过来的介绍:http://blog.csdn.net/ljz2173/article/details/46009573,不再多说。

于是版主又傻乎乎的去安装libspatialindex,但是开发这个libspatialindex的大神似乎并不喜欢windows,官网上只给了十分简要的安装说明,不过基本等于没说呀,大概意思是自己去找个编译器编译后使用吧。。。版主又借助在海外的优势,四处google怎样在windows下安装libspatialindex,找了半天全是国外网友的疑问,却没人回答......看来大部分人都是用的Linux,版主这么小白还用windows简直就是错误......国内网友似乎也无人问津这个问题。找了半天似乎只有国外一个大神成功了,不过我按照他的步骤试了很久,还是不行。这里还是把地址贴出来,仅供参考:

http://lists.gispython.org/pipermail/spatialindex/2012-December/000336.html,有兴趣有时间的同学可以一试windows环境下安装Python的Rtree包

其实!其实!其实!哪里会有那么多麻烦事...发现了另一个Rtree官网的介绍,这里 http://toblerity.org/rtree/install.html#windows,看最下边win那里,“The Windows DLLs of libspatialindex are pre-compiled in windows installers that are available from PyPI. Installation on Windows is as easy as:” 

windows环境下安装Python的Rtree包

这明明就是说win版本的,这个Dll文件已经预编译进去了呀!!!根本不需要自己去装libspatialindex......

下边正式说安装步骤,废话有点多了:

1. 这里 http://www.lfd.uci.edu/~gohlke/pythonlibs/#rtree 下载对应版本的Rtree的whl安装包,注意是python2.7还是3.5,注意电脑是32还是64位。可以放在电脑任意位置

2. 确保电脑里已经安装了python中wheel这个包,没有的话这个可以直接在cmd中输入pip install wheel安装

3. 打开cmd,输入pip install E:\软件安装包\Rtree-0.8.2-cp35-cp35m-win_amd64.whl,这个E盘是我存放Rtree的whl安装包的位置,自己安装的时候根据具体情况改一下即可。

然后两秒安装成功。

windows环境下安装Python的Rtree包

此时版主不知是该笑还是该哭,只觉得自己笨的要死...

不过,至今也没找到怎样正确在windows下安装libspatialindex的方法,还请大神指教。

顺便啰嗦一句吧,Linux安装libspatialindex的步骤大概如下:

https://github.com/libspatialindex/libspatialindex/wiki/1.-Getting-Started

鉴于国内github可能被墙,顺便也贴过来吧:

This tutorial will help you to get started with libspatialindex using C++ on linux. The following code is run on Ubuntu 12. If you are using windows the installation may be different. First install some prerequisites please enter the following into terminal. You may very well already have these installed.

sudo apt-get update
sudo apt-get install curl
sudo apt-get install g++
sudo apt-get install make

Now we download and install the library. It doesn't matter what directory you download this in. Please note the version number, you can check if there are more recent versions in the download page here http://download.osgeo.org/libspatialindex/ . Now enter the following into your terminal:

curl -L http://download.osgeo.org/libspatialindex/spatialindex-src-1.8.5.tar.gz | tar xz
cd spatialindex-src-1.8.5
./configure
make
sudo make install
sudo ldconfig

libspatialindex should now be installed on your system. Let's try to make a small c++ program to test this. Create the file tutorial1.cpp somewhere and enter the following:

#include <iostream>
#include <spatialindex/capi/sidx_api.h>
using namespace std;
using namespace SpatialIndex;

int main(int argc, char* argv[])
{
char* pszVersion = SIDX_Version();
fprintf(stdout, "libspatialindex version: %s\n", pszVersion);
fflush(stdout);
free(pszVersion);
}

Now let's compile the code. Type the following into the console. Please note -std=c++0x makes it compile into C++11, although not required here it will be used in later examples.

g++ -std=c++0x tutorial1.cpp -lspatialindex_c -lspatialindex -o tutorial1

Now run the program:

./tutorial1

and it should output the following:

libspatialindex version: 1.8.5