For what its worth, I managed to build and install cartopy (and cartes) on Windows 10 with python 3.10.9 and Cartopy==0.21.1 :
- using pip
- i.e. without anaconda/conda
- without the tricky
global-options
stuff as seen here
The base for all of this is to do a custom build of https://libgeos.org
You'll need :
- powershell console and admin rights, everything below should work within the powershell
- EDIT : one can probably use a standard DOS shell using the Visual Studio DOS shell launchers (see edit 2 below)
- MSVC C++ build tools, possibily visual studio
- cmake
Procedure
- create a fresh ptyhon 3.9 venv
python -m venv venv
or
py -3.10 -m venv venv
- Install Visual Studio and MSVC c++ build tools
https://visualstudio.microsoft.com/fr/downloads/
I don't remember which versions I installed ... Retro analysis shows that this was invoked or referenced at some point subsequently to the pip install cartopy
command:
MSBuild version 17.5.1+f6fdcf537 for .NET Framework
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\ and child dirs
C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\ and child dirs
see also Cannot open include file: 'io.h': No such file or directory
Install chocolatey
Open a Powershell console "Developer PowerShell for VS 2022" and activate your venv
venv\Scripts\activate.ps1
- Install cmake
chocolatey install cmake
- Partially apply procedure indicated here https://libgeos.org/usage/download/ :
- unzip geos source zip, then go to unzipped dir
I took version 3.11.2, but take whatever suits your needs.
I choose to install into %ProgramFiles%
, again, take whatever suits you.
cd the_unzip_dir
mkdir _build
cd _build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="%ProgramFiles%\geos-3.11.2" ..
Cmake will detect the Visual Studio toolsuite and generate a bunch of MSVS project files (*.vcxproj
).
(What happens if you try without VS but minggw or else ... I don't know.)
- Build the Visual Studio projects then perform "installation" step (replaces the "make part from the original tutorial):
msbuild.exe .\ALL_BUILD.vcxproj
msbuild.exe .\INSTALL.vcxproj
- Add the installed GEOS directories to the path and env. variables.
I am not sure which one is relevant, but it might be relevant at some stage ...
(
%ProgramFiles%
above translates in windows to C:\Program Files
(with a space))
set GEOS_INCLUDE_PATH="C:\Program Files\geos-3.11.2\include"
set GEOS_LIBRARY_PATH="C:\Program Files\geos-3.11.2\bin"
set PATH="C:\Program Files\geos-3.11.2\lib";%GEOS_INCLUDE_PATH%;%GEOS_LIBRARY_PATH%;%PATH%
- Create include directory inside the python venv and put the built include files into it;
(this is for the pip install stage)
mkdir venv\libs
mkdir venv\include
xcopy /E "%ProgramFiles%\geos-3.11.2\bin" venv\libs
xcopy /E "%ProgramFiles%\geos-3.11.2\lib" venv\libs
xcopy /E "%ProgramFiles%\geos-3.11.2\include" venv\include
pip install cartopy
Building wheels for collected packages: cartopy
Building wheel for cartopy (pyproject.toml) ... done
Created wheel for cartopy: filename=Cartopy-0.21.1-cp311-cp311-win_amd64.whl size=10785690 sha256=88a2afb9224263f8269a99a2f71ecc503f105ec54166dcd9c446e3abc61477c1
Stored in directory: c:\users\<ME>\appdata\local\pip\cache\wheels\e0\34\c2\3c8ace93982cfef7b3b142d6b4f6ef30321b9768a80c115c70
Successfully built cartopy
- Now, this is for the venv runtime usage
xcopy /E "%ProgramFiles%\geos-3.11.2\bin" venv\Lib\site-packages\cartopy
xcopy /E "%ProgramFiles%\geos-3.11.2\lib" venv\Lib\site-packages\cartopy
- Check runtime imports and usage
pip install cartes
python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cartopy
>>> import cartopy.trace
>>> from cartes.crs import Lambert93
>>> dir(cartopy.trace)
['CartesianInterpolator', 'Geod', 'Interpolator', 'LineAccumulator', 'ProjError', 'SphericalInterpolator', 'Transformer', '_Testing', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__pyx_unpickle_LineAccumulator', '__spec__', '__test__', '_interpolator', 'lru_cache', 'project_linear', 're', 'sgeom', 'warnings']
>>> print(cartopy.trace.__doc__)
This module pulls together proj, GEOS and ``_crs.pyx`` to implement a function
to project a `~shapely.geometry.LinearRing` / `~shapely.geometry.LineString`.
In general, this should never be called manually, instead leaving the
processing to be done by the :class:`cartopy.crs.Projection` subclasses.
>>>print("Looks fine !")
Resulting pip freeze:
- Cartopy==0.21.1
- cartes==0.7.4
- shapely==2.0.1
- (no geos :))
pip freeze
aiohttp==3.8.4
aiosignal==1.3.1
altair==4.2.2
appdirs==1.4.4
async-timeout==4.0.2
attrs==23.1.0
beautifulsoup4==4.12.2
cartes==0.7.4
Cartopy==0.21.1
certifi==2022.12.7
charset-normalizer==3.1.0
click==8.1.3
click-plugins==1.1.1
cligj==0.7.2
colorama==0.4.6
contourpy==1.0.7
cycler==0.11.0
entrypoints==0.4
Fiona==1.9.3
fonttools==4.39.3
frozenlist==1.3.3
geopandas==0.12.2
idna==3.4
Jinja2==3.1.2
jsonschema==4.17.3
kiwisolver==1.4.4
lxml==4.9.2
markdown-it-py==2.2.0
MarkupSafe==2.1.2
matplotlib==3.7.1
mdurl==0.1.2
multidict==6.0.4
munch==2.5.0
numpy==1.24.2
packaging==23.1
pandas==2.0.0
Pillow==9.5.0
Pygments==2.15.1
pyparsing==3.0.9
pyproj==3.5.0
pyrsistent==0.19.3
pyshp==2.3.1
python-dateutil==2.8.2
pytz==2023.3
requests==2.28.2
rich==13.3.4
scipy==1.10.1
shapely==2.0.1
six==1.16.0
soupsieve==2.4.1
toolz==0.12.0
tqdm==4.65.0
tzdata==2023.3
urllib3==1.26.15
yarl==1.8.2
EDIT 1 2023-04-22:
It should be better to follow the OSGEO readme instruction for windows build, see osgeo INSTALL.md.
NB: one should adapt the Visual Studio version to whatever is appropriate. A Ninja build system command is also provided.
Quoting:
Build with CMake generator for Ninja (fast)
If you prefer the command-line, in the Visual Studio 2019 command prompt, x64 Native Tools Command Prompt for VS 2019
or x64_x86 Cross Tools Command Prompt for VS 2019
run:
cmake -S . -B _build_vs2019_ninja -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build _build_vs2019_ninja -j 16 --verbose
Build with CMake generator for MSBuild (default)
In the non-specific Command Prompt:
64-bit
cmake -S . -B _build_vs2019x64 -G "Visual Studio 16 2019" -A x64 -DCMAKE_GENERATOR_TOOLSET=host=x64
cmake --build _build_vs2019x64 --config Release -j 16 --verbose
32-bit
cmake -S . -B _build_vs2019x32 -G "Visual Studio 16 2019" -A x32 -DCMAKE_GENERATOR_TOOLSET=host=x64
cmake --build _build_vs2019x32 --config Release -j 16 --verbose
EDIT 2 : 2023-04-22
For some reason on another windows 10 machine, the "Visual Studio 2022 PowerShell for Developers" kept references to the Visual Studio x86 build tools (cl.exe), which led pip to build a x86 wheel and try to link x86 with a python x64 environement.
Several options:
- Avoid PowerShell and use the appropriate DOS launcher provided by VS install, i.e. for a VS 2022 install, it should be one of:
- x64 Native Tools Command Prompt for VS 2022
- x64_x86 Cross Tools Command Prompt for VS 2022
- x86 Native Tools Command Prompt for VS 2022
- x86_x64 Cross Tools Command Prompt for VS 2022
i.e.
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2022.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools\VC\x64_x86 Cross Tools Command Prompt for VS 2022.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools\VC\x86 Native Tools Command Prompt for VS 2022.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools\VC\x86_x64 Cross Tools Command Prompt for VS 2022.lnk"
- From a Developper PowerShell, this should do the trick
PS > Launch-VsDevShell.ps1 -Arch amd64
- Alternative to 2.
a. Launch a Developer PowerShell
b. Determine the Visual Studio instance to use:
# From a Visual Studio Developer Power Shell
# (here I have only one installed, so its pretty easy)
PS > Launch-VsDevShell.ps1 -List
The following Visual Studio installations were found:
# displayName instanceId installationVersion isPrerelease installationName installDate
- ----------- ---------- ------------------- ------------ ---------------- -----------
1 Visual Studio Community 2022 7c1743f6 17.5.33530.505 False VisualStudio/17.5.4+33530.505 2023-04-20T20:08:47Z
Enter '#' of the Visual Studio installation to launch DevShell. <Enter> to quit: :
b. setup appropriate powershell environment
PS > Enter-VsDevShell 7c1743f6 -DevCmdArguments '-arch=x64 -no_logo'
(no logo is optional ...)
The Start Menu shortcut command was on my machine
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell 7c1743f6}"