PostGIS, Geoserver and Leaflet | GIS Web Development

In this article we will see how data can query in Postgres and store and view at Geoserver and Leaflet i.e GIS Web Development with PostGIS, Geoserver and Leaflet. For this you will require setup of all three. You can visit our tutorial to install Geoserver For Linux or geoserver install in windows, Postgres and Postgis installation for Ubuntu 16.04. and a basic of Leaflet Js Javascript Library. If you are done with intial setup, then we can first start with to adding Shapefile in postgres by creating  database in database.

PostGIS, Geoserver and Leaflet | GIS Web Development – Steps

Step 1: After installing Geoserver and Postgress postgis in your system you need to download dataset. The data we are using is shapefile for this tutorial, which you can download from the sources given in this post. The shapefile can be administrative boundary or road network etc. For any region. This website will help you to download zip file as well, which will have all supporting file for shapefile.

Step 2: Now you need to open Postgres, add database, table and add shape file using shp2pgsql. This all has been explained in our tutorial.

Step 3: Start Geoserver by running shell file provided in bin folder of geoserver. That can be run by executing the given command on terminal.

cd <path to Geoserver/bin>
sh startup.sh

Then Open browser and type

http://localhost:8080/geoserver 

and login by using your credentials. Generally its admin as username and password as geoserver.

Step 4: Now create a new workspace on Geoserver. Now Move to workspace option in data section by providing required information. Create new store as PostGIS and provide information required.  Here you need to give workspace name and data source name you created. If you want you can write the description about your data.

PostGIS, Geoserver and Leaflet | GIS Web Development

Now you need to provide connection parameter.

Dbype is the type of database, you can leave it as default or can specify your database type. Host and port: these are the parameter, which help geoserver to connect with postgres. The Host is space where database exist. The port to connect with postgres host, you can provide if you have set any otherwise make it as 5432.

Database: this is the database, which you have created using postgis. You are not sure about the name you can check it on pgAdmin or in terminal.

\l : this will provide you the list of available database. For this you must work with proper user-name.

Schema: to know your table, which you have created in database you can execute \d as this will provide you list of table associated database with their schema.

User and Password: this the USER with whom you have created database. In postgres you can create a new user with proper password.

Namespace: Namespace to be associated with the database. This field is altered by changing the workspace name. Remaining option you can leave as it is. And save the information.

Stpe 5: Now open the Layer option in data section, searched for your workspace and publish the layer.

Stpe 6: Here you will see tabs as data and publishing. In data tab you need to provide the reference coordinate system (CRS) by clicking on compute from data or compute from native bounds. After this you can save this layer with default styling. If you want to style your layer, which includes color, symbols etc. You can visit our tutorial styling vector data.

Step 7: Now moving to layer preview option in same data section. Here you can see your shape file.

Step 8: The shape file now can be render on map using leaflet javascript library.

For that you need to create HTML file, add leaflet library. After this create a div, which will contain map. Using L.map() and initialize the map and set view using setView() this takes center latitude longitude and zoom as arguments. You can check how to load a layer of polygon, point or polyline in leafletjs.

After that the shape file using L.TileLayer.WMS() , provide the base WMS URL, and specify whatever WMS options you need. The option can be layer, transparent and format etc. After this  use addTo() function to add layer to initialized map. Open that HTML file on browser to see the map with rendered shapefile.

Hope this PostGIS, Geoserver and Leaflet | GIS Web Development helped you in creating a map layer of shapefile which is loaded in postgis, and added the wms layer from geoserver which is then served as map in browser with leafletjs. If you find any problem in understanding the same, do let us know by commenting below.

Connection between PostGIS and QGIS

GIS applications are tools that allow users to create interactive queries (user-created searches), analyze spatial information, edit data in maps, and present the results of all these operations. Geographic information science is the science underlying geographic concepts, applications, and systems and What goes beyond a GIS is a spatial data infrastructure, a concept that has no such restrictive boundaries. For handling the spatial data infrastructure we use tools like PostGIS and QGIS. this post will help guides you about How can we establish a Connection between PostGIS and QGIS with the help of some GIS operations.

Connection between PostGIS and QGIS

PostGIS and QGIS are two major platforms used for handling GIS operations. PostGIS adds support for geographic objects allowing location queries to be run in SQL. As PostGIS is a spatial database extender for PostgreSQL object-relational database. In our previous post you can read about PostGIS and PostgreSQL.

Now we are going the discuss the details of how We can establish connection between PostGIS and QGIS and before that let’s just discuss some important topics in brief.

QGIS

