Dienstag, 17. September 2013

Build and install latest PostGIS in Ubuntu Linux

If you need to stick with the latest PostGIS version for your PostgreSQL-database, you either have to find a suitable PPA or simply build the extension yourself, because Ubuntu's PostGIS version is out-of-date. Instead of using 3rd party PPAs, I recommend to build it yourself the following way:

  1. Install the prerequisite packages:

    sudo apt-get install packaging-dev postgresql postgresql-server-dev-9.1 libxml2-dev libgeos-dev libproj-dev libgdal-dev

  2. Download the sources (e.g. for PostGIS 2.1.0):

    wget http://download.osgeo.org/postgis/source/postgis-2.1.0.tar.gz

  3. Extract the sources and go into extraction folder:

    tar xvzf postgis-2.1.0.tar.gz; cd postgis-2.1.0

  4.  Configure the sources

    ./configure

  5. Compile PostGIS

    make

  6. Install the compiled PostGIS extension

    sudo make install

  7. Use PostGIS within PostgreSQL:

    CREATE EXTENSION postgis;
     
Now you can build and control the update of your PostGIS versions by yourself.

Freitag, 6. September 2013

Process PostgreSQL data in R with Ubuntu Linux 12.04

Here is the ultimate overview  for all who like to work with R in Ubuntu and who want to process data from PostgreSQL. As a requirement, I assume that you already setup your PostgreSQL database.


  1. Step: Install dependencies (PostgreSQL, JDBC-Connector for PostgreSQL and R)
    sudo apt-get install postgresql libpostgresql-jdbc-java r-base-core r-cran-rjava r-cran-dbi 
  2. Step: Open Terminal & run R with
    R
  3. Step: Install JDBC-Package (RJDBC) for R
    install.packages(c("RJDBC"))
  4. Step: Connect to your database from within R
    # Include JDBC package
    library(RJDBC)

    # Initialize JDBC PostgresSQL driver
    drv <- JDBC("org.postgresql.Driver", "/usr/share/java/postgresql-jdbc4-9.1.jar", identifier.quote="`")

    # Establish connection to database
    conn <- dbConnect(drv, "jdbc:postgresql://localhost/database", "user", "pw")
    # Run your SQL select query
    data <- dbGetQuery(conn, "select * from iris where Species='setosa'")
  5.  That's it :) 
If you want to connect a non-local database, you have to adjust your dbConnect-command.