This note assumes

advertisement
1
Lab 1
Installing Initial Software PostgreSQL,PostGIS,OpenJump
for Spatial Databases Course DT249,DT211,DT228
Lecturer Pat Browne
This note describes how to install the course software on Windows XP.
1) Use OpenJump as the graphical client to send queries and view results.
2) Install PostgreSQL/PostGIS
3) Install OpenJump a desktop geographical system
4) Load spatial data in PostgreSQL/PostGIS
For this lab we are really only interested in 1) using OpenJump. The other topics (2-4) are
provided for your information only. You should use Switch to Classic View for
Windows Control Panel.
1) Query PostgreSQL/PostGIS spatial tables in OpenJump
From Windows Start menu launch OpenJUMP 1.6.3 rev.3576 CORE .
From OpenJump main menu make a connection to the postgis database using
File | Open | Datastore Layer
Click on the icon to the right of connection
Make a new connection, by clicking on Add. Use your student number for username and
password as in following:
If logged into cork
Or from lab machine
2
This connection need only be made once, it can be reused for subsequence queries.
Run a query from the main menu: File | Run Datastore Query
When the connection is made, type in the following into the query box:
select asbinary(the_geom) from county;
the_geom
is the standard name for the geometry column in PostgreSQL/PostGIS.
Click OK. OpenJump should retrieve following map from PostgreSQL/PostGIS.
12) Some more OpenJump queries
Display road data.
select asbinary(the_geom) from roads;
Using the SQL Shell (psql) we query the non-spatial (more precisely nongraphical) stored data
select name, km2, area_km2 from county;
3
The columns km2 and area_km2 are the stored values of the perimeter and area
respectively. We will now compare the calculated and stored values of the border of
Meath:
select km2 from county where name = 'Meath';
select perimeter(the_geom) from county where name = 'Meath';
select sum(area(the_geom))/1000000 as “sq Kms” from county AS c where c.name like
'%Dublin%';
From here on the material is informational only:
===========================================================
1) Ensure that you have the latest version of Java runtime (JRE)installed on your
machine, see
http://www.java.com/en/
You can check which version of JRE is on your machine by typing
java –version
At a command prompt.
2) If you have an older version of PostgreSQL on your machine you should uninstall
it. Do not do this step if you have important information in your old PostgreSQL.
When re-installing PostgreSQL on Windows XP you usually need to remove old
directories and the old user, usually postgres.
If you decide to go ahead with this step then you should remove any trace of previous
installs such as old directories and the old user (usually postgres) as follows:
1.1) Stop the PostgreSQL server through Control Panel | Administrative Tools | Services
1.2) Remove PostgreSQL program using Add/Remove Programs from the control panel.
1.3) Remove a user called 'postgres' Control Panel | User Accounts | Advanced |
Advanced | Users Local Users and Groups | Users
1.4) Remove the complete PostgreSQL directory structure C:\Program Files\PostgreSQL
1.5) Remove the directory C:\Documents and Settings\postgres
1.6) Start Button / right click My Computer / Properties / Advanced / Environmental
Variables / Edit Path to remove entry for postgresql delimited by ;.
1.7) On Vista you may have to: Start Button / Run / cmd enter command net user
postgres /delete
1.8) Reboot your machine.
4
3a) Install PostgreSQL 8.4 for Windows XP form memory key
Copy the course software and data from Student_Distrib on boba.ict.ad.it.ie:
Z:\PBrowne\SPATIAL-DATABASES-SOFTWARE-DATA
to memory key and then to your say YOURDRIVE-&-FOLDER.
Navigate to
YOURDRIVE-&-FOLDER\SPATIAL-DATABASES-SOFTWARE-DATA\postgreSQL
Click on : postgresql-8.4.2-1-windows
Follow the dialogs, using defaults. You should use the password postgres for the
super user postgres. Do not launch the StackBuilder.
This would attempt to find PostGIS (and other plugins) on the Web. If you choose to
download from the Internet, then you can use StackBuilder, but you must be sure of
which version you are installing. To use StackBuilder you must be connected to the
Internet, see .
http://www.bostongis.com/PrinterFriendly.aspx?content_name=postgis_tut01
For the moment we will not use this option. We will install PostGIS as a separate step
(next).
Make the account name: postgres
5
Make the account password: postgres
3b) Installing PostgreSQL on Vista and Windows 7
The same as for Windows XP except;
1) You need to deactivate User Access Control from Control Panel
3) Temporarily turn off Windows or other firewalls, internet security, or anti-virus
programs.
4) Install PostgreSQL by right clicking and selecting Run AS Administrator.
Alternatively, you could try to run a Command Prompt as Administrator (by rightclicking on the shortcut), then directly execute the postgresql-8.4.msi from that command
prompt.
3) Install PostGIS
PostGIS is the spatial extension for PostgreSQL. Navigate to the
YOURDRIVE-&-FOLDER\SPATIAL-DATABASES-SOFTWARE-DATA\PostGIS
Click on postgis-pg84-setup-1.4.1-1.exe
Accept defaults, use postgres as the password.
This step will make a spatially enabled database called postgis.
4)In summary of the correct settings
Account name: postgres :superuser and service
Account password: postgres
Port: 5432
The name of PostGIS database is postgis
Later we will set up specific user and accounts and special purpose databases (e.g. for
routing). Please use postgres as your system and database superuser name and
password. The PostGIS manual is at:
http://postgis.refractions.net/documentation/manual-1.4/
For more detail on PostGIS see:
http://postgis.refractions.net/
5) Check PostGIS using pgAdmin
Open the PostgreSQL administration tool called pgAdmin III from the main Windows
Start Menu. You will be prompted for the password (postgres). If you expand the
Object Browser you should see the PostGIS database as below.
6
This indicates that the PostGIS extension is installed.
6) Open the text interface to PostgreSQL
From the Start menu open PostgreSQL’s command window/prompt with:
All Programs | PostGreSQL8.4 | SQL Shell (psql)
Accept default server, database, port, and user name.
Examine the output of the following commands:
\h
-- for help with SQL commands
\?
-- for help with psql commands
\l
-- for listing of databases
\d
-- for listing the tables in a database
\du
-- for listing the users in a database
\dn
-- for listing the schemas in a database
All the above should give results from the default postgres database.
\q
-- the quit command, don’t quit yet
To connect to a database type the following command:
\c postgis -- Connects to a database called postgis
In the next section we will create some a table in the postgis database.
7) A simple table and query in SQL Shell (psql)
We will create, query, and drop a table
create table test ( t integer);
\d
\d test
insert into test(t) values(3) ;
select * from test;
drop table test;
\d
7
8) Check PostGIS in SQL Shell (psql)
The spatial extensions should have been loaded automatically on install. But just in case
they should be checked. Check PostGIS extensions have loaded:
\c postgis -- if not done in section 1.
\dT histogram2d
\dT spheroid
\dT geometry
\df ST_geometry_eq
\df ST_Perimeter
You will know if the extension has been loaded if the above functions and types are
found. The PostGIS SQL/OGC extensions can also be checked using pgAdmin III
9) Load spatial data into PostgreSQL/PostGIS
We often need to interact with PostgreSQL at command prompt level from outside the
DBMS.
Change directory (cd) in the command prompt to:
C:\Program Files\PostgreSQL\8.4\bin
Your screen should look like this:
We will now load spatial data from this command line.
Copy all the SQL files from:
YOURDRIVE-&-FOLDER\SPATIAL-DATABASES-SOFTWARE-DATA \Data
And put them in:
C:\Program Files\PostgreSQL\8.4\bin
-- At PostgreSQL bin command line, load counties into PostgreSQL, you may be
prompted for a password
psql -d postgis -U postgres -f county.sql
The first command will look like this
8
The command will make a county table in the postgis database.
If all goes well you should see a number of INSERT statements.
Now load the roads.sql into PostgreSQL
psql -d postgis -U postgres -f roads.sql
The last two commands should be pasted one at a time into the Command Prompt
Window (not the SQL Shell). These commands will make two tables in the postgis
database. To make sure that the data was loaded type the following query in the SQL
Shell (psql):
select name from county;
Note that the SQL Shell (psql) in black below is distinct from Command
Prompt as shown in white below (your system will not have different colours).
Now perform two text based queries in SQL shell (not in OpenJump, or command
prompt)
select name from county;
select name from roads;
10 Install OpenJump
OpenJump GIS can act as a client for PostgreSQL/PostGIS. OpenJump uses Java, which
you should have installed in step 1).
We use the latest version OpenJUMP-Installer-1.6.3-r3576-CORE.exe from
http://sourceforge.net/projects/jump-pilot/files/latest/download?source=files
SQL queries can be issued and viewed directly from OpenJump. If the data is required in
the database for later use, then the user must explicitly create a table from the result set,
viewing it in OpenJump does not necessarily make the data permanently available to
PostgreSQL. Note all spatial SQL issued in OpenJump needs to be cast asbinary,
we will mention this again.
9
You may increase the memory available to OpenJump by placing -Xmx512m in
OpenJump | Properties | Target
11) Query PostgreSQL/PostGIS tables in OpenJump
From OpenJump main menu make a connection to the postgis database using
File | Open | Datastore Layer
Click on the icon to the right of connection
Make a new connection, by clicking on Add. Use your student number for username and
password as in following:
10
This connection need only be made once, it can be reused for subsequence queries.
Run a query from the main menu: File | Run Datastore Query
When the connection is made, type in the following into the query box:
select asbinary(the_geom) from county;
the_geom
is the standard name for the geometry column in PostgreSQL/PostGIS.
Click OK. OpenJump should retrieve following map from PostgreSQL/PostGIS.
12) Some more OpenJump queries
Display road data.
select asbinary(the_geom) from roads;
Using the SQL Shell (psql) we query the non-spatial (more precisely nongraphical) stored data
select name, km2, area_km2 from county;
The columns km2 and area_km2 are the stored values of the perimeter and area
respectively. We will now compare the calculated and stored values of the border of
Meath:
select km2 from county where name = 'Meath';
11
select perimeter(the_geom) from county where name = 'Meath';
select sum(area(the_geom))/1000000 as “sq Kms” from county AS c where c.name like
'%Dublin%';
In the labs you can set up a connection to Cork as using 147.252.234.32 instead of
localhost.
You can connect to Cork with your student number as follows:
13) Checking the loaded data.
In SQL Shell (psql) you should still be connected to postgis (\c postgis)
Type the following:
\dt county
You should see the county table created by the psql command in the previous section.
Note and index and constraints were automatically created. Note the_geom is the
column where the geometry is stored. Geometry is generally only meaningful when
rendered and viewed graphically. It is not a good idea to query raw geometry in a text
interface, you should use astext(your-geometry)..
Download