QGIS is one of the most common open source application used by GIS analyst, Geographers, Civil Engineers and many other professionals. QGIS is a Desktop based Geographic Information System (GIS) Application for creating maps, editing, viewing and analyzing Geospatial data. Quantum GIS is a cross-platform, free and open source application. It can be run on multiple operating system like Mac OS X, Windows, Ubuntu, Linux and Unix.

PostGIS

PostGIS is an open source software program that adds support for geographic objects to the PostgreSQL object-relational database. PostGIS follows the Simple Features for SQL specification from the Open Geospatial Consortium (OGC).
Technically PostGIS was implemented as a PostgreSQL’s extension. And There are a large number of software products that can use PostGIS as a database backend.

PostgreSQL

PostgreSQL is an open source relational database management system ( DBMS ) developed by a worldwide team of volunteers. PostgreSQL is not controlled by any corporation or other private entity and the source code is available free of charge.
When we installed PostgreSQL database server, the PostgreSQL installer also installed some useful tools for working with the database server. You can connect to the PostgreSQL database server by using the psql or pgAdmin tool.
psql is an interactive terminal program provided by PostgreSQL. You can do a lot with psql tool e.g., execute SQL statements, manage database objects, etc. and The second way to connect to a database is using pgAdmin GUI application. By using pgAdmin GUI application, you can interact with PostgreSQL database server via an intuitive user interface.

Connection between PostGIS and QGIS

  • Import Shapefile into PostGIS

    Connection between PostGIS and QGIS

    We are going to import some Shapefiles into PostGIS for creating a table in PostgreSQL database. Importing shapefiles into PostGIs using psql is already discussed in our previous post “Import Shapefile in Postgresql – shp2pgsql”.
    Shapefile: The most popular geospatial vector data format. First developed by ESRI. Because of it’s open specification it is now predominantly used to share GIS data between open source and proprietary software.
    We can also do this import with pgShapeloader tool (also known as “PostGIS Shapefile Import/Export Manager” or “PostGIS Shapefile and DBF loader”). It provides another option for loading shapefiles into PostGIS database tables. It is the graphical user interface (GUI) equivalent of the command line shp2pgsql tool.
    The pgShapeloader tool combines the two data loading stages, converting data into SQL commands and running those commands against the target database, into one operation.
    On Linux pgShapeloader can be launched from the terminal with the shp2pgsql-gui command which generates this.

    now click View connection details and enter the connection information in the PostGIS Connection section.

    Click OK to return to the main application. The shapefile loader uses the supplied connection details to connect to the target database; the connection status is reported in the Log Window.

    To select your source files, click Add File to open the Select a Shape File dialog box. Navigate to the location of your shapefile, click the shapefile you wish to load, and click Open. Multiple files can also be added in the same way. and Once all import options have been added and configured, click OK and click Import to start the conversion.

    Now these shape files are added in the PostGIS databases as tables you can view them and their attributes in pgAdmin or psql.

  • Add and Modify PostGIS data files in QGIS

    Now we are going to add and modify the PostGIS database files in QGIS application.

    • Open QGIS application, you will see PostGIS option in Browser toolbar here.
    • Right click on the PostGIS option and click on the “New Connection” that will open a “Create a New PostGIS connection” toolbar.

      Connection between PostGIS and QGIS

    • Enter the Database Connection details and then click the “Test Connection” button. this button will open up an authentication form for server details. fill the authentication details and click Ok.
    • you will see a message on toolbar i.e connection to is successful. Now click OK and you have successfully connected your database with the QGIS. You can see the Database and the Shapefiles we imported in PostGIS below the PostGIS in browser window in QGIS.
    • Now for adding these Shapefile as layers in QGIS, Right click on the files and select “Add Selected layers to Canvas” then the file will show as a layer on canvas and in layers window in QGIS. you can also see the attributes of the Shapefile  by right-click on the filename in Layers window and selecting Open attribute table.
    • Now for Modifying the Shapefiles , I prefer you to go to Split Sub Divide polygon layer using QGIS – Shapefile, kml, GeoJson.
      There you can modify the Shapefile and we can see that as the polygon is Sub-divided It increases the attributes of the Shapefile.
  • Now as we can see that the attributes of Shapefile after modifying has increased compare to the attributes when the file was imported from PostGIS database.

When the Shapefile is saved after modification in QGIS by default the file is also saved in PostGIS database with the new and changed attributes it has before.

The connection between QGIS and PostGIS is proves that if any data file is altered in QGIS, the file also gets modified in the PostGIS database system.

Download free Shapefile Maps

This is how we can show the establishment of the Connection between PostGIS and QGIS.

Exit mobile version