Databases and Web Mapping the Open Source Way Open Source GIS
Transcript of Databases and Web Mapping the Open Source Way Open Source GIS
Databases and Web Mapping the Open Source WayOpen Source GIS Masterclass, Lancaster UniversityJanuary 2010
Jo CookOA Digital
[email protected] 880212
Introduction
Introduction
1
2
3
Databases
● Serverbased database (not like access)● Enterpriselevel, with support for
transactions and multiple users
+
www.postgresql.orgpostgis.refractions.net
How to connect to a database
● HOST: localhost, 192.168.3.40, myserver.com● DATABASE● USERNAME● PASSWORD● PORT: Usually 5432
Ways to connect
Getting spatial data into PostgreSQL
● OGR2OGR● SHP2PGSQL
Syntax:
$shp2pgsql /location/of/file.shp tablename s coordinate system W latin1 | psql d database U username W h host p port
Querying data
● SQL: Structured Query Language
SELECT some comma delimited columns FROM your_table WHERE some condition is met;
● Use PSQL from command line or PgAdmin3
Spatial Querying
● Find out which county a point is in:
SELECT name_2 FROM tblcounties WHERE st_within(geomfromtext('point(1.3 54.26)', 4326), the_geom);
● Find out which counties border Lancashire:
SELECT a.name_2 FROM tblcounties a, tblcounties b WHERE b.name_2='Lancashire' AND a.the_geom && b.the_geom;
Map Servers
Types of Map Server
● Minnesota Map Server (aka Mapserver)
cgi program on a web server. Configuration via text files
● Geoserver
javabased program. Configuration via web interface
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 'globalraster' TYPE RASTER STATUS DEFAULT DATA bluemarble.gif ENDEND
Checking the map file
● SHP2IMG
$shp2img m /location/of/mapfile.map o /location/of/outputfile.png
● WITH THE BROWSER
http://yourserver.com/cgibin/mapserv?map=/location/of/mapfile.map&mode=map
Connecting to PostgreSQL from Mapserver
LAYER NAME "province" STATUS ON TYPE POLYGON CONNECTIONTYPE POSTGIS CONNECTION "host=localhost port=5432 dbname=canada user=postgres password=postgres" DATA "the_geom from province"END
Web Servers and Web Pages
Structure of a web page
<html>
<head><title> My Home Page </title>
</head>
<body>HELLO WORLD!
</body>
</html>
Scripting
● SERVERSIDE:
php, asp● CLIENTSIDE:
javascript
And finally...Web Mapping<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/cgibin/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>
Base Mapping and Map Controls
● Can use base data from Google, Microsoft, Yahoo, Openstreetmap etc, but may need to reproject to EPSG 900913 (spherical mercator)
● Can add map controls such as layer switchers, coordinates, reference maps and so on
Beyond OpenLayers
•MapGuide Open Source http://mapguide.osgeo.org•Mapfish http://mapfish.org/•Mapchat http://mapchat.ca/•Featureserver http://featureserver.org/
And Finally...
Jo CookOA Digitalhttp://[email protected]+44 (0)1524 880212
This work is licenced under the Creative Commons AttributionShare Alike 2.0 UK: England & Wales License. To view a copy of this licence, visit http://creativecommons.org/licenses/bysa/2.0/uk/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
Open Source Web Mapping is Great Fun!!!