[GIS] how install shapely on windows so it can be imported from matlab

geosMATLABpythonshapelywindows

i'm on windows 7/64. shapely doesn't offer windows installers on their pypi page, even though they seem to have recently (github issues 104 and 109 — i don't have the rep to post links. their pypi page, and their README say to use chris gohlke's unofficial binaries. i'm not clear on the reason — something like pypi can't install dll's, so you have to make an installer by hand if your code depends on a dll, but making the installer is a pain because you need ms visual studio or something?

anyway, gohlke's binary works fine for me in winpython (2.7), but not when i try to import using matlab's new python bridge. shapely is the only python library on windows that is giving me this trouble in matlab — i can use openCV, numpy, scipy, scikit's, mosek, picos, PIL, future, all fine. it's also the only library for which i need gohlke's installer. so i think there is a connection.

when i py.importlib.import_module('shapely') in matlab, i get this:

PyException with properties:

ExceptionObject: [1x3 py.tuple]
     identifier: 'MATLAB:Python:PyException'
        message: 'Python Error: [Error 1114] A dynamic link library (DLL) initialization routine failed'
          cause: {}
          stack: [1x1 struct]

a popup also comes up:

Microsoft Visual C++ Runtime Library
R6034 "an application has made an attempt to load the c runtime library incorrectly"

googling this, i get msdn article ms235560

Visual Studio 2008. An application has made an attempt to load the C
runtime library without using a manifest. This is an unsupported way
to load Visual C++ DLLs. You need to modify your application to build
with a manifest.
Applications must use a manifest to load the C runtime library. For
more information, see Visual C++ Libraries as Shared Side-by-Side
Assemblies and Manifest Generation in Visual Studio. … To correct
this error Rebuild your application to include a manifest. Building an
application with Visual Studio automatically puts the manifest into
the resulting .exe or .dll file. If you are building at the command
line, use the mt.exe tool to add the manifest as a resource. Use
resource ID 1 if you build an .exe, and resource ID 2 if you build a
.dll. For more information, see How to: Embed a Manifest Inside a
C/C++ Application."

this seems to relate to chris gohlke's comment 35357436_23108273 at stack overflow question 23108273.

here are some more things i've tried.

in matlab:

>> x=py.ctypes.util.find_library('geos_c')

x =

  Python NoneType with no properties.

    None

but same in python!

>>> from ctypes.util import find_library
>>> print find_library('geos_c')
None

however, i can use shapely from python:

>>> from shapely.geometry import MultiPolygon, Polygon, Point
>>> Point()
<shapely.geometry.point.Point object at 0x000000000399F2E8>

in matlab:

>> x=py.ctypes.CDLL('C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\site-packages\shapely\DLLs\geos_c.dll')

x =

  Python CDLL with no properties.

    <CDLL 'C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\site-packages\shapely\DLLs\geos_c.dll',
handle e3f60000 at 7078b588>

>> x.GEOSversion()
No appropriate method, property, or field 'GEOSversion' for class
'py.ctypes.CDLL'.

>> x.initGEOS()
No appropriate method, property, or field 'initGEOS' for class 'py.ctypes.CDLL'.

>> x=py.ctypes.CDLL('C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\site-packages\shapely\DLLs\geos_c.dll').GEOSversion()
No appropriate method, property, or field 'GEOSversion' for class
'py.ctypes.CDLL'.

Error: Unexpected MATLAB expression.

>> x=py.ctypes.CDLL('C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\site-packages\shapely\DLLs\geos_c.dll').initGEOS()
No appropriate method, property, or field 'initGEOS' for class 'py.ctypes.CDLL'.

Error: Unexpected MATLAB expression.

>> py.dir(x)

ans =

  Python list with no properties.

    ['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__',
'__format__', '__getattr__', '__getattribute__', '__getitem__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__', '_func_flags_', '_func_restype_',
'_handle', '_name']

in python:

>>> from ctypes import CDLL
>>> x=CDLL("C:\\Users\\nlab\\Downloads\\WinPython-64bit-2.7.9.5\\python-2.7.9.am
d64\\Lib\\site-packages\\shapely\\DLLs\\geos_c.dll")
>>> print x
<CDLL 'C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\Lib\si
te-packages\shapely\DLLs\geos_c.dll', handle e3f60000 at 2292cc0>
>>> x.GEOSversion()
-469489536
>>> x.initGEOS()
36606784
>>> dir(x)
['GEOSversion', '_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__', '
__format__', '__getattr__', '__getattribute__', '__getitem__', '__hash__', '__in
it__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__se
tattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_func_fla
gs_', '_func_restype_', '_handle', '_name', 'initGEOS']

a search for geos_c.dll just shows shapely's directory in my python's site-packages.

Best Answer

we want to use matlab's msvcr. in shapely, replacing CDLL(find_library('c')) with CDLL('C:\\Program Files\\MATLAB\\R2015a\\bin\\win64\\msvcr100.dll') makes it all work. this answer seems to be a method that would determine that path automatically, appropriate for wherever you are invoking shapely from.