在Windows上安装Pyspatialite

rod*_*kov 3 python mingw visual-c++ spatialite

我在ubuntu上使用pyspatialite没有问题 - 它是完美的.但Windows令我头疼.我一直试图解决这个问题至少三天.

我正在使用Visual Studio 2010 express和mingw(gcc)作为编译器.我遇到并解决了很多问题:vcvarsall.bat,iconv.h等,但实际上我并不确切知道我做了什么.我为DLL,.h文件(标题),.a文件(afaik与mingw相关),osgeo4w二进制文件等尝试了几个环境变量.但我无法弄明白.

我在使用MSVC时收到以下错误:'gcc' failed with exit status 1并且在使用MinGW时:命令提示符处的许多警告行与此lex.gml.c, lex.kml.c, lex.geojson.c类似或相似.

我不是开发人员.我只对开源GIS世界感兴趣.有谁知道"如何在Windows上安装pyspatialite"的分步指南?

RMW*_*aos 11

After a great deal of trial-and-error, google searches, broken keyboards, various forum/chat Q&A sessions, and head injuries - not to mention having to create my own fixes/workarounds for several issues - I've finally succeeded in building and installing a 64-bit version of PySpatiaLite 3.0.1 for Windows x64. Woo-hoo!

Given how difficult it was for me to get to the end-point (several weeks!), I wanted to share this tutorial as an answer to the specific question of, "How do I build and install a 64-bit version of PySpatiaLite on Windows?" Rather than create a new self-answered question thread, I thought it better to just respond to this question about Install PySpatiaLite on Windows, assuming that it would show up for anyone searching for how to do it in a 64-bit build as well. So here goes...

注意: Markdown在这个网站上的工作方式,我无法完全控制列表的编号方案; 每个标题后它重置为1.因此,在原始教程中,我可能已编号为30,这里我必须注意它显示为Building xyz下的第5步,等等.你能做什么?


HOW-TO:在WINDOWS上安装64位PYSPATIALITE

40个非常容易遵循的步骤 - 不,真的!


这里有很多步骤,但这只是因为我把它们分解成了最小的组件; 所以不要感到不知所措.在高级别,你真的只做了5件事:

  1. 下载所有必要的来源
  2. 设置构建环境(您可能已经完成了此操作)
  3. 构建和安装依赖库
  4. 构建和安装PySpatiaLite
  5. Testing your installation

That's it. Not so difficult, really, is it? Oh yes it was!! But hopefully, not so difficult for you following these steps. For many of you, the added detail in these steps will seem like overkill, particularly if you've built from source code before (I hadn't - really!), but I make no assumptions about the skill level or expertise of the audience; so this How-To is written so that a relative beginner (like me!) should be able to follow it.

NOTE: Pay attention to the notes throughout this document, as they often point out some gotchas or things that would not otherwise be obvious at first glance.

注意:请尽可能使用最新的稳定版本,而不是测试版或夜间快照.有时候,除了获得最前沿的测试版(或者如果你认识人的话,还有alpha版),你别无选择.例如,您将需要最新的稳定版GEOS 每晚构建来解决问题.

注意:当您访问下面的某些链接时,您可能会注意到您要构建的其他库的一些预编译二进制文件.我强烈建议你不要使用这些.您无法确定它们所构建的环境,并且如果您在自己的环境中构建它们,那么事情会更好/更安全/更确定.Gettext的例外.

ME:代表"我的环境"作为比较依据

要求:

  • Microsoft Windows x64操作系统(Duh!我们在这做什么?)
    ME: MS Windows 7 Home x64

  • MS Visual Studio Express C++ (MSVC) 2008
    ME: I downloaded and mounted a virtual ISO image rather than use the web-based installer
    NOTE: Choose "vcsetup.exe" (C++) NOT "vcssetup.exe" (C#) - notice the extra 's'? You'd think they'd make it a little more obvious, eh? The full version of MSVC will work equally as well, but like me, I assume free is of great value to you.
    NOTE: Might work with MSVC 2010, but I strongly suggest sticking with same version that Python was compiled on because sometimes it can be a bit prickly, which is why I suggest building with MSVC rather than mingw-w64 or Cygwin.

  • MS软件开发工具包(SDK)7.0 amd64 .NET Framework 3.5
    ME:此处还下载了ISO映像.
    注意:可能使用SDK 7.1 amd64 .NET Framework 4.0(MSVC 2010),但请参阅上面的注释

  • Python 2.7 x64
    ME: Python 2.7.3 x64(预编译的Win x64安装)
    注意:可能适用于2.5+版本,包括3.x

  • 最新的GNU iconv源代码
    ME: libiconv 1.9.2

  • 最新的Proj.4源代码
    ME: libproj 4.8.0

  • 最新的GEOS源代码
    ME: libgeos 3.3.5(svn_trunk)+ 2012-07-28夜间快照
    注:也可以从此页面下载其中一个夜间快照; 我们需要从中复制一个丢失的目录,但是我们不想用它来构建库本身.

  • 最新的GNU Gettext运行时BINARY
    ME: Gettext 0.18.1.1-2
    注意:由于与iconv的循环依赖,我们在这里使用二进制而不是源代码构建; 你可以解决它,但这更容易/更快/更简单/更少压力诱导.

  • Latest PySpatialLite Package
    ME: PySpatiaLite 3.0.1
    NOTE: As of this writing, 3.0.1 is the latest version available; the specific quirks getting this version to work probably won't all match up with any other version, but much of the steps necessary to compile the dependency libs should still apply.

  • Notepad++ or other fully-featured editor app.
    NOTE: Sure, MS NotePad will do in a pinch, but it's going to make this process a whole lot harder. I recommend something a little more robust, and Notepad++ happens to be my favorite.

Setting up the Build Environment:

  1. Install MSVC 2008, and run Windows Update (sometimes 2 or more times to get everything), then when no updates remain...

  2. Install SDK 7.0, and run Windows Update again (and again, etc., etc., ad nauseum), then when no updates remain...

  3. Open a Windows folder (or Windows Explorer) and navigate to 'C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC'

  4. Edit 'vcvarsall.bat'

  5. Locate the code that look like this at the line numbers indicated:

    Line 19: if not exist "%~dp0bin\amd64\vcvarsamd64.bat" goto missing  
    Line 20: call "%~dp0bin\amd64\vcvarsamd64.bat"  
    
    Run Code Online (Sandbox Code Playgroud)
  6. The paths in these lines are just plain wrong. Don't ask me why. I guess Microsoft had their heads up their collective arses that day, but if you just remove the '\amd64' and change the file name to 'vcvars64.bat' in each line it should be ok. The end result should look like this:

    Line 19: if not exist "%~dp0bin\vcvars64.bat" goto missing  
    Line 20: call "%~dp0bin\vcvars64.bat"  
    
    Run Code Online (Sandbox Code Playgroud)

    NOTE: You can test by opening a command window (START --> RUN --> type 'cmd' --> OK) and running the following command:

    c:\> vcvarsall x64  
    
    Run Code Online (Sandbox Code Playgroud)

    You should get a series of output stating that it's 'Setting the environment for using Microsoft Visual Studio 2008 x64 tools'. Go ahead and close this window.

  7. Now let's test your SDK 7.0 environment. Click START --> ALL PROGRAMS --> Microsoft Windows SDK 7.0 --> CMD Shell. If all is working properly, you should have a Command line window with yellow text. That indicates a 64-bit environment. Perfect! You can close this window for now.
    NOTE: You might want to create a shortcut to the SDK 7.0 CMD Shell - we'll be using it quite often throughout this process.

  8. Decide where you ultimately want to install the dependency libraries. Go ahead and create the folder now if it doesn't already exist. Remember the path you chose, as we'll be referring to it often. For simplicity, we'll call this '\Install' or sometimes 'C:\Install' in example code.

  9. Decide where you will keep temporary build files during this install process. This should not be the same location as your '\Install' folder. We'll refer to this as '\Temp' or 'C:\Temp' in example code.

  10. Extract the GNU Gettext binary to '\Temp', which should result in a folder called '\Temp\Gettext-0.18.1.1-2' or similar.

  11. Copy libintl.dll from '\Temp\Gettext-0.18.1.1-2\bin' to '\Install\bin' (you'll have to create '\bin' yourself), or just copy and paste all the Gettext ('\bin', ''\lib', etc.) sub- folders into '\Install'. Either way works fine.

  12. Navigate to '\Install\bin', make a copy of 'libintl.dll' and call it 'intl.dll'. This is necessary for some libraries that look for it under a different name. Now we're ready to proceed with the library builds...

Building the Dependency Libraries:

  1. Extract each of the libraries (Proj.4, GEOS, and iconv) into their own folders in '\Temp'. I suggest renaming each folder to something short but descriptive (e.g. '\Temp\Proj4') as typing '\Temp\Proj-4.8.0' (or longer!) over and over again gets tiresome.

Building iconv:

  1. Open your SDK CMD Shell, and change directories to your iconv folder (e.g. 'cd c:\temp\iconv').

  2. Type either of the following two commands:

    Single-threaded static lib: (recommended)  
    c:\Temp\iconv\> nmake install /f makefile.msvc MFLAGS=-ML PREFIX=c:\Install  
    
    Multi-threaded static lib: (so far it's working for me)  
    c:\Temp\iconv\> nmake install /f makefile.msvc MFLAGS=-MT PREFIX=c:\Install  
    
    Run Code Online (Sandbox Code Playgroud)

    Replace 'c:\Install' with your '\Install' path. After a long time of doing some arcane c-style linking and such, the process will eventually complete, and assuming you see no errors that break the build process (e.g. 'cl.exe exited with status 2') - it's relatively safe to ignore the numerous warnings - you're ready to move on.
    NOTE: If you prefer you can build and install the libraries in two separate steps by dropping 'install' right after 'nmake' then rerun the command as above afterwards, but what's the point? If the build portion fails, it won't install anyway.

Building Proj.4:

  1. Still in the CMD Shell, change directories to your '\Temp\Proj4' folder (e.g. 'cd c:\temp\proj4').

  2. Type the following command:

    c:\Temp\proj4\> nmake install-all /f makefile.vc INSTDIR=c:\Install  
    
    Run Code Online (Sandbox Code Playgroud)

    Replace 'c:\Install' with your '\Install' path.
    NOTE: The extension on the makefile here is now '.vc' not '.msvc'. Either seems to be common practice.
    NOTE: Notice that here it's 'nmake install-all ...', which is more common than 'nmake install ...', particularly because 'install' is a built-in nmake command that shouldn't be overridden.
    NOTE: This is the only instance I've seen the use of 'INSTDIR=' rather than 'PREFIX='. It's not wrong per se, just not the standard convention that I've seen used most often.
    Don't worry about any messages such as, 'The system cannot find the file specified. 0 file(s) copied.' The install script is pretty basic and tries to install all .DLL and other files even if you didn't choose to compile them.
    After a pretty short time, the process will complete (again, errors bad, warnings ok), and you can proceed.

Building GEOS:

  1. This one is going to take a little extra time and effort. Remember that you downloaded both the latest stable release and a nightly snapshop? If you haven't already, go ahead and extract both to separate folders (e.g. '\Temp\GEOS' and '\Temp\GEOSnap').

  2. Copy the folder '\Temp\GEOSnap\src\triangulate' to '\Temp\GEOS\src'. For some reason, as of this writing, '\triangulate' is missing from the latest stable build. That's all we needed from '\GEOSnap'; moving on...

  3. Download GEOS\makefile.vc patch to '\Temp\GEOS\makefile.vc'. - Say, 'Thank you.'

  4. Download GEOS\src\makefile.vc patch to '\Temp\GEOS\src\makefile.vc'. - You're welcome.
    NOTE: Don't get these two mixed up! If you do, it will go badly for you.
    In addition to implementing a fix from GEOS Trac #574, these two makefiles for MSVC add code to perform basic install and uninstall functions (e.g. 'nmake install-all ...'). Otherwise, you would need to either manually copy over all the files (there's a bunch all over the place!) or use another build method such as CMake, which is currently a bit dodgey.

  5. Navigate to '\Temp\GEOS\src', edit 'dirlist.mk', and add the following code at the line number indicated:

    Line 45: triangulate \  
    
    Run Code Online (Sandbox Code Playgroud)

    This entry should fall below 'simplify \' and above 'util'. Save and close.

  6. Open your CMD Shell, and change directories to your '\Temp\GEOS' folder (e.g. 'cd c:\Temp\GEOS').

  7. Type the following command:

    c:\Temp\GEOS\> nmake install-all /f makefile.vc PREFIX=c:\Install  
    
    Run Code Online (Sandbox Code Playgroud)

    Replace 'c:\Install' to your '\Install' path. After a long, Long, LONG time, the build process will complete. Assuming no build-breaking errors, you should be notified that GEOS was installed in '\Install'.
    That's all for the dependency libs; now you're (finally) ready to install PySpatiaLite!

Building and Installing PySpatiaLite:

  1. Extract PySpatiaLite to '\Temp' in its own folder (e.g. '\Temp\PSL').

  2. Navigate to '\Temp\PSL', edit the file called 'setup.py', and modify the following code at the line numbers indicated:

    Line 45: include_dirs = ['c:\Install\include']  
    Line 46: library_dirs = ['c:\Install\libs']  
    Line 47: libraries = ['geos','geos_c','proj','iconv']  
    Line 48: runtime_library_dirs = ['c:\Install\bin']  
    
    Run Code Online (Sandbox Code Playgroud)

    Replace the 'c:\Install' portion above with your own '\Install' path, but keep the '\include', '\libs', and '\bin' portions instact. Save and close the file.
    NOTE: You're only adding ",'iconv'" to line 47.

  3. Open your CMD Shell and change directories to your PySpatiaLite folder (e.g. 'cd c:\Temp\PSL').

  4. Type the following command:

    c:\Temp\PSL\> python setup.py build  
    
    Run Code Online (Sandbox Code Playgroud)

    Wait for it to download the spatialite amalgamation, unload the files it requires, begin the build process, and eventually fail.
    Yep. That's right. The build will likely fail (unless Loki has fixed the issue by the time you read this).

  5. If and when it does fail, take a look at the console output about 14 lines above the failure message, and you should see buried in that unholy mess of code something like:

    -DVERSION = "3.0.1"  
    
    Run Code Online (Sandbox Code Playgroud)

    Make a note of that number; you're going to need it in a couple places going forward.

  6. Minimize CMD Shell, navigate to '\Temp\PSL\amalgamation', and edit the file labeled spatialite.c. Don't panic! It's big, it's bad, it's scary, but we're only going to make a small change: a kludgey but effective workaround.

  7. Add the following code at the line number indicated:

    Line 54: #define VERSION "3.0.1"  
    
    Run Code Online (Sandbox Code Playgroud)

    NOTE: Replace "3.0.1" with the number you saw in step 30 (5 here) above, and make sure you use double quotes (") NOT single quotes ('), or it will fail. Yes, I found this out the hard way, unlike you c gurus out there who know better.
    If you're wondering why we had to wait for it to fail before making this change, it's because the file didn't exist on your system until the amalgamation was downloaded during the first build attempt. So there.

  8. Now re-run the command in step 31 above, and the build should complete successfully.

  9. Now run the following command:

    c:\Temp\PSL\> python setup.py install  
    
    Run Code Online (Sandbox Code Playgroud)

    And whala! Assuming no more bothersome errors, you're finally done with the build/install process. Have a cookie and pat yourself on the back. You can delete all those '\Temp' folders now if you choose. Now for some tests to make sure everything is working properly...

Testing Your Install of PySpatiaLite x64:

  1. Open a CMD window and change directories to '..\Python27\Lib\site-packages\pyspatialite\test' (e.g. 'cd c:\python27...\test').

  2. Do not start the Python CLI. Type each of these commands in succession:

    c:\python27\...\test\> python dbapi.py  
    c:\python27\...\test\> python dump.py  
    c:\python27\...\test\> python factory.py  
    c:\python27\...\test\> python hooks.py  
    c:\python27\...\test\> python regression.py  
    c:\python27\...\test\> python spatial.py  
    c:\python27\...\test\> python transactions.py  
    c:\python27\...\test\> python types.py  
    c:\python27\...\test\> python userfunctions.py  
    
    Run Code Online (Sandbox Code Playgroud)

    After each test, you should receive a report that it ran xx number of tests in x.xxx seconds, and whether the tests passed (OK) or failed. If they all pass, you're golden.
    NOTE: 'spatial.py' may fail as a result of it expecting the SpatiaLite version to be '2.3.1', whereas yours is likely '3.0.1'. Not to worry, a simple edit of the file will fix this and allow you to run the test.

  3. Edit 'spatial.py' and change "2.3.1" to the verison you actually installed (remember that number from step 30 (Building PySpatiaLite - step 5) above?) at the line number indicated:

    Line 18: self.assertEqual(row[0], "3.0.1")  
    
    Run Code Online (Sandbox Code Playgroud)

    Run the 'spatial.py' test again, and it should work fine now. Not really sure why they'd want to check for the pyspatialite version in this test anyway ... moving on!

    The built-in tests are nice, but how about a more real-world-oriented test? You know, one where you actually create, populate, and poll and manipulate data in a database? Yeah, that sounds just like the ticket!

  4. Scroll to the bottom of the Language Bindings: Python section of the online Spatialite Cookbook, and copy the 'spatialite_sample.py' code into a new Python file (e.g. 'psl_sample.py').

  5. Before you save, close, and run this test, check for a possible copy/paste error. Make doubly sure the code reads exactly like this at the line numbers indicated:

    Line 28: sql = "SELECT AddGeometryColumn('test_pt', "  
    Line 29: sql += "'geom', 4326, 'POINT', 'XY')"  
    
    Run Code Online (Sandbox Code Playgroud)

    I'm guessing somethings getting lost in the translation from HTML code to Python file, because the end space and quote on line 28 kept getting dropped to the beginning of line 29 every time I cut/paste this code, and from more than one source, too. Easy fix, tho.

  6. Now save, close, and run it. Do I really have to show you how to do that? C'mon! For a hint, look at step 35 (2 here). ;-)
    Assuming all the tests passed, both the built-in and the cookbook sample, then all indications point to you having a solid 64-bit installation of PySpatiaLite.

  7. Celebrate! (needed to round this out to an even 40 ;-) )

I hope this "little" HOW-TO was useful to you. Please feel free to provide feedback/updates/corrections.

Best regards,

-RMWChaos, Your Friendly Neighborhood Evil Overlord

http://www.thedarklands.com