11
Lancaster University Masterclass in Open Source GIS: January 2010 Databases and Web Mapping the Open Source way Introduction There is considerably more to web mapping than Google Mash-ups. While the approach outlined in this section is a bit more complicated, it's a lot more powerful, flexible and interesting, and you have complete control over your maps. There are three parts to this section: databases, map servers and web servers. It's not possible to go into full detail about any of these areas in the time available, but hopefully you will go away with an overview of how things work and an idea about where to go for more information. Hopefully, this section will allow you to load data into a spatial database and learn how to work with and interrogate it. You will then use a map server and web server to display it in a web-based map over suitable base data. There will be brief digressions into displaying your data in a desktop GIS, and into using web mapping services. Finally we'll look at some of the more advanced and interesting new developments in web mapping. Databases The database we are going to use is PostgreSQL (http://www.postgresql.org ), which is a server-based multi-user database with support for storing spatial vector data such as points, lines, and polygons. The difference between server-based databases such as PostgreSQL and file-based databases such as Microsoft Access is that there is complete separation between the data, stored on the server, or host, and the software used to access the data on the remote pc, or client. The software used to access the data is usually one of the following: 1.PSQL- a command line package bundled with the server installation 2.PgAdmin3- with a graphical user interface allowing access to tables, management tasks and queries 3.PhpPgAdmin- a web-based user interface similar to PgAdmin3 4.Desktop GIS packages such as Quantum GIS and GvSIG 5.Map Servers PostGIS is an extension for PostgreSQL that provides additional functionality such as spatial queries (within, distance from, inside and so on) and coordinate systems. These allow you to do complex spatial queries and management of your data without needing any additional software. Basics Data is stored on the HOST server in a DATABASE, and accessed using a USERNAME, PASSWORD, and PORT. The HOST parameter is likely to be an IP address (numbers of the form 192.168.3.23 which form the unique address of the server on the network), a name, or “localhost” if you are connecting 

Lancaster University GIS Course 2010

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Lancaster University GIS Course 2010

Lancaster University Masterclass in Open Source GIS: January 2010

Databases and Web Mapping the Open Source way

IntroductionThere is considerably more to web mapping than Google Mash­ups. While the approach outlined in this section is a bit more complicated, it's a lot more powerful, flexible and interesting, and you have complete control over your maps.

There are three parts to this section: databases, map servers and web servers. It's not possible to go into full detail about any of these areas in the time available, but hopefully you will go away with an overview of how things work and an idea about where to go for more information.

Hopefully, this section will allow you to load data into a spatial database and learn how to work with and interrogate it. You will then use a map server and web server to display it in a web­based map over suitable base data. There will be brief digressions into displaying your data in a desktop GIS, and into using web mapping services. Finally we'll look at some of the more advanced and interesting new developments in web mapping.

