Postgres & PostGIS installation

So far I have install and worked with Leaflet & GeoServer. In order to complete the full stack web GIS solution the database component. The most common (open source) software for this option is the PostgreSQL/PostGIS.

In my case that I am using Mac (macOS High Sierra V10.13.6), the work-through that I followed is based on this post and it is as follows:

  • assuming that homebrew is installed
  • assuming that no previous version of Postgres is installed (has to be uninstalled)
  • open terminal and install Postgres:
brew install postgres
  • install PostGIS
brew install postgis

The previous steps might take quite some time due to the fact that there are several dependencies. Additional software will be installed (or updated) like: freexl, gdbm, sqlite, python@2, geos, giflib, jpeg, json-c, libtiff, proj, libgeotiff, libpng, libpq, libxml2, libspatialite, xz, python, numpy, pcre, zstd, gdal, boost, gmp, mpfr, cgal, sfcgal

  • start the PostgreSQL server
pg_ctl -D /usr/local/var/postgres start

Defaults for PostgreSQL are:

  1. address: 127.0.0.1
  2. port: 5432
  3. user & password the same as the user who created the PostgreSQL
  • Create Database

initdb /usr/local/var/postgres

If terminal shows an error

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".

Remove old database file

rm -r /usr/local/var/postgres

Run the initdb command again

initdb /usr/local/var/postgres

In case this wont work, restart the terminal.

  • Instead of continue working on the terminal I prefer to use the pgAdmin. Download the software in the pgAdmin download site.
  • On the pgAdmin browser user interface select the server and log in (if required) using your credentials. Select databases and then from menu Object>Create>Database. Give a name to the database and create it.
  • Select the new database on the left pane. Select from the menu Tools/Query tool
  • Give the below query to create the PostGIS extension on the DB.
CREATE EXTENSION postgis;
  • In order to verify repeat but give the query
SELECT PostGIS_Version();

As a result there should the PostGIS version used in the DB.

Screen Shot 2018-09-30 at 19.13.24

 

Leaflet & GeoServer in 1 hour

Today I wanted to test Leaflet with GeoServer. While I was looking for a decent GeoServer WMS service, I realised it would be way faster to install and operate the GeoServer and use one of the default WMS services. So I did. The hole procedure took me less than an hour.

First I found and downloaded the latest version of GeoServer from the official download page. It is quite small in size and it is totally worth it if you are a “GIS guy” (or not) to check it out.

Even though I would have created the Leaflet code myself I tried to find something ready and just configure it. There is a post on the gis.stackexchange which helped me quite a lot (LINK).

I amended the code in order to add the Ocean Basemap by ESRI on the background and to use a different layer from my GeoServer instance. I was tempted to create also a different layer on the GeoServer based on the GeoServer Docs, but this will come on a different post in the future more in detail. I finally used the topp:states layer which originally is a shapefile saved as layer in the GeoServer. The HTML code that I used can be found here.

Screen Shot 2018-09-28 at 22.28.41

 

Leaflet map example: Electoral districts of Athens

This is a basic example of how to create a leaflet map. It is a map of the electoral district of Athens Municipality. Data are retrieved from data.gov.gr.

It includes:

  • loading OSM as a background map
  • some basic styling of the polygons
  • loading the data from a json file using ajax
  • creating popup based on the json file
  • adding a layer control

Screen Shot 2018-09-26 at 15.57.47

Link to code.

Link to data file.