Skip to content

Installing Open Source Geo Software: Mac Edition

Pavan Gupta edited this page Feb 17, 2015 · 38 revisions

(back to general Software section). Switch to the Ubuntu version »

Installation Steps

These steps are known to work on 10.8 (Mountain Lion) and presumed to work on 10.7.

A shell script that will execute many of these steps is here

Prerequisites

  • Install Xcode from the Mac App Store.
  • Install the Xcode Command Line Tools by opening Xcode, Xcode → Preferences → Downloads → Components → "Install" button to the right of "Command Line Tools".
  • Install Homebrew by following the directions at that link.
  • Install pip (OR, install latest Python via Homebrew. (includes pip):
sudo easy_install pip
brew install python
  • Install numpy (required by gdal if using --complete):
pip install numpy
  • (optional) Install the esri File Geodatabase API to enable GDAL support for FileGDBs (created with ArcGIS >= 10.x). h/t Ragi (NOTE - this doesn't work at the moment for OS X Mavericks - see solution)
    • Download the esri File Geodatabase API for Mac, version 1.2 (you'll need an esri account). (using version 1.3 of the fgdb api doesn't currently work with the homebrew gdal installation method below (Sept 2013))
    • Copy it to /usr/local:
cd /tmp
unzip ~/Downloads/FileGDB_API_1_2-64.zip
cp -R /tmp/FileGDB_API/include/* /usr/local/include/
cp -R /tmp/FileGDB_API/lib/* /usr/local/lib/
  • Install GDAL/OGR. --complete may be omitted if you have simple needs (it installs the kitchen sink to work with a wide variety of formats). --enable-unsupported may be omitted if you did not install the FileGDB API (and do not have other proprietary libraries for gdal to try linking to). If you'd like the current development version, add --HEAD. --with-postgres will install PostgreSQL. This will take a while, especially if --complete was included.
brew install gdal --complete --enable-unsupported --with-postgres
  • Add /usr/local/lib/python2.7/site-packages to your PYTHONPATH. This is but one way to achieve this:
echo 'export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages' >> ~/.bash_profile

Most basic operations (and a some super complex!) can be undertaken in Python using Shapely.

sudo ARCHFLAGS='-arch x86_64' pip install Shapely

Fiona is a better python interface for OGR (see above).

sudo ARCHFLAGS='-arch x86_64' pip install Fiona

This will install TileStache and its core dependencies:

sudo pip install tilestache

To install additional dependencies (for compositing and testing):

sudo pip install Pillow werkzeug

First, install XQuartz. You can log out later.

Next, install Mapnik. If you need bleeding edge features, include --HEAD.

brew install mapnik --with-cairo --with-gdal --with-geos

Quantum GIS

If you don't mind running QGIS from the command link (qgis), install it with Homebrew. Otherwise, install the KyngChaos build.

Note: If you opt for the KyngChaos build and run into issues with GDAL compatibility, you may need to brew remove gdal and try using KyngChaos' GDAL build.

brew tap homebrew/science
brew install pyqt
brew install qgis --with-grass --with-postgis
brew linkapps
mkdir -p ~/.MacOSX
defaults write ~/.MacOSX/environment.plist PYTHONPATH -string "/usr/local/lib/python2.7/site-packages"

brew linkapps and setting PYTHONPATH in your global environment should allow you to run QGIS from ~/Applications, but something is currently preventing QGIS from seeing the correct PYTHONPATH, so you'll need to run it from the command line in the meantime.

TopoJSON requires Node.js, so you'll need that first. Installing from Homebrew is the easiest way to stay up-to-date (although you'll want /usr/local/share/npm/bin in your PATH):

brew install node

The official installer / binaries are another option, as is nvm, which is most appropriate if you're developing apps using Node and want/need to use multiple versions simultaneously.

Once Node is installed, TopoJSON can be installed globally using NPM (the Node Package Manager):

npm install -g topojson

PostgreSQL

Install PostgreSQL using Homebrew (if it wasn't already installed as a dependency of any of the above):

brew install postgresql

Initialize a database (note: these directions differ slightly from brew's recommendation in that the path includes the PostgreSQL version):

initdb /usr/local/var/postgres/9.2/main -E utf8

Add aliases to start and stop PostgreSQL. The following will add alias lines to your .bashrc and re-source it to make them immediately available:

cat <<EOF >> $HOME/.bashrc
alias start_pg='pg_ctl -D /usr/local/var/postgres/9.2/main start -l /usr/local/var/postgres/9.2/main/server.log'
alias stop_pg='pg_ctl -D /usr/local/var/postgres/9.2/main stop -s -m fast'
EOF
source $HOME/.bashrc

To start PostgreSQL, run start_pg. To stop, run stop_pg.

PostGIS

Install PostGIS 2.x using Homebrew (if it wasn't already installed as a dependency of any of the above):

brew install postgis

Restart PostgreSQL to load the newly installed libraries:

stop_pg && start_pg

Follow the directions for PostGIS 2.x in the PostGIS guide to set up a template and import some data.

Making PostgreSQL Fast

The defaults live in 1990s land. Upgrade your computer's gray matter!

  • Give Postgres more system memory, lots! - More is better here, set to the total RAM on your machine.
  • PGTune: Postgres config file: set the shared ram for each worker to ~2 to 4 gigs, depending on your setup. More (4 gb) is better unless you're running multiple concurrent sessions and need to play nice (2 gb).

Accessing PostgreSQL from Python

psycopg2 is one option:

sudo pip install psycopg2

Miscellany

avce00 will convert E00s to binary coverages, which OGR is happier dealing with. pgdbf will load DBFs into Postgres. shapelib includes some DBF and Shapefile utilities.

brew install avce00 pgdbf shapelib

Troubleshooting

  1. Bad / partial / aborted brew install <whatever> won't let you finish installing:
rm $(brew --cache)/Formula/<software_name_like_gdal>.brewing

Note: use * to remove all partially brewed recipes.

Shell Environment Configuration

(This presumes bash, which is OS X's default shell.)

$HOME/.bash_profile contains shell settings (affecting what you see in Terminal.app).

Note: This file is invisible (due to the leading ., so you'll need to find it or type nano ~/.bash_profile on the command line.

Let's edit it! Add the following (you may have already added this on brew's recommendation):

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

If you get get lost by randomly dropping into VIM, set Nano as your default editor:

export EDITOR=nano
export VISUAL=nano

If you use git on the command line, colors often improve legibility. This will turn on colors for git status, git diff, etc.:

git config --global color.ui auto

This will display the active branch in your prompt, as well as whether you have any pending changes. Add the following:

# extracted from @mojodna's dotfiles, learned from the internet
source /usr/local/etc/bash_completion

function __git_status_flag {
  git_status="$(git status 2> /dev/null)"
  remote_pattern="^# Your branch is (.*) of"
  diverge_pattern="# Your branch and (.*) have diverged"
  if [[ ! ${git_status} =~ "nothing to commit" ]]; then
    state="*"
    spacer=" "
  fi
 
  if [[ ${git_status} =~ ${remote_pattern} ]]; then
    spacer=" "
    if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
      remote=""
    else
      remote=""
    fi
  fi
 
  if [[ ${git_status} =~ ${diverge_pattern} ]]; then
    remote=""
    spacer=" "
  fi
 
  echo "${state}${remote}${spacer}"
}
 
PS1='\[\e[32m\]\u@\[\e[1m\]\h\[\e[22m\]:\[\e[1;34m\]\w\[\e[22;35m\]$(__git_ps1 " [\[\e[33m\]$(__git_status_flag)\[\e[35m\]%s]")\[\e[33m\] \$ \[\e[0m\]'

bash_completion is required for __git_ps1:

brew install bash-completion

Additional References