
*********************************************************
*********************************************************
INSTALLATION
*********************************************************

Copy class materials
create c:/class dir
create c:/class/tmp dir
copy data dir into to c:/class (so final result is c:/class/data)

copy the entire dir ms4w (in software) to c:/ (so final result is c:/ms4w)
install MS4W page 35 ff
Install Notepad++ + syntax highlighting
Install Firefox and IRC + Firebug add ons

Later
Install PostgreSQL + PostGIS 
Install QGIS -> Plugin for MapServer
# Install OSGEO4W -> local Rep QGIS ...


Mapserver syntax highlighting for Notepad++
https://github.com/AstunTechnology/MapserverSyntaxHighlighting/tree/master/Notepad%2B%2B
    userDefineLang.xml -> %appdata%\Notepad++
    mapfile.xml -> %programfiles(x86)%\Notepad++\plugins\APIs


*********************************************************
*********************************************************
OpenLayers
*********************************************************

OL 2.13 + OL 3 show examples - local

Before editing OL etc
show note pad column mode
Using
Alt + Mouse dragging   or
Alt + Shift + Arrow keys
to switch to column mode:
https://notepad-plus-plus.org/features/column-mode-editing.html


OL Examples (version 2.13) on page 32 + 111 
"OL examples included on the class DVD".

To make this work locally on your machine you need to load the following shape file into your postgis osgis database:
c:/class/data/layers/officelocations.shp

shp2pgsql -I -s 2285 c:/class/data/layers/officelocations.shp  officelocations | psql -U osgis -d osgis


--  Identify Features Example (map.html on DVD)
http://osgis.terragis.net/identify/map.html

--  Digitize Polygons Example (mapserver_wa_digitize.php and save_polygon.php on DVD and live example)
http://osgis.terragis.net/mapserver_wa_digitize.php

--  Measure Distance and Area Example and control documentation
http://osgis.terragis.net/identify/map_controls.html

-- Click Event Handler Example. This event can be used to trigger any action to write inside click event script. For example
we could query PostGIS, retrieve feature attributes and open a pop-up or web page.

Dynamic
http://osgis.terragis.net/identify/map_dynamic.html

Buffer
http://osgis.terragis.net/identify/map_staticbuffer.html


*********************************************************
*********************************************************
MapServer
*********************************************************
 
#Introduce MapServer page 33

is MS4W installed --> 
Notepad ++ highlighting --> 
run cmd as Administrator
run setenv.bat ! 
-- better yet
-- set path in system env variables

-- Control Panel -->  System and Security -->  System -->  Advanced system settings -->  Environment variables. 

-- add to PATH after all that is in there already 
c:\ms4w\Apache\cgi-bin;c:\ms4w\tools\gdal-ogr;c:\ms4w\tools\mapserv;c:\ms4w\tools\shapelib;c:\ms4w\proj\bin;c:\ms4w\tools\shp2tile;c:\ms4w\tools\shpdiff;c:\ms4w\tools\avce00;c:\ms4w\python\gdal;c:\ms4w\tools\php;c:\ms4w\tools\mapcache;c:\ms4w\tools\berkeley-db;c:\ms4w\tools\sqlite;c:\ms4w\tools\spatialite;c:\ms4w\tools\unixutils;c:\Program Files\PostgreSQL\9.4\bin\;

# test map file
shp2img -m c:/class/data/mapfiles/wacounties_wms.map -l "Counties Rail" -o counties_rail_map.png

Utilities 

# Prepare shape file -tiles
shp2tile - cut shape into tiles
tile4ms - create tile index shapefile for use with MapServers TILEINDEX feature. This will create a shapefile of rectangular extents of all shapefiles listed
or with from the ogr package: ogrtindex

tiled shapes + tileindex shape
c:/class/data/layers/catalog/hydrography/rivers/wa_rivers_1k/index.dbf



WMS page 42
*********************************************************

GetMap
http://localhost/cgi-bin/mapserv--> map=/var/www/sites/mapdata/projects/alliance/alliance_background.map&version=1.1.1&service=WMS&request=GetMap&layers=States&srs=EPSG:2285&bbox=630000,-540000,2700000,780000&format=image/png&width=400&height=300&styles=default

can be full map e.g. many layers