DatabasesThe database we are going to use is PostgreSQL (http://www.postgresql.org), which is a server­based multi­user database with support for storing spatial vector data such as points, lines, and polygons. The difference between server­based databases such as PostgreSQL and file­based databases such as Microsoft Access is that there is complete separation between the data, stored on the server, or host, and the software used to access the data on the remote pc, or client. The software used to access the data is usually one of the following:

1.PSQL­ a command line package bundled with the server installation

2.PgAdmin3­ with a graphical user interface allowing access to tables, management tasks and queries

3.PhpPgAdmin­ a web­based user interface similar to PgAdmin3

4.Desktop GIS packages such as Quantum GIS and GvSIG

5.Map Servers

PostGIS is an extension for PostgreSQL that provides additional functionality such as spatial queries (within, distance from, inside and so on) and coordinate systems. These allow you to do complex spatial queries and management of your data without needing any additional software.

Basics

Data is stored on the HOST server in a DATABASE, and accessed using a USERNAME, PASSWORD, and PORT. The HOST parameter is likely to be an IP address (numbers of the form 192.168.3.23 which form the unique address of the server on the network), a name, or “localhost” if you are connecting 

Page 2: Lancaster University GIS Course 2010

directly from the server. The port parameter is usually the default 5432. 

Each program needs these 5 parameters in order to connect to the database. The Detailed configuration of user security (who is allowed to access which database, and using which computer) is possible using text­based config files on the host.

The way of communicating with the data in a PostgreSQL database is by using Structured Query Language, or SQL. This is text­based, and human­readable, with a very precise syntax and grammar. We will cover a small subset of SQL when we learn how to query the data in a later section.

PostGIS has to be installed after PostgreSQL. It creates additional tables in the database to store information on coordinate systems. PostGIS can either be installed on an individual database, or in the template from which all new databases are created.

Loading data into PostgreSQL

There are several ways of loading spatial data into PostgreSQL. The most common are two command line packages called OGR2OGR and SHP2PGSQL. We will use  SHP2PGSQL, which is bundled with the PostGIS installation. As the name suggests it only works with shape files.

SHP2PGSQL converts the data in a shapefile into a stream of SQL commands which are then loaded using PSQL. These two steps can be concatenated into a single command.

As well as the 5 connection parameters, SHP2PGSQL requires the following pieces of information:

1.The location and name of the shapefile

2.The coordinate system of the data in SRID form (4326 for lat/long)

3.The name of the table you wish to load it into

The syntax is as follows:

$ shp2pgsql /location/of/your/shapefile.shp tablename ­s coordinate system ­W latin1 | psql ­d database ­U username ­W ­h host ­p port

The shp2pgsql ­W parameter sets the encoding of the database to match the encoding of the shape file and isn't always necessary. The psql ­W parameter prompts you for a password before processing the command.

Note the case of ­U and ­W, the lowercase parameters mean something different!

You can check that the data has loaded using PgAdmin3­ this will show you the text­based attributes of the data. The geometry of the data will be stored in the geometry column in the table, this is called the_geom if you use SHP2PGSQL or wkb_geometry if you use OGR2OGR. The different names do not matter, as the postgis­enabled database automatically contains a table called geometry_columns that lists the geometry column used by each table, and the coordinate system in use. This is automatically filled in by the SHP2PGSQL or OGR2OGR program.

TASK: Use PgAdmin3 to check your shapefile has loaded, and also to check that the geometry_columns table has been filled in.

Querying data

The tabular data in PostgreSQL can be queried using SQL as described above. The basic syntax is as 

Page 3: Lancaster University GIS Course 2010

follows:

SELECT columns FROM table WHERE some condition is met;

A guide to using SQL can be found on the PostgreSQL cheatsheet provided, but in brief:

•SELECT * FROM table;

This will select all the data in all the fields from a given table.

•Logical and Arithmetic operators can be used in the WHERE condition to choose data that meets a given parameter (or parameters).

•In the WHERE condition, text is enclosed in commas 'like this' but numeric data is not.

•The wildcard character % can be used in the WHERE condition LIKE, for example to find all names that end in 's':

SELECT * FROM table_with_names_in WHERE firstname LIKE ('%s'); 

TASK: Use the PgAdmin3 query window to select all counties that begin with L.

Spatial Querying

In a PostGIS­enabled database, it is possible to construct complex spatial queries, like buffers, intersections, unions, as well as management functions like re­projecting your data into a new coordinate system. The syntax is an extension to SQL, and many examples can be found in the PostGIS cheat­sheet and manual. (http://postgis.refractions.net/download/postgis­1.4.1.pdf)

Some examples:

1.Find the total extent of a layer:

SELECT st_extent(the_geom) FROM tblcounties;

2.Find out which county a particular point is in:

SELECT name_2 FROM tblcounties WHERE st_within(geomfromtext('POINT(­1.3 54.26)', 4326), the_geom);

3.Find out which counties adjoin Lancashire:

SELECT a.name_2 FROM tblcounties a, tblcounties b WHERE b.name_2 = 'Lancashire' AND a.the_geom && b.the_geom;

Note that this uses a couple of additional concepts. Firstly, to answer this question a table needs to be compared to itself, otherwise known as a self­join. This necessitates the use of aliases, so PostgreSQL knows which copy of the table it is doing each command to. In this case the aliases are assigned in the SELECT part of the statement and then used to refer to the table throughout the query. The && symbol is shorthand for checking that two geometries overlap.

TASK: Find out which county the above point is in, then find out which counties overlap it.

Map ServersA map server is a program that lives on a host server and accepts requests from a client computer (web or desk­based) and returns geographic data from a variety of defined data sources. These can be raster 

Page 4: Lancaster University GIS Course 2010

(base­mapping) or vector (points, lines and polygons). 

In its most simple form that map server packages up all the defined data sources and returns a simple flat image to the client. Given additional parameters, the map server can return an image of a limited subset of the data sources, and for a given bounding box or coordinate system.

Types of Map Server

There are two main open source map servers available. These are:

1.Minnesota Mapserver (usually abbreviated to Mapserver). This is a cgi executable, ie a program that runs when required by a web server. Configuration is done using a text file called a map file.

2.Geosever. This is a java­based package with a web interface for configuration.

There are strengths and weaknesses to both packages. It is possible to run both packages on the one server, returning data from the same data sources. In this course we will just be using Minnesota Mapserver.

Installation

Detailed installation instructions are beyond the scope of this tutorial, but information can be found for all major operating systems at the mapserver website (http://mapserver.org/). In brief, the program comprises the main executable, mapserv, which must be placed in a location accessible to the web server, and a series of utility tools for working with maps and data.

Map File Syntax

The map file is a text file with a .map extension. It must live in a location accessible to the web server. It comprises three basic sections, of which the second two are subsets of the first.

1.Map

This defines global configuration options such as the name, the maximum extent, where to find the data,  and supporting files, and the level of error reporting.

2.Web

This contains the locations of the folders where mapserver stores temporary files and images. 

3.Layer

Each data source in the map is defined as a layer,  it's type, and connection information are defined.

Below is a very simple map, with one single layer, showing the hierarchy of the sections. Note that each section ends with the keyword END. Note also that, although mapserver is not case­sensitive, it is standard practice to put keywords in capitals and text strings in quotation marks. 

If a map is to be used in a dynamic fashion, such as on a web site, it normally has a number of additional sections such PROJECTION, which defines it's coordinate system, and CLASS, which contains the styling information for a layer.

MAP NAME "sample" STATUS ON SIZE 600 400

Page 5: Lancaster University GIS Course 2010

SYMBOLSET "../etc/symbols.txt" EXTENT -180 -90 180 90 UNITS DD SHAPEPATH "../data" IMAGECOLOR 255 255 255 FONTSET "../etc/fonts.txt"

# # Start of web interface definition # WEB IMAGEPATH "/ms4w/tmp/ms_tmp/" IMAGEURL "/ms_tmp/" END

# # Start of layer definitions # LAYER NAME 'global-raster' TYPE RASTER STATUS DEFAULT DATA bluemarble.gif ENDEND

Checking a map file

There are a number of ways to check that the basic syntax of a map file is correct:

1.Shp2img

This is a utility bundled with most mapserver installations that generates an image from a map file. The syntax is as follows:

$shp2img -m /location/of/your/mapfile.map -o test.png2.Checking using a browser

This method is more complex but will generate an error message rather than a blank image in the event of an error, so can be more informative. The basic syntax is as follows, but note that this differs slightly from operating system to operating system:

http://yourserver.com/cgi­bin/mapserv?map=/full/path/of/your/mapfile.map&mode=map

Connecting to a PostgreSQL data source using Mapserver

Mapserver uses the 5 parameters defined above to connect to a PostGIS­enabled PostgreSQL data source. The CONNECTIONTYPE parameter must also be used, and set to PostGIS. Additionally the DATA parameter must include an SQL query that selects some or all of the geometry from the specified table. Note that the SELECT keyword is omitted from the query, and all the query terms must be in lower­case.

LAYER NAME "province" STATUS ON TYPE POLYGON

Page 6: Lancaster University GIS Course 2010

CONNECTIONTYPE POSTGIS CONNECTION "host=localhost port=5432 dbname=canada user=postgres password=postgres" DATA "the_geom from province"END

Task: Find the extent of the counties data. In the template map file provided, add a connection to the counties data, change the extent, and check it using shp2img and using a web browser.

Web Servers and Web Pages

Introduction

A web server is a program that sits on a host and responds to remote requests from a client pc using http (hypertext transfer protocol). This is normally in the form of a web page that is then displayed in a browser. The web page simply needs to be stored in a location accessible to the web server.

The simplest form of web page generally has the suffix html (it is written in hypertext markup language). The client requests a page using its URL (Uniform Resource Locator­ usually the name of the web site, plus the directory the page is stored in, plus the name of the page) and the web server sends that page to the client. The client browser renders the page, and sends off further requests for the additional resources referred to in the page, such as images, or styling information.

Basic structure of a web page

Web pages have a very simple pre­defined structure. Like map files, they contain a hierarchy of elements that perform different functions, in this case defined by opening and closing tags. There can be only one set of each tags. The basic structure is as follows:

<html>

<head><title> My Home Page </title>

</head>

<body>HELLO WORLD!

</body>

</html>

The <head> section is used to tell the browser information about the page and how it should be run. In addition to the title (which will appear in the title bar of the browser, it may contain metadata about the language of the page, and links to external files for styling or scripting.

In the <body> section of the page, everything outside of a pair of angle brackets will be displayed on the web page. As well as plain text, such as in the example above, other attributes (keywords in angle brackets) can be used for styling, formatting text as a link to another web page, or showing an image.

Page 7: Lancaster University GIS Course 2010

By using “view source” in a web browser, you can view the structure of any web page.

Task: Make a simple web page showing some text and display it in your browser

Scripting

Scripting is a way of getting a web page to do more than display static text or images. For example, it is used to create forms to allow you to submit data to a web site, and to display data from a database or map.

There are many scripting languages available, but there are only two types:

1.Server­side

In this case the web page contains instructions that are interpreted using additional software on the web server. The result is then formatted as a standard web page and rendered as such by the client (so “view source” shows you the rendered structure rather than the original script). Examples of this are pages that display data from a database. The usual language (and software) used for this is PHP (PHP Hypertext Processor­ this is not a mistake). These pages usually have the suffix php. This has the disadvantage that the page must be submitted back to the server for information (such as a filled­in form) to be processed.

2.Client­side

In this case the browser interprets the instructions after it has received the web page from the server. The most common language used is javascript. The script is either placed in the <head> element of a standard html web page, in it's own <script> tags, or in an additional file requested by the browser (these will usually have the suffix js). This method allows for considerable functionality without the need to re­submit the page back to the server, but it is more limited.

Task: Look at the phpinfo.php file in your web directory using a text editor, then run it in a browser.

And finally... Web Mapping

Introduction

There are a number of different open source web mapping options, generally using either client or server­side scripting to work. We are going to concentrate on openlayers (http://openlayers.org), which is written in javascript. The information below is largely taken from the excellent opengeo openlayers workshop (http://workshops.opengeo.org/openlayers­intro/), which also goes into more advanced mapping.

Making a map

You don't really need to know much/any javascript to use openlayers in its basic form, but you do need to respect the syntax and the order in which the components are declared and used.

This works in the following way:1. The OpenLayers library is loaded using the script tag. 2. There is a positioning element (<div>) in the body of the page, with the id “map” that contains 

Page 8: Lancaster University GIS Course 2010

the map. 3. A map is created using JavaScript. 4. A layer is created using JavaScript, and then added to the map. 5. The map zooms out as far as possible. 6. The JavaScript function gets called only after the body tag’s onLoad event. 

Page 9: Lancaster University GIS Course 2010

For example:

<html><head>

<script src="http://www.openlayers.org/api/OpenLayers.js"></script><script type="text/javascript">

var map; function init() {

map = new OpenLayers.Map('map');mylayer = new OpenLayers.Layer.MapServer( "World Map",

"http://localhost/cgi­bin/mapserv.exe",{map: 'C:/world_mapfile.map'});

map.addLayer(mylayer);map.zoomToMaxExtent();

}</script>

</head><body onload="init()">

<div id="map"  style="width: 600px; height: 300px"></div></body>

</html>

Step 1 (red): Getting the OpenLayers Library <script src="http://www.openlayers.org/api/OpenLayers.js"></script>

The URL points to the location of the JavaScript file that loads OpenLayers. 

Step 2 (orange): Positioning the map in the web page

<div id="map" style="width: 600px; height: 300px"></div>

The <div> tag is a container with the id “map” for the map that we are creating in our page. Later, when we initialize the JavaScript map object, we will give it this id.

The size of the map is determined by the size of the element that contains it. Here, we have set the size of the map to be 600 pixels by 300 pixels. 

Step 3 (yellow): The Map Object 

map = new OpenLayers.Map('map');

There are lots of "maps" in this statement! This line creates a new OpenLayers.Map object with the to go in the div 'map' and makes it a variable called map so it can be referred to elsewhere in the code.

The OpenLayers.Map object, tells the openlayers javascript that we are using the Map class. This allows us to access a whole library of pre­built code without needing to write the code ourselves.

Page 10: Lancaster University GIS Course 2010

Step 4 (blue): Creating a Layer 

mylayer = new OpenLayers.Layer.MapServer( "World Map",

"http://localhost/cgi­bin/mapserv.exe", {map: 'C:/world_mapfile.map'} );

map.addLayer(mylayer);

Like a Mapserver map file, OpenLayers organizes a single map into several layers or data sources. In this case, we are adding a new object of the Mapserver layer class, telling it the location of the mapserver executable, the location of the map file, and the layer we want from it (“World Map”). We are then assigning it the variable name mylayer, which is then used to load the layer in the map.  

Different layer classes need different parameters to be given. Examples of many different layer types can be seen on the openlayers web site (http://openlayers.org/dev/examples/)

Step 5 (purple): Positioning the Map 

map.zoomToMaxExtent();

This code tells the map to zoom out to its maximum extent, which by default is the entire world. It is possible to set a different maximum extent. 

OpenLayers maps need to be told what part of the world they should display before anything will appear on the page. Calling map.zoomToMaxExtent() is one way to do this. Alternative ways that offer more precision include using zoomToExtent() and setCenter(). 

Step 6 (green): Loading the Map 

<body onload="init()">

In order to ensure that your document is ready, it is important to make sure that the entire page has been loaded before the script creating the map is executed. 

We ensure this by putting the map­creating code into a function, and then calling it only when the <body> element is fully loaded. 

Task: Copy the openlayers.html file in your web directory to openlayers_ms.html and adapt to use your mapserver file then display the results in the web browser. 

Base Mapping

Displaying your data in a web map is one thing, but it looks much better when overlain on some base mapping or satellite data. Openlayers can access base mapping from a variety of different mapping and imagery providers, such as Google, Yahoo, Microsoft, OpenStreetmap and so on.

Overlaying data onto base mapping brings an additional layer of complexity into the mix as the base mapping and your data may not necessarily be in the same coordinate system. In fact it is quite unlikely that they will be, as your data is likely to be smaller in scale and the base mapping is likely to be world­wide.

Commercial base layers tend to be in Spherical Mercator coordinates, which has the EPSG code 900913 (it looks like GOOGLE as an aide memoire). Your data can be projected either server­side by the map server or client­side by the openlayers code.

Page 11: Lancaster University GIS Course 2010

However, for simplicity, we will choose a base layer that does not require re­projection in this case.

Task: Edit the base layers example in your web directory to use your layer and examine the other components of the file to see how additional layers and map controls are included.

Beyond Openlayers

There are a number of alternative web mapping options than what have been demonstrated here. They all have strengths and weaknesses and different approaches. 

MapGuide Open Source (http://mapguide.osgeo.org) provides a pre­built web server and web mapping application with a full Graphical User Interface for building your maps (in php or .net) without needing to know any code. 

Mapfish (http://mapfish.org/) builds on openlayers but adds additional libraries to allow you to build more sophisticated applications.

Mapchat (http://mapchat.ca/) uses mapserver and ka­map, an alternative to openlayers and adds the ability to do real­time chat, like a geospatial instant messenger.

Featureserver (http://featureserver.org/) is another cgi executable that works with openlayers to provide full read­ and write access to your data sources. In other words you can allow web­based editing of your postgresql data, such as adding and deleting features or changing attribute text.

This time it really is finalThat's all folks!