[GIS] How to enable SFCGAL in PostGIS

extensionspostgispostgresqlsfcgal

I have the problem with the enabling extension SFCGAL.

CREATE EXTENSION postgis_sfcgal;

doesn't work for me even I have the PostGIS v2.2.

When I do that, it throws an error

ERROR: could not open extension control file
/Applications/Postgres.app/Contents/Versions/9.5/share/postgresql/extension/postgis_sfcgal.control:
No such file or directory.

Best Answer

It seems the PostGIS version you are using does not provide SFCGAL support.

I managed to compile PostGIS 2.2.2 with SFCGAL 1.3.0 support for PostgreSQL 9.5 using following script, which is based on this. You need to get the latest CGAL library first using the PPA given on this webpage. I was facing some 'missing keys errors' on the way I was able to resove using y-ppa-manager.

Version overview:

Unbuntu Trusty Tar 14.04 LTS
PostGIS 2.2.2
GEOS 3.5.0
PostgreSQL 9.5
SFCGAL 1.3.0
CGAL 4.7.2 

The script is just what I copied toghter during the process, you might have to adapt some parts.

#!/bin/bash
# install cgal ----------------------------------------------------------------

sudo sh -c "echo 'deb http://debian.cgal.org stable main' >> /etc/apt/sources.list"
sudo sh -c "echo 'deb-src http://debian.cgal.org stable main' >> /etc/apt/sources.list"

# install y-ppa-manager to get missing keys
sudo add-apt-repository ppa:webupd8team/y-ppa-manager

# install geos 3.5 ------------------------------------------------------------
cd ~
wget http://download.osgeo.org/geos/geos-3.5.0.tar.bz2
bunzip2 geos-3.5.0.tar.bz2
tar xvf geos-3.5.0.tar
cd geos-3.5.0
./configure
make && sudo make install


# add postgresql official repository to apt
# apt-get update
# echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" >> /etc/apt/sources.list
# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# apt-get update

# install required libraries
apt-get install autoconf build-essential cmake docbook-mathml docbook-xsl libboost-dev libboost-thread-dev libboost-filesystem-dev libboost-system-dev libboost-iostreams-dev libboost-program-options-dev libboost-timer-dev libcunit1-dev libgdal-dev libgeos++-dev libgeotiff-dev libgmp-dev libjson0-dev libjson-c-dev liblas-dev libmpfr-dev libopenscenegraph-dev libpq-dev libproj-dev libxml2-dev xsltproc git build-essential wget

# this is done above
# apt-get install libcgal-dev

# install postgresql-9.5 server
apt-get install postgresql-9.5 postgresql-9.5-dev postgresql-server-dev-9.5

# build sfcgal from source
wget https://github.com/Oslandia/SFCGAL/archive/v1.3.0.tar.gz
tar zxvf v1.3.0.tar.gz
cd SFCGAL-1.3.0 &&  cmake . && make  && make install
cd ..

# build postgis from source
wget http://postgis.net/stuff/postgis-2.2.2dev.tar.gz
tar zxvf postgis-2.2.2dev.tar.gz
cd postgis-2.2.2dev
./configure --with-sfcgal=/usr/local/bin/sfcgal-config
make && make install
cd ..

# probably uneeded ------------------------------------------------------------
# chmod 777 /usr/local/lib/libSFCGAL.*
# cp /usr/local/lib/libSFCGAL.* /usr/lib/postgresql/9.5/lib
# ldconfig -v | grep -i sfcgal 

sudo -i -u postgres
psql -c "create database postgis_test;"
psql postgis_test -c "create extension postgis;"
psql postgis_test -c "create extension postgis_sfcgal;"
psql postgis_test -c "select postgis_sfcgal_version();"