http://sql.usgeocoder.com/cgi-bin/mapserv.fcgi--> map=/home/sql2/cgi-bin/data/maps/oi1_rtest.map&LAYERS=oceanpoly%2Cworld%2CLand%2Cplaces%2CIndian_Reservations%2Cfed_lands%2Cwilderness%2Cus_mil%2CParks%2Clevel_rivers%2Clakes%2CMajor_Cities%2Croads%2CRailway%2Chwy%2Cplaces_outline%2CCounty%2CCity_Names%2CState%2CState_Labels%2Clandmarks%2Cplaces_labels&TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&FORMAT=image%2Fpng&SRS=EPSG%3A4326&BBOX=-122.31732592968,47.62915289949,-122.29853231366,47.647079117846&WIDTH=650&HEIGHT=620

http://sql.usgeocoder.com/cgi-bin/mapserv.fcgi--> map=/home/sql2/cgi-bin/data/maps/oi1_rtest.map&LAYERS=oceanpoly%2Cworld%2CLand%2Cplaces%2CIndian_Reservations%2Cfed_lands%2Cwilderness%2Cus_mil%2CParks%2Clevel_rivers%2Clakes%2CMajor_Cities%2Croads%2CRailway%2Chwy%2Cplaces_outline%2CCounty%2CCity_Names%2CState%2CState_Labels%2Clandmarks%2Cplaces_labels&TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&FORMAT=image%2Fpng&SRS=EPSG%3A4326&BBOX=-122.39350738711,47.556487509325,-122.22235085623,47.719744508011&WIDTH=650&HEIGHT=620

http://sql.usgeocoder.com/cgi-bin/mapserv.fcgi--> map=/home/sql2/cgi-bin/data/maps/oi1_rtest.map&LAYERS=places%2CIndian_Reservations%2Cfed_lands%2Cwilderness%2Cus_mil%2Clevel_rivers%2Clakes%2CMajor_Cities%2Croads%2CRailway%2Chwy%2CCounty%2CCity_Names%2CState%2CState_Labels%2Clandmarks%2Cplaces_labels&TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&FORMAT=image%2Fpng&SRS=EPSG%3A4326&BBOX=-123.9355040117,46.085659959718,-120.68035423164,49.190572057618&WIDTH=650&HEIGHT=620

GetCapabilities 
http://localhost/cgi-bin/mapserv? map=/var/www/sites/mapdata/projects/alliance/alliance_background.map&VERSION=1.1.1&REQUEST=getCapabilities&SERVICE=wms

GetLegend
http://localhost/cgi-bin/mapserv--> map=/var/www/sites/mapdata/projects/alliance/alliance_boundaries.map&version=1.1.1&service=wms&request=getlegendgraphic&layer=Tract_Boundaries&format=image/png

GetfeatureInfo
form http://osgis.terragis.net/identify/map.html
http://osgis.terragis.net/cgi-bin/mapserv--> map=/var/www/sites/ol/osgis/identify/identify.map&LAYERS=locations&QUERY_LAYERS=locations&STYLES=&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&BBOX=-13951474.858337%2C5657257.264282%2C-13095380.141663%2C6391052.735718&FEATURE_COUNT=10&HEIGHT=600&WIDTH=700&FORMAT=image%2Fpng&INFO_FORMAT=text%2Fhtml&SRS=EPSG%3A900913&X=320&Y=302


*********************************************************
*********************************************************
POSTGIS
*********************************************************

-- use psql command line and other utility programs
-- see c:\Program Files\PostgreSQL\9.4\bin\
-- 
-- set path in system environment variables
-- 
	
-- Control Panel -->  System and Security -->  System -->  Advanced system settings -->  Environment variables. 

-- add to PATH after all that is in there already 
c:\Program Files\PostgreSQL\9.4\bin\;
and
c:\ms4w\Apache\cgi-bin;c:\ms4w\tools\gdal-ogr;c:\ms4w\tools\mapserv;c:\ms4w\tools\shapelib;c:\ms4w\proj\bin;c:\ms4w\tools\shp2tile;c:\ms4w\tools\shpdiff;c:\ms4w\tools\avce00;c:\ms4w\python\gdal;c:\ms4w\tools\php;c:\ms4w\tools\mapcache;c:\ms4w\tools\berkeley-db;c:\ms4w\tools\sqlite;c:\ms4w\tools\spatialite;c:\ms4w\tools\unixutils;

