Worskhop Leicester 2010

31
Beyond Google Maps and Mashups

description

Databases and Web Mapping the Open Source Way

Transcript of Worskhop Leicester 2010

Page 1: Worskhop Leicester 2010

Beyond Google Maps and Mashups

Page 2: Worskhop Leicester 2010

Practicalities

● Download: www.archaeogeek.com/downloads/leics2010.zip

●Save to /home/user and extract

●/home/user/leics2010 folder should then contain:shape_files (folder)demo.mapmapserver.htmlbaselayers.htmlleicester_2010_notes.pdf

Page 3: Worskhop Leicester 2010

Installing Quantum GIS plugins

Needed Plugins: Mapserver Export, PostGIS manager, SPIT

Page 4: Worskhop Leicester 2010

Databases

Page 5: Worskhop Leicester 2010

Databases

● Server-based database (not like access)● Data is entirely separate from user interface● Inside the database, data stored in schemas

for finer control

+

www.postgresql.orgpostgis.refractions.net

Page 6: Worskhop Leicester 2010

How to connect to a database

● HOST: localhost, 192.168.3.40, myserver.com● DATABASE: ??? (your choice)● USERNAME: user● PASSWORD: user● PORT: 5432 (default)

Page 7: Worskhop Leicester 2010

Getting spatial data into PostgreSQL

● OGR2OGR (command line)● SHP2PGSQL (command line)● SPIT plugin for Quantum GIS● Essential Parameters:

host, username, password, dbname, port (as before)

● Additionally: name and location of shapefile, name of table in database, coordinate system of data

Page 8: Worskhop Leicester 2010

Loading data with SPIT

Create new database with pgadmin3and connect to it with SPIT in QGIS

Page 9: Worskhop Leicester 2010

SPIT (continued)

Make “DB Relation Name” all lower case!!!

Page 10: Worskhop Leicester 2010

Database house-keeping

● Each table should have a primary key● Each spatial table should be recorded in

the geometry_columns table● Each spatial table should have a spatial

indexCREATE INDEX yourtable_idx ON yourtable USING GIST(geometry_column);

Page 11: Worskhop Leicester 2010

Querying data

● SQL: Structured Query LanguageSELECT some comma de-limited columns FROM your_table WHERE some condition is met;

● Use PSQL from command line, PgAdmin3, or PostgreSQL Manager/SQL Editor

● SQL is case-sensitive for table and column names, and keywords are usually in capitals

Page 12: Worskhop Leicester 2010

Querying data

● Display on the map using CREATE VIEW AS:

CREATE VIEW viewname AS SELECT * FROM united_kingdom_counties WHERE...

● Add to QGIS using Add Layer/Add PostGIS layer

Page 13: Worskhop Leicester 2010

Spatial Querying

● Find out which county a point is in:SELECT “PROV3NAME” FROM united_kingdom_counties WHERE st_within(geomfromtext('point(-1.4 52.6)', 4326), the_geom);

● Find out the spatial extent of your data:SELECT st_extent(the_geom) FROM united_kingdom_counties;

Page 14: Worskhop Leicester 2010

Map Servers

Page 15: Worskhop Leicester 2010

Types of Map Server

● cgi program on a web server. Configuration via text files

● java-based program. Configuration via web interface

Page 16: Worskhop Leicester 2010

The Map FileMAP NAME "sample" STATUS ON SIZE 600 400 EXTENT -180 -90 180 90 UNITS DD SHAPEPATH "../data" IMAGECOLOR 255 255 255 WEB IMAGEPATH "/ms4w/tmp/ms_tmp/" IMAGEURL "/ms_tmp/" END LAYER NAME 'global-raster' TYPE RASTER STATUS DEFAULT DATA bluemarble.gif ENDEND

Page 17: Worskhop Leicester 2010

Checking and Viewing a map file

● SHP2IMG at the command lineshp2img -m /home/user/leics2010/demo.map -o /home/user/demo.png

● With a browserhttp://localhost.com/cgi-bin/mapserv?map=/home/user/leics2010/demo.map&mode=map

Page 18: Worskhop Leicester 2010

Adding new layers to a mapfileLAYER NAME "your layer" STATUS DEFAULT TYPE POLYGON DATA "yourshapefile.shp" CLASS

NAME "your name for this symbology"STYLE

SYMBOL 0 OUTLINECOLOR 0 0 0 COLOR 207 150 123 END

ENDEND

Page 19: Worskhop Leicester 2010

Connecting to PostgreSQL from Mapserver

LAYER NAME "UK Counties" STATUS DEFAULT TYPE POLYGON CONNECTIONTYPE POSTGIS CONNECTION "host=localhost port=5432 dbname=osgis2010 user=user password=user" DATA "the_geom from united_kingdom_counties" CLASS

…END

END

Page 20: Worskhop Leicester 2010

Displaying single layers

● Set the Layer status to “ON” rather than “DEFAULT”

● Specify the layers as part of the URL:http://localhost/cgi-bin/mapserv?map=/home/user/leics2010/demo.map&layer=counties

Page 21: Worskhop Leicester 2010

Web Mapping

Page 22: Worskhop Leicester 2010

Web Mapping: Practicalities

● Make the web folder writeablesudo chown -R user /var/wwwsudo chmod -R 755 /var/www

● If asked for a password, it's user

Page 23: Worskhop Leicester 2010

Structure of a web page

<html>

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

</head>

<body>HELLO WORLD!

</body>

</html>

Page 24: Worskhop Leicester 2010

Scripting

● SERVER-SIDE:php, asp

<?phpphpinfo();

?>● CLIENT-SIDE:

javascript

Page 25: Worskhop Leicester 2010

Solving Problems using Firefox

Page 26: Worskhop Leicester 2010

OpenLayers

Page 27: Worskhop Leicester 2010

OpenLayers page structure<html>

<head><script src="..lib/OpenLayers.js"></script><script type="text/javascript">

var map, mylayer; 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>

Page 28: Worskhop Leicester 2010

Adding Controls

Scale Bars, Permalink and Mouse Position

Layer Switcher and Overview Map

Page 29: Worskhop Leicester 2010

Base Mapping● Can use base data from Google, Microsoft,

Yahoo, Openstreetmap, etc● Specify base layer (not always necessary)

{'isBaseLayer': true}● Overlay needs to be transparent

{'transparent':true, 'format': png}

● Change addlayers directivemap.addLayers([layer1,layer2]);

Page 30: Worskhop Leicester 2010

Beyond OpenLayers

•MapGuide Open Source http://mapguide.osgeo.org•Mapfish http://mapfish.org/•Mapchat http://mapchat.ca/•Featureserver http://featureserver.org/

Page 31: Worskhop Leicester 2010

And Finally...

Jo CookOA Digitalhttp://[email protected]+44 (0)1524 880212

This work is licenced under the Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License. To view a copy of this licence, visit http://creativecommons.org/licenses/by-sa/2.0/uk/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.

Have fun!!!