based on untested ideas or techniques
Development log of whatever experiments, ideas and projects I come across.

QGIS in OSX

Bruno Santos on November 9th, 2009

And after several(million) tries I finally was able to load a Vector Layer with the python Bindings.

So let me give you some background on the problem:

I was trying to run qgis 1.4.0 (current development version). The app runs just fine.
Python Bindings failed to load a vector layer and the only error message was “Failed to load”.

Yeah, I noticed you failed, but can you please let me know why?

Most post I found about that problem says the qgis_prefx path is wrongly set up. Mine wasn’t.
In my /etc/profile I have

export QGISHOME=/Users/pheres/apps/qgis1.4.0.app/Contents/MacOS

and in my code

from qgis.core import *
import os
import sys
import pdb

prefix = os.getenv("QGISHOME")
QgsApplication.setPrefixPath(prefix, True)
QgsApplication.initQgis()
vlayer = QgsVectorLayer("test.shp", "test", "ogr")

Well, but long story short, I just compiled QGIS again with BUILD_TYPE as DEBUG.
(To do that you just add -D CMAKE_BUILD_TYPE=Debug to the cmake command.

With the debug I discovered the plugin libs weren’t being loaded.

(QgsProviderRegistry) Checking  /Users/pheres/apps/qgis1.4.0.app/Contents/MacOS/lib/qgis/libogrprovider.so : ...invalid (lib not loadable)

If we otool the libs we will find something like this:
Kameo:qgis pheres$ otool -L libogrprovider.so
libogrprovider.so:
QtCore.framework/Versions/4/QtCore (compatibility version 4.5.0, current version 4.5.2)
QtXml.framework/Versions/4/QtXml (compatibility version 4.5.0, current version 4.5.2)
/Library/Frameworks/GDAL.framework/Versions/1.6/GDAL (compatibility version 15.0.0, current version 15.1.0)
/Library/Frameworks/GEOS.framework/Versions/3/GEOS (compatibility version 4.0.0, current version 4.1.1)
@executable_path/lib/libqgis_core.1.4.0.dylib (compatibility version 1.4.0, current version 1.4.0)
QtGui.framework/Versions/4/QtGui (compatibility version 4.5.0, current version 4.5.2)
QtSvg.framework/Versions/4/QtSvg (compatibility version 4.5.0, current version 4.5.2)
QtNetwork.framework/Versions/4/QtNetwork (compatibility version 4.5.0, current version 4.5.2)
/Library/Frameworks/PROJ.framework/Versions/4.6/PROJ (compatibility version 6.0.0, current version 6.5.0)
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.4.0)
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)

The problem is in the @executable_path/lib/libqgis_core.1.4.0.dylib.
In OSX the @executable_path doesn’t seams to work.

So to solve this I wrote this bash script to update the library paths in every file in the providers path.

#!/usr/bin/env bash

version=$1
qgis_path=$2
file_to_change=$3

if [ "$file_to_change" = "." ]
then
file_to_change="*.so"
fi

for i in $file_to_change
do
    echo "updating $i"
    install_name_tool -change @executable_path/lib/libqgis_core.$version.dylib $qgis_path/lib/libqgis_core.$version.dylib  -change @executable_path/lib/libqgis_gui.$version.dylib $qgis_path/lib/libqgis_gui.$version.dylib -change @executable_path/lib/libqgis_analysis.$version.dylib $qgis_path/lib/libqgis_analysis.$version.dylib $i
done

DISCLAIMER: You may freely use the above script, but with NO guarantees. I can ruin you computer for all that I know. USE AT YOUR OWN RISK.

Assurances aside, I basically had to change the library paths for each file using the install_name_tool.

This problem is related with the following ticket in the QGIS Trac.

Categories: qgis | No Comments »