-- Installaltion PostgreSQL + PostGIS

-- create new db user role as superuser and assign pw
createuser -U postgres -P -s osgis

create DB osgis:
createdb -U osgis osgis

-- connecting to a db using postgres user
psql -U postgres -d osgis
-- after that any sql or psql command can be used

-- connect to new db osgis using osgis user
psql -U osgis

-- create Postgis extention in osgis db
CREATE EXTENSION postgis;

--psql command to list all tables 
\dt

-- psql command list all column of table counties2008
\d counties2008

- Running SQL from file
psql -U postgres -d osgis -f thepath/myfile.sql



create table points ( pt geometry, name varchar );
insert into points values ( 'POINT(0 0)', 'Origin' );
insert into points values ( 'POINT(5 0)', 'X Axis' );
insert into points values ( 'POINT(0 5)', 'Y Axis' );

select name, st_astext(pt), st_distance(pt, 'POINT(5 5)') from points;


-- import county shape file:
shp2pgsql -I -s 2285 counties2008.shp counties2008 > counties.sql
psql -U postgres -f counties.sql osgis
-- or
shp2pgsql -I -s 2285 counties2008.shp counties2008 | psql -U osgis -d osgis
NOTE switches:
-a  for append mode 
-W latin1 for encoding

-- show Shapefile and dbf table loader
--- create desktop shortcut
C:\Program Files\PostgreSQL\9.4\bin\postgisgui\shp2pgsql-gui.exe

--Select one spatial feature and reproject :
select st_astext(st_transform(geom,4326)) from counties2008 limit 1;

-- Intersect of a point with counties to get name:
select NAME from counties2008 where 
st_intersects(counties2008.geom,st_transform(((st_setsrid((st_MakePoint(-122.206834,47.611421)),4326))),2285));
-- counties2008.geom && st_transform(((st_setsrid((st_MakePoint(-122.206834,47.611421)),4326))),2285) and 

-- Creating an empty table called locations via SQL
CREATE TABLE locations
(
id int,
name varchar(30),
latitude float8,
longitude float8,
gid serial);

-- change owner to osgis

ALTER TABLE locations OWNER TO osgis;

copy locations (id, name, latitude, longitude) from 'c:/class/data/layers/point/locations.csv' using delimiters ','  with null as '';

select AddGeometryColumn('','locations','geom',4326,'POINT',2);
OR
ALTER TABLE locations ADD COLUMN geom geometry(Point,4326);

-- Update new column with features generated by the Makepoint function , with EPSG (SRID) 4326 ( thats decimal degrees)
update locations set geom = ST_SETSRID(st_Makepoint(longitude,latitude),2285);

-- Creating a spatial index
create index locations_gidx on locations using gist(geom);

-- create unique id field 
-- Add Primary Key on gid
ALTER TABLE locations ADD CONSTRAINT pkey_id PRIMARY KEY(id);

-- select all points within specified counties
select counties2008.name, locations.id, locations.name from locations, counties2008 where 
st_intersects(counties2008.geom,st_transform(locations.geom,2285)) and counties2008.name IN ('King','Pierce') order by locations.id ;

--Select one spatial feature and reproject :
select astext(st_transform(the_geom,4326)) from counties2008 limit 1;

-- reproject two points from geographic (SRID 4326) to spherical Mercator projection (SRID 3857) and output as text
select st_astext(st_transform((st_SETSRID(st_Makepoint(84.6,41.8),4326)),3857));


-- Load member data (for mapserver_wa_digitize.php OL Example
-- http://localhost/osgis/other/mapserver_wa_digitize.php
-- file at c:/ms4w/apps/osgis/other/mapserver_wa_digitize.php )
- Running SQL from file
psql -U osgis -d osgis -f c:/class/data/layers/point/members.sql

-- use pgAdmin to add id column as type serial
-- Add Primary Key on id
ALTER TABLE members ADD CONSTRAINT member_pkey_id PRIMARY KEY(id);

-- Creating the spatial index
create index members_gidx on members using gist(the_geom);



-- Internal query planner
VACUUM ANALYZE;

Page 63 review 