Basemap is a Python package, which never releases new update after 2014. It’s easy to use in Linux or OS X but not easy to install on Windows. It doesn’t supply setup-package for Python 3.4 or 3.5 or newer version.
For Python 2.6/2.7/3.2/3.3, there’re pre-built setup-package which are easy to use. (See SourceForge.net)
However, if we want to use Python 3.4 or higher version, it seems to be necessary for us to compile it by ourselves.

It’s a guide about compiling and installing basemap in Windows using Microsoft C/C++ compiler.

Pre-Requirements

CMake (See CMake)
Microsoft C/C++ Compiler (See Microsoft Visual Studio)
Python 3.4 or later (See Python)
Matplotlib (See Matplotlib)

Build All Dependencies and Basemap

Build GEOS

Download geos-3.5.0.tar.gz (See GEOS)
NOTE: The geos shipped with basemap is geos-3.3.0 but there are some small bugs in version 3.3.0 which cause compiling error with MSVC. So we use the newest version instead. The API provided in version 3.5.0 is the same as that in version 3.3.0

Extract source files to a directory such as E:\Temp\geos-3.5.0
Create a new directory for building such as E:\Temp\geos-3.5.0\build

Important: A required file is missed from the source tarball for some reason that I don’t know. I put it in the Appendix. Copy and save it to E:\Temp\geos-3.5.0\cmake\modules\GenerateSourceGroups.cmake

Run CMake-Gui and specify the source directory and build directory and click Configure. Modify the CMAKE_INSTALL_PREFIX to where you like, such as E:/geos  (use slash in path).
Then click Generate and Visual Studio Solution files should be generated in the build directory (E:\Temp\goes-3.5.0 in this case).

Open the geos.sln using Visual Studio. Change the build-type to ‘Release’ and generate the project ALL_BUILD
After building all sources, generate the project INSTALL and then the headers, static libraries (*.lib) and dynamic libraries (*.dll) will be installed to CMAKE_INSTALL_PREFIX (E:\goes in this case).

Build Basemap

Download basemap-1.0.7.tar.gz (See SourceForge.net)
Extract source files to a directory such as E:\Temp\basemap-1.0.7

Pre-build Preparation

* Check your Python’s distutils component to make sure the msvc9compiler.py is correct. (See Reference Link Error When Compiling and Installing Python Package in Windows
* Find the Line 79 of basemap-1.0.7\setup.py . Make sure that geos_c  is in libraries  (It’s missed in basemap-1.0.7 and you will meet Link Error when building _geoslib).

Build and Install

Run VS2013 x86 Native Tools Command Prompt (which can be found in Visual Studio Tools in Startup Menu), or VS2013 x64 Native Tools Command Prompt if you use 64-bit Python. (To tell which architecture of Python you are using, run python in command line and you will find it)
Go into the basemap directory and set proper environment variable GEOS_DIR :

Note: Do not add quotation marks (“) to the path even if there are spaces like “Program Files”.
Python always tries to find some specific version of MSVC(using Environment Variable such as VS100COMNTOOLS) and raise Error “Unable to find vcvarsall.bat”. So we need to set proper environment that points to MSVC 2013:

Then we can build and install the basemap package:

If there is no error, congratulations.

The Final Step

Run python from command line and let’s check whether Basemap is working:

You may find that there’s a strange error:

You can find _geoslib.pyd in <python-install-prefix>\lib\site-packages
But if you check the dependencies of _geoslib.pyd you will find that it needs geos_c.dll, which is not in search path of dynamic libraries (E:\geos\bin in this case).
There are two ways to solve this problem:
* Add the geos_c.dll path to your environment variable PATH.
* Or copy geos_c.dll and geos.dll to <python-install-prefix>\lib\site-packages

Now, basemap is installed properly. Enjoy using it in your codes 🙂

Appendix

GenerateSourceGroups.cmake

Compile and Install Basemap in Windows (Using MSVC)

2 comments

  • 2017-03-30 at 03:32
    Permalink

    I followed your guidance step by step and was stopped just before the final step:
    C:\Users\xjin19\Downloads\basemap-1.0.7.tar\dist\basemap-1.0.7>python setup.py i
    nstall
    C:\Users\xjin19\AppData\Local\Continuum\Anaconda3\lib\distutils\dist.py:261: Use
    rWarning: Unknown distribution option: ‘namespace_packages’
    warnings.warn(msg)
    error: [WinError 87] The parameter is incorrect
    Exception ignored in: <_io.TextIOWrapper name='’ mode=’w’ encoding=’utf-
    8′>
    OSError: [WinError 87] The parameter is incorrect

    I am using VS2015 on Win7, and my python is wrapped in Anaconda3. Please help

    Reply
    • 2017-05-08 at 00:12
      Permalink

      Sorry, I didn’t notice your comment. Have you solved this problem ? I’m sorry to say that I have no idea about this error.

      Reply

Leave a Reply

Your email address will not be published.