[GRASSweb-list]markus: web/grass51/tutorial ex_basic.html,NONE,1.1 ex_complex.html,NONE,1.1 examples.html,1.16,1.17 index.html,1.12,1.13
grass at intevation.de
grass at intevation.de
Wed Feb 12 07:13:28 EST 2003
Author: markus
Update of /grassrepository/web/grass51/tutorial
In directory doto:/tmp/cvs-serv4693
Modified Files:
examples.html index.html
Added Files:
ex_basic.html ex_complex.html
Log Message:
split examples into basic and complex
--- NEW FILE: ex_basic.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<DEFANGED_meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<DEFANGED_meta name="Author" content="Markus Neteler">
<DEFANGED_link rel="stylesheet" type="text/css" href="../../sitestyle.css">
<title>GRASS 5.1 Usage examples</title>
</head>
<body>
<!-- HEADER -->
<table width=100%><tr><td width=80>
<A HREF="index.html">
<DEFANGED_IMG src="../../images/grass.smlogo.gif" alt="HOME" border="0" align=middle></A>
</td><td><H1>
GRASS 5.1 Usage Examples
</H1></td></tr></table>
<!-- END OF HEADER -->
<hr>
<a href="index.html">Tutorial HOME</a> |
<a href="demo.html">Demo LOCATION</a> |
<a href="geom_storage.html">Geometry management</a> |
<a href="attrib_storage.html">Attribute management</a> |
<a href="examples.html">Usage Examples</a> |
<a href="links.html">Links</a>
<hr>
GRASS 5.1 is currently under development. It is neither complete (it still
contains only a few modules) nor it is intended for productional usage.
Development releases are provided for people who want to help test, debug,
and improve GRASS 5.1. Knowing this, please read on.
<p>
<i><b>Disclaimer:</b> These examples described here may work or
not. You are kindly invited to send us further examples and/or
code bugfixes/enhancements.</i>
<P>
<hr>
<a name="toc"></a>
<ol>
<li><a href="#intro">Introduction to Examples</a>
<li><a href="#convert">Convert from/to GRASS 5.0 vector maps</a>
<li><a href="#geometry">Geometry storage in various formats</a>
<li><a href="#attributes">Attribute storage in (external) databases</a>
<li><a href="#3DDWG">Example: Import 3D DXF/DWG vector data and NVIZ
3D vector visualization</a>
<li><a href="#v.extract">Example: Extract vector data from map to new map
with SQL statements (PostGRASS)</a>
<li><a href="#ogr_export">Example: Vector map export data using OGR</a>
<li><a href="#SQL_queries">Example: SQL queries</a>
<li><a href="#shape_conversion">Example: Conversion of SHAPE map to GRASS
5.1 vector map</a>
<li><a href="#map_from_points">Example: Generating a vector map from a point
data set</a>
<li><a href="#db_copy">Example: Copying a database table from one DBMS to another within GRASS 5.1
table</a>
</ol>
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="intro"></a>
Introduction to Examples
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
Before starting with
<h2>Following modules are available at time of this writing</h2> (see
timestamp at bottom, this list may not reflect the latest changes)
<ul>
<li>d.erase d.m d.mon d.path d.rast d.vect d.what.rast d.what.vect d.zoom
<li>db.columns db.copy db.createdb db.describe db.dropdb db.execute
db.tables db.connect db.databases db.drivers db.droptable db.select
postgisdb
<li>g.copy g.gisenv g.list g.mapsets g.region g.remove g.rename
<li>nviz
<li>v.build v.build.polylines v.category v.clean v.convert v.db.connect
v.digit v.extract v.format v.in.ascii v.in.ogr v.in.sites v.info
v.out.ascii
v.out.ogr v.patch v.shape.register v.shape.unregister v.surf.rst
v.topo.check
<li>
If you start a module just by name, a related <a
href=http://mpa.itc.it/radim/g51/d.vect-gui.jpg>GUI window pops up</a>.
<li>
d.m is a new <a href=http://mpa.itc.it/radim/g51/d.m.jpg>display manager</a>.
</ul>
To get debug information, set variable (set to 0 for debug silence) to
1..5:
<div class="code"><pre>
export GRASS_DEBUG_LEVEL=5
</div></pre>
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="convert"></a>
Convert from/to GRASS 5.0 vector maps
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
<b>
Convert 5.0 vector maps to 5.1
</b><p>
You can run 5.1 (at the same time) on a 5.0 location without problems.<br>
To convert 5.0 vector data to 5.1, run:
<div class="code"><pre>
v.convert in=vectormap_from_50 out=vectormap
v.db.connect vectormap
g.region -p vect=vectormap
d.vect vectormap
d.m
</div></pre>
Vector maps from 5.0 and 5.1 do not interfere (different directories),
so you can use the same names.
<p><b>
Convert 5.1 vector map back to GRASS 5.0
</b><p>
<div class="code"><pre>
v.out.ascii -o newmap.clean out=newmap.clean
</div></pre>
Then go to 5.0 and run v.in.ascii + v.support.
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="geometry"></a>
Geometry storage in various formats
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
There are various possibilities to store vector geometry in GRASS 5.1:
<ul>
<li>native 5.1 format (default)
<li>SHAPE format
<li>PostGRASS format (using PostgreSQL/PostGIS)
<li>OGR supported formats (under development)
</ul>
Read on here about <a href=geom_storage.html>storing vector geometry in
GRASS 5.1</a>.
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="attributes"></a>
Attribute storage in (external) databases
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
There are various possibilities to store attributes in GRASS 5.1:
<ul>
<li>DBF file (default)
<li>ODBC connected RDBMS
<li>PostgreSQL database
<li>mySQL database
</ul>
Read on here about <a href=attrib_storage.html>storing attributes in
GRASS 5.1</a>.
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="3DDWG"></a>
Example: Import 3D DXF/DWG vector data and NVIZ 3D vector visualization
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
[for this exercise we use the Spearfish data set from the GRASS web site]<br>
[The WATRTOWR.DXF can be found <a href=http://www.3dcafe.com/asp/architex.asp#DXF>here</a>]
<P>
Since GRASS 5.1 supports 3D vector data, we can easily import DXF and DWG
files and display them with NVIZ:
<div class="code"><pre>
v.in.dwg in=WATRTOWR.DXF out=watertowerXY
</div></pre>
The topology should be build for 'faces' which are (filled) 3D vector
polygons. The individual vector colors and DXF/DWG types are imported
and stored in the related database table (DBF file per default).
To know a bit more about the map, we run:
<div class="code"><pre>
v.info watertowerXY
</div></pre>
To look at the imported XY vector map (the DXF/DWG file is usually
unprojected) with NVIZ, we have to generate a raster DEM for this XY region.
Later we will transform the vector object to UTM coordinates in the
Spearfish region.<br>
As a values for the elevation it is a good idea to select the minimum
(bottom) height of the imported 3D vector object(s). This value can be
reported with 'v.info' (see above, B: bottom value = zmin):
<div class="code"><pre>
g.region vect=watertowerXY -p
r.mapcalc "dem_flat=-1874"
nviz el=dem_flat vect=watertowerXY
</div></pre>
<P>
<b>Transformation to UTM coordinates:</b>
<P>
Most DXF/DWG drawings are done within XY coordinates. To transform them to
a national grid, we can use 'v.transform' with a 4 point transformation.
We select UTM coordinates where to place the water tower and query the
elevation (d.what.rast elevation.dem). We decide that the xy-extension of
the water tower be 20m and it's height 100m (defined later).<br>
The required transformation points file for 'v.transform' may look like this
(L: left, R: right, U: upper, L: lower, N, S, W, E):
<div class="code"><pre>
# XY UTM
#-------
# UL NW
# UR NE
# LR SW
# LL SE
-584 585 598000 4920770
580 585 598020 4920770
580 -600 598020 4920750
-584 -600 598000 4920750
</div></pre>
These values are stored in the ASCII file 'wt.points'. Then the
transformation can be performed directly within the Spearfish location. The
'zcale' parameter is useful to rescale the vector object height to map units
(e.g. 100m tower height) from XY to real-world length. You can use the
output of 'v.info' to calculate a value for 'zcale':<br>
zcale=target_height/(T-B)<br>
The 'zshift' parameter is used to shift the object vertically on top of the
elevation model (use the bottom 'B' value shown by 'v.info' and add the
local elevation). The '-t' flag automatically shifts the DXF/DWG object(s)
to sea level which is useful if the vector objects were composited with
partially negative z values:
<div class="code"><pre>
v.transform -t in=watertowerXY out=watertowerUTM points=wt.points zscale=0.04 zshift=1320
g.region rast=elevation.dem
nviz el=elevation.dem vect=watertowerUTM
</div></pre>
You will have to zoom heavily to find the water tower as it appears to
be very small compared to the surrounding mountains.
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="v.extract"></a>
Example: Extract vector data from map to new map with SQL statements
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
<div class="code"><pre>
#note: strings must be quoted:
v.extract markveggy.shp output=markveggy.1 where="VEGTYPE = 'PS'"
#selection with OR:
# Take care to use parenthesis on both sides of or ( otherwise the result m
# be bogus). Single clause of one comparison should also be enclosed in
# brackets standing on one of the sides of 'or' like in:
# select * from tab where (c1 < 5) or (c2 >1)
v.extract markveggy.shp out=markveggy.1 new=1 where="(VEGTYPE = 'Wi') or (VEGTYPE = 'PS') or (PRIME_TYPE='Wi')"
#to use the new map, you should link attributes to it. This can
#be the table from the parent map. Edit the DB file and add a row for
#the new map (here: markveggy.1)
#Then check connection:
v.db.connect input=markveggy.1
#Query new map with attributes:
d.what.vect -a
#Or you can display with labels attached to the vector: First we check
# from which column labels to display:
db.columns tab=markveggy
d.vect markveggy.1 att=vegtype display=attr lcolor=red
</div></pre>
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="ogr_export"></a>
Example: Vector map export data using OGR
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
#Export of GRASS 5.1 vector map to SHAPE format (generates /tmp/testogr.shp
#and related files):
<div class="code"><pre>
v.out.ogr input=multi typ=line dsn=/tmp layer=testogr
</div></pre>
<p>
#Export to GML format (generates /tmp/testogr..gml file with layer 'testogr'):
<div class="code"><pre>
v.out.ogr input=multi typ=line dsn=/tmp/testogr.gml layer=testogr format=GML
</div></pre>
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="SQL_queries"></a>
Example: SQL queries
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
<b>Example: we want to read attributes from a connected DBF table (see above
how to connect attribute tables):</b><p>
<div class="code"><pre>
#Select from table 'markveggy' (in database 'grass51test') all rows where 'VEGTYPE = "IFA"':
#NOTE: string attributes must be quoted:
echo "select VEGCNP_ID from markveggy where VEGTYPE = 'IFA'" | db.select -hc
#NOTE: for areas such selections only work when area centroids are present!
d.erase
d.vect markveggy.vegtype where="VEGTYPE = 'IFA'"
d.what.vect -a
</div></pre>
<b>Example: we want to read attributes from a PostgreSQL table connected through
ODBC (see above how to connect attribute tables):</b><p>
<div class="code"><pre>
echo "select VEGCNP_ID from markveggy where VEGTYPE = 'IFA'" | db.select -hc
#NOTE: for areas such selections only work when area centroids are present!
d.erase
d.vect markveggy.vegtype where="VEGTYPE = 'Wiii'"
d.what.vect -a
</div></pre>
You see that it is the same! After connecting a GRASS 5.1 vector map, all
db.* modules work in the same way independent from the connected RDBMS.
<p>
Hints for GRASS-PostGIS: see e.g. <a
href="http://grass.itc.it/pipermail/grassuser/2002-November/015822.html">this
thread</a>.
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="shape_conversion"></a>
Example: Conversion of SHAPE map to GRASS 5.1 vector map
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
WARNING: don't try this at home. Still pretty unstable.
<div class="code"><pre>
v.out.ascii markveggy.shp out=test1
v.in.ascii test1 out=newmap
v.info newmap
#NOTE: this cannot work properly as SHAPE contains closed polygons
#v.clean should be used etc...
v.clean newmap out=newmap.clean tool=break,rmdupl
d.vect newmap.clean ty=area,boundary col=red
#we connect this map to the old DBF file:
db.connect database=/ssi0/ssi/neteler/grassdata/botswanaLL/shp
db.describe table=markveggy
#we have to add an entry to the DB file:
cd `g.gisenv GISDBASE`
cd `g.gisenv LOCATION_NAME`
cd `g.gisenv MAPSET`
#if DB file not present, create it. Otherwise add the line:
newmap 1 markveggy VEGCNP_ID /ssi0/ssi/neteler/grassdata/botswanaLL/PERMANENT/shp dbf
#NOTE: it is sufficent to only write changed entries for 'KEY_COLUMN',
# 'path' and 'driver' entries because unchanged entries are inherited
# from the previous line(s). The full DB file make now contain:
markveggy.shp 1 markveggy VEGCNP_ID /ssi0/ssi/neteler/grassdata/botswanaLL/PERMANENT/shp dbf
newmap 1 markveggy
#... which links map 'newmap' with field 1 to table 'markveggy' with CAT_COLUMN
#'VEGCNP_ID' with the DBF file in directory '/ssi0/ssi...' using the 'dbf' driver
#(yes, we need an DB file editor - do you write it for us?)
#verify some settings:
g.gisenv
#query
d.erase
d.vect newmap
d.what.vect newmap
</div></pre>
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="map_from_points"></a>
Example: Generating a vector map from a point data set
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
Assume you have a CSV (comma separated values) file and want to generate a
map from that. That's rather easy. Convert the CSV file to dBase format with
StarOffice or another software. Then store the DBF file into that directory,
where you keep your DBF files for the current LOCATION (maybe where your
SHAPE files live). The map be "BOTSD90.dbf". Connect the DBMI to that
directory:
<div class="code"><pre>
db.connect driver=shp database=/ssi0/ssi/neteler/grassdata/botswanaLL/shp
db.columns BOTSD90
</div></pre>
should display the columns. there must be of course an X and a Y column with
coordinates inside. Then try to query the table to check if it's really
working:
<div class="code"><pre>
echo "select X_COORD, Y_COORD, shp_fid from BOTSD90" | db.select -h
</div></pre>
<p>
Note that "shp_fid" column is generated on the fly. Now we can generate the
vector points map (which was the sites file in GRASS 5.0):
<div class="code"><pre>
echo "select X_COORD, Y_COORD, shp_fid from BOTSD90" | db.select -h |v.in.ascii output=BOTSD90map
</div></pre>
There we are!
<div class="code"><pre>
d.vect BOTSD90map
</div></pre>
<p>
Finally we have to connect the table to the new vector map. Edit the DB file
(if not there, generate it in the MAPSET directory) and add something like:
<div class="code"><pre>
BOTSD90map 1 BOTSD90 shp_fid /ssi0/ssi/neteler/grassdata/botswanaLL/shp shp
</div></pre>
(map field_1 table IDcolumn directory driver)
<p>
Query some points:
<div class="code"><pre>
d.what.vect -a BOTSD90map
</div></pre>
For amusement we export the map as SHAPE map:
<div class="code"><pre>
v.out.ogr BOTSD90map type=point dsn=./ layer=BOTSD90map
</div></pre>
Cheers.
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="db_copy"></a>
Example: Copying a database table from one DBMS to another within GRASS 5.1
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
Here we copy a DBF file from current directory into a PostgreSQL database
table on machine 'pgserver':
<div class="code"><pre>
db.copy from_driver=dbf from_database=./ from_table=census1990wet \
to_driver=pg to_database="host=pgserver,dbname=grass51test" \
to_table=census1990wetB
</div></pre>
Simultaneously we can watch the data arriving in PostgreSQL (launch select
command several times):
<div class="code"><pre>
psql -h pgserver
grass51test=> select count(*) from census1990wetB;
</div></pre>
This may take some time depending on network speed and table size.
<hr>
<P>
Further <a href=links.html>Links</a> (related software, SQL reference etc).
<!-- FOOTER -->
<p>
<hr>
<DIV ALIGN=right>© 2002-2003 Markus Neteler<br>
<a href="mailto:weblist at grass.itc.it">Comments</a> about this page | <a href="../../faq/index.html">FAQ</a> | <a href="../../download.html">Download</a> | <a href="../../support.html">Support</a> | <a href="../../gdp/index.html">Docs</a> | <a href="../../grassdevel.html">Programming</a> | Back <a href=index.html>5.1 Tutorial Home</a><br>
<i>Last change:
$Date: 2003/02/12 12:13:26 $
</i></DIV>
</body>
</html>
--- NEW FILE: ex_complex.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<DEFANGED_meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<DEFANGED_meta name="Author" content="Markus Neteler">
<DEFANGED_link rel="stylesheet" type="text/css" href="../../sitestyle.css">
<title>GRASS 5.1 Usage examples</title>
</head>
<body>
<!-- HEADER -->
<table width=100%><tr><td width=80>
<A HREF="index.html">
<DEFANGED_IMG src="../../images/grass.smlogo.gif" alt="HOME" border="0" align=middle></A>
</td><td><H1>
GRASS 5.1 Usage Examples
</H1></td></tr></table>
<!-- END OF HEADER -->
<hr>
<a href="index.html">Tutorial HOME</a> |
<a href="demo.html">Demo LOCATION</a> |
<a href="geom_storage.html">Geometry management</a> |
<a href="attrib_storage.html">Attribute management</a> |
<a href="examples.html">Usage Examples</a> |
<a href="links.html">Links</a>
<hr>
GRASS 5.1 is currently under development. It is neither complete (it still
contains only a few modules) nor it is intended for productional usage.
Development releases are provided for people who want to help test, debug,
and improve GRASS 5.1. Knowing this, please read on.
<p>
<i><b>Disclaimer:</b> These examples described here may work or
not. You are kindly invited to send us further examples and/or
code bugfixes/enhancements.</i>
<P>
<hr>
<a name="toc"></a>
<ol>
<li><a href="#postgismapserver">Example: Writing the GRASS 5.0 Spearfish
'streams' map into PostGIS for MapServer</a>
<li><a href="#shortestpath">Example: shortest path vector networking with
d.path</a>
</ol>
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="postgismapserver"></a>
Example: Writing the GRASS 5.0 Spearfish 'streams' map into PostGIS for MapServer
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
In this example we want to take the Spearfish 'streams' vector map and write
it to PostGRASS (PostGIS). This map will be accessible by MapServer.<br>
<b>Note:</b> PostGRASS data linked to MapServer support only LINE and POINT
vectors but not POLYGONS (PostGRASS is not doubling boundaries as required
for non-topological OGR/MapServer).
<P>
First we create a new PostGIS database suitable for PostGRASS, then we
convert the old 5.0 'streams' map to 5.1 native format.
<div class="code"><pre>
postgisdb -c spearfish -h grass.itc.it -U postgres -G ./
v.convert input=streams at PERMANENT output=streams
</div></pre>
The category labels are automatically written to a new DBF file.
<br>
Note that there are two small (!) leftover vector lines in the original 5.0
streams map. These you may delete with the new 'v.digit', then the topology
will be clean.
<br>
Now we write it to PostGRASS. For that we have to create a 'frmt' file which
defines the PostGRASS connection and a 'head' file which defines the basic
vector metadata. Note that special characters including '.' are forbidden
(SQL restriction):
<div class="code"><pre>
cd $HOME/grassdata/spearfish/user1/vector
mkdir streams_pg
cd streams_pg
</div></pre>
Here generate the ASCII file 'frmt':
<div class="code"><pre>
FORMAT:postgis
HOST:pgserver.itc.it
DATABASE:spearfish
USER:postgres
GEOM_TABLE:streams_pgg
CAT_TABLE:streams_pgc
</div></pre>
Next we need a 'head' file (you may re-use another 'head' file from another
vector map):
<div class="code"><pre>
ORGANIZATION: GRASS Development Team
DIGIT DATE: 8/23/90
DIGIT NAME: grass
MAP NAME: streams_pg
MAP DATE: 2003
MAP SCALE: 24000
OTHER INFO: Map imported from Spearfish 5.0
ZONE: 0
MAP THRESH: 0.500000
</div></pre>
Then we import the map to PostGRASS:
<div class="code"><pre>
v.out.ascii input=streams out=streams_pg
v.in.ascii input=streams_pg out=streams_pg
</div></pre>
Now we may also want to store the attributes in PostgreSQL. The 'v.convert'
which we used above, had already written a DBF file. This we can copy now to
PostgreSQL into the same database 'spearfish':
<div class="code"><pre>
db.copy from_driver=dbf from_database=$HOME/grassdata/spearfish/user1/dbf \
from_table=streams to_driver=pg \
to_database="host=pgserver.itc.it,dbname=spearfish,user=postgres" \
to_table=streams_atts
</div></pre>
To check the existing tables in PostGIS, we run:
<div class="code"><pre>
db.connect driver=pg database="host=pgserver.itc.it,dbname=spearfish,user=postgres"
db.tables -p
db.describe -c streams_atts
</div></pre>
We have the geometry and the attributes table ready now. Finally missing is
the DB link definition for GRASS which is defined in the 'dbln' file:
<div class="code"><pre>
cd $HOME/grassdata/spearfish/user1/vector/streams_pg
</div></pre>
The 'dbln' file contains the connection parameters such as:
<div class="code"><pre>
#field table ID database DBMIdriver
1 streams_atts CAT host=pgserver.itc.it,dbname=spearfish,user=postgres pg
</div></pre>
Now we should be able to look at and query the map with:
<div class="code"><pre>
d.vect streams_pg col=green
d.what.vect streams_pg
</div></pre>
<P>
<b>MapServer preparations</b><p>
To make MapServer accept the PostGIS data, we need a table
'GEOMETRY_COLUMNS' which links the geometry to the attributes within
PostgreSQL. Generate following SQL ASCII file:
<div class="code"><pre>
CREATE TABLE GEOMETRY_COLUMNS (
F_TABLE_CATALOG VARCHAR(256) NOT NULL,
F_TABLE_SCHEMA VARCHAR(256) NOT NULL,
F_TABLE_NAME VARCHAR(256) NOT NULL,
F_GEOMETRY_COLUMN VARCHAR(256) NOT NULL,
COORD_DIMENSION INTEGER NOT NULL,
SRID INTEGER NOT NULL,
TYPE VARCHAR(30) NOT NULL
);
</div></pre>
and generate this this table in PostGIS within the 'spearfish' database:
<div class="code"><pre>
psql -h pgserver.itc.it -U postgres spearfish < geometry_columns.sql
</div></pre>
<!-- select * from geometry_columns;
\d geometry_columns
-->
Then we fill this table with one row:
<div class="code"><pre>
psql -h pgserver.itc.it -U postgres spearfish
INSERT INTO geometry_columns VALUES ('','spearfish','streams_pgg','geom','2','-1','GEOMETRY');
\q
</div></pre>
The entry order is:<br>
CATALOG empty , SCHEMA database, NAME geomtable, COLUMN geometry_column,
DIMENSION 2, SRID -1, TYPE GEOMETRY
<p>
Finally MapServer requires a mapfile entry similar to this (polygons are
unsupported :
<div class="code"><pre>
LAYER
NAME "streams_pgg"
CONNECTIONTYPE postgis
CONNECTION "user=postgres dbname=spearfish host=127.0.0.1"
# Column geom_table
DATA "geom from streams_pgg"
TYPE LINE
STATUS DEFAULT
CLASS
SYMBOL "pattern1"
NAME "Soils"
OUTLINECOLOR 100 50 0
COLOR 255 100 100
LABEL
POSITION CC
SIZE SMALL
COLOR 255 0 0
END
END
END
</div></pre>
And here you can see how it looks like: <br>
<a href=http://grass.itc.it/cgi-bin/mapserv?map=/var/www/map-script/mapserverjs.map&layer=dem>Demo Online MapServer
reading GRASS raster, SHAPE, GeoTIFF and PostGRASS/PostGIS data</a>
<P>
<b>Add a table 'view'</b><p>
Since we still have the geometry and attributes in different tables, we
can add a 'view' onto both tables (sort of virtual table merging both).
Note that for a 'view' MapServer 3.7 is required. If you use a version < 3.7
you should use 'CREATE TABLE' instead:
<div class="code"><pre>
CREATE VIEW test AS select streams_pgg.id, streams_pgc.cat, streams_pgg.type, streams_pgg.geom \
from streams_pgg,streams_pgc WHERE streams_pgg.id = streams_pgc.id AND \
streams_pgc.field = 1;
INSERT into geometry_columns values ( ' ', ' ', 'test2', 'geom', 2, -1, 'GEOMETRY' );
</div></pre>
Then change the table name in the 'mapfile' (DATA entry) to the view.
Should work... As the 'streams' map does not have exciting attributes, you
may calculate the length of each vector, assign it to the attributes table
and enjoy the map queries.
<p><table width="100%" cellpadding="3" border="0">
<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
<a name="shortestpath"></a>
Example: shortest path vector networking with d.path
<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
</b></big></font></td></tr></table>
[for this exercise we use the Spearfish data set from the GRASS web site]
<P>
A module based on the DGLib vector network library is 'd.path' (see <a
href="http://mpa.itc.it/radim/g51/d.path.jpg">screenshot</a> which
calculates shortest path on a vector network map. Traveling costs may be
either line lengths, or costs saved as attributes in a database table
(supported are cost assignments for both arcs and nodes).
<P> We want to use the 'roads' map of the Spearfish demo location (if
needed, convert to 5.1 format with 'v.convert'). A simple example
is to use the vector length as costs:
<div class="code"><pre>
d.vect roads
d.path map=roads
</div></pre>
More realistic shortest path queries may consider the maximum speed
according to the road type. If desired to use different traveling costs for
the two directions of a road, forward and backward costs are read from two
different table columns. In our example we simply assume the same costs
(same table column) for both directions. Node that you have to hit a node
with the mouse in the GRASS monitor to select start/end point when
using d.path.
As the column 'travelcost' is not yet present in the table 'road' we can use
StarOffice or OpenOffice to add it (see <a href="attrib_storage.html#toc">here</a>).
The we can continue and insert values according to the road type (be sure to
quote strings when using for where statement):
<div class="code"><pre>
echo "select * from roads" | db.select
</div></pre>
... shows that the travelcost column is filled with 0.0. To assign
new travel costs, we run:<br>
<div class="code"><pre>
echo "update roads set travelcost=5 where cat=1" | db.execute
echo "update roads set travelcost=20 where cat=2" | db.execute
echo "update roads set travelcost=40 where cat=3" | db.execute
echo "update roads set travelcost=60 where cat=4" | db.execute
echo "update roads set travelcost=80 where cat=5" | db.execute
echo "select * from roads" | db.select
</div></pre>
Then we can run the 'd.path' with our assigned travel costs:
<div class="code"><pre>
d.path map=roads afcol=travelcost abcol=travelcost
</div></pre>
To see the forward directions of the vector lines, use the 'dir' for the
'display' parameter in 'd.vect'. It will add small arrows indicating the
forward directions when plotting the vector map.
<!-- FOOTER -->
<p>
<hr>
<DIV ALIGN=right>© 2002-2003 Markus Neteler<br>
<a href="mailto:weblist at grass.itc.it">Comments</a> about this page | <a href="../../faq/index.html">FAQ</a> | <a href="../../download.html">Download</a> | <a href="../../support.html">Support</a> | <a href="../../gdp/index.html">Docs</a> | <a href="../../grassdevel.html">Programming</a> | Back <a href=index.html>5.1 Tutorial Home</a><br>
<i>Last change:
$Date: 2003/02/12 12:13:26 $
</i></DIV>
</body>
</html>
Index: examples.html
===================================================================
RCS file: /grassrepository/web/grass51/tutorial/examples.html,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- examples.html 10 Feb 2003 17:32:26 -0000 1.16
+++ examples.html 12 Feb 2003 12:13:26 -0000 1.17
@@ -41,720 +41,9 @@
<hr>
<a name="toc"></a>
<ol>
-<li><a href="#intro">Introduction to Examples</a>
-<li><a href="#convert">Convert from/to GRASS 5.0 vector maps</a>
-<li><a href="#geometry">Geometry storage in various formats</a>
-<li><a href="#attributes">Attribute storage in (external) databases</a>
-<li><a href="#3DDWG">Example: Import 3D DXF/DWG vector data and NVIZ
- 3D vector visualization</a>
-<li><a href="#v.extract">Example: Extract vector data from map to new map
- with SQL statements (PostGRASS)</a>
-<li><a href="#ogr_export">Example: Vector map export data using OGR</a>
-<li><a href="#SQL_queries">Example: SQL queries</a>
-<li><a href="#shape_conversion">Example: Conversion of SHAPE map to GRASS
- 5.1 vector map</a>
-<li><a href="#map_from_points">Example: Generating a vector map from a point
- data</a>
-<li><a href="#db_copy">Example: Copying a database table from one DBMS to another within GRASS 5.1
- table</a>
-<li><a href="#postgismapserver">Example: Writing the GRASS 5.0 Spearfish
- 'streams' map into PostGIS for MapServer</a>
-<li><a href="#shortestpath">Example: shortest path vector networking with
- d.path</a>
+<li><a href="ex_basic.html">Basic examples</a>
+<li><a href="ex_complex.html">More complex examples</a>
</ol>
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="intro"></a>
-Introduction to Examples
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-Before starting with
-
-<h2>Following modules are available at time of this writing</h2> (see
-timestamp at bottom, this list may not reflect the latest changes)
-
-<ul>
-<li>d.erase d.m d.mon d.path d.rast d.vect d.what.rast d.what.vect d.zoom
-<li>db.columns db.copy db.createdb db.describe db.dropdb db.execute
- db.tables db.connect db.databases db.drivers db.droptable db.select
- postgisdb
-<li>g.copy g.gisenv g.list g.mapsets g.region g.remove g.rename
-<li>nviz
-<li>v.build v.build.polylines v.category v.clean v.convert v.db.connect
- v.digit v.extract v.format v.in.ascii v.in.ogr v.in.sites v.info
- v.out.ascii
- v.out.ogr v.patch v.shape.register v.shape.unregister v.surf.rst
- v.topo.check
-<li>
-If you start a module just by name, a related <a
- href=http://mpa.itc.it/radim/g51/d.vect-gui.jpg>GUI window pops up</a>.
-<li>
-d.m is a new <a href=http://mpa.itc.it/radim/g51/d.m.jpg>display manager</a>.
-</ul>
-
-To get debug information, set variable (set to 0 for debug silence) to
- 1..5:
-<div class="code"><pre>
-export GRASS_DEBUG_LEVEL=5
-</div></pre>
-
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="convert"></a>
-Convert from/to GRASS 5.0 vector maps
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-<b>
-Convert 5.0 vector maps to 5.1
-</b><p>
-
-You can run 5.1 (at the same time) on a 5.0 location without problems.<br>
-To convert 5.0 vector data to 5.1, run:
-<div class="code"><pre>
-v.convert in=vectormap_from_50 out=vectormap
-v.db.connect vectormap
-g.region -p vect=vectormap
-d.vect vectormap
-d.m
-</div></pre>
-
-Vector maps from 5.0 and 5.1 do not interfere (different directories),
-so you can use the same names.
-
-<p><b>
-Convert 5.1 vector map back to GRASS 5.0
-</b><p>
-
-<div class="code"><pre>
-v.out.ascii -o newmap.clean out=newmap.clean
-</div></pre>
-
-Then go to 5.0 and run v.in.ascii + v.support.
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="geometry"></a>
-Geometry storage in various formats
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-There are various possibilities to store vector geometry in GRASS 5.1:
-
-<ul>
-<li>native 5.1 format (default)
-<li>SHAPE format
-<li>PostGRASS format (using PostgreSQL/PostGIS)
-<li>OGR supported formats (under development)
-</ul>
-
-Read on here about <a href=geom_storage.html>storing vector geometry in
-GRASS 5.1</a>.
-
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="attributes"></a>
-Attribute storage in (external) databases
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-There are various possibilities to store attributes in GRASS 5.1:
-
-<ul>
-<li>DBF file (default)
-<li>ODBC connected RDBMS
-<li>PostgreSQL database
-<li>mySQL database
-</ul>
-
-Read on here about <a href=attrib_storage.html>storing attributes in
-GRASS 5.1</a>.
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="3DDWG"></a>
-Example: Import 3D DXF/DWG vector data and NVIZ 3D vector visualization
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-[for this exercise we use the Spearfish data set from the GRASS web site]<br>
-[The WATRTOWR.DXF can be found <a href=http://www.3dcafe.com/asp/architex.asp#DXF>here</a>]
-<P>
-Since GRASS 5.1 supports 3D vector data, we can easily import DXF and DWG
-files and display them with NVIZ:
-
-<div class="code"><pre>
-v.in.dwg in=WATRTOWR.DXF out=watertowerXY
-</div></pre>
-
-The topology should be build for 'faces' which are (filled) 3D vector
-polygons. The individual vector colors and DXF/DWG types are imported
-and stored in the related database table (DBF file per default).
-
-To know a bit more about the map, we run:
-<div class="code"><pre>
-v.info watertowerXY
-</div></pre>
-
-To look at the imported XY vector map (the DXF/DWG file is usually
-unprojected) with NVIZ, we have to generate a raster DEM for this XY region.
-Later we will transform the vector object to UTM coordinates in the
-Spearfish region.<br>
-
-As a values for the elevation it is a good idea to select the minimum
-(bottom) height of the imported 3D vector object(s). This value can be
-reported with 'v.info' (see above, B: bottom value = zmin):
-
-<div class="code"><pre>
-g.region vect=watertowerXY -p
-r.mapcalc "dem_flat=-1874"
-nviz el=dem_flat vect=watertowerXY
-</div></pre>
-<P>
-
-<b>Transformation to UTM coordinates:</b>
-<P>
-Most DXF/DWG drawings are done within XY coordinates. To transform them to
-a national grid, we can use 'v.transform' with a 4 point transformation.
-We select UTM coordinates where to place the water tower and query the
-elevation (d.what.rast elevation.dem). We decide that the xy-extension of
-the water tower be 20m and it's height 100m (defined later).<br>
-The required transformation points file for 'v.transform' may look like this
-(L: left, R: right, U: upper, L: lower, N, S, W, E):
-
-<div class="code"><pre>
-# XY UTM
-#-------
-# UL NW
-# UR NE
-# LR SW
-# LL SE
--584 585 598000 4920770
-580 585 598020 4920770
-580 -600 598020 4920750
--584 -600 598000 4920750
-</div></pre>
-
-These values are stored in the ASCII file 'wt.points'. Then the
-transformation can be performed directly within the Spearfish location. The
-'zcale' parameter is useful to rescale the vector object height to map units
-(e.g. 100m tower height) from XY to real-world length. You can use the
-output of 'v.info' to calculate a value for 'zcale':<br>
-zcale=target_height/(T-B)<br>
-
-The 'zshift' parameter is used to shift the object vertically on top of the
-elevation model (use the bottom 'B' value shown by 'v.info' and add the
-local elevation). The '-t' flag automatically shifts the DXF/DWG object(s)
-to sea level which is useful if the vector objects were composited with
-partially negative z values:
-
-<div class="code"><pre>
-v.transform -t in=watertowerXY out=watertowerUTM points=wt.points zscale=0.04 zshift=1320
-g.region rast=elevation.dem
-nviz el=elevation.dem vect=watertowerUTM
-</div></pre>
-
-You will have to zoom heavily to find the water tower as it appears to
-be very small compared to the surrounding mountains.
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="v.extract"></a>
-Example: Extract vector data from map to new map with SQL statements
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-<div class="code"><pre>
-#note: strings must be quoted:
-v.extract markveggy.shp output=markveggy.1 where="VEGTYPE = 'PS'"
-
-#selection with OR:
-# Take care to use parenthesis on both sides of or ( otherwise the result m
-# be bogus). Single clause of one comparison should also be enclosed in
-# brackets standing on one of the sides of 'or' like in:
-# select * from tab where (c1 < 5) or (c2 >1)
-
-v.extract markveggy.shp out=markveggy.1 new=1 where="(VEGTYPE = 'Wi') or (VEGTYPE = 'PS') or (PRIME_TYPE='Wi')"
-#to use the new map, you should link attributes to it. This can
-#be the table from the parent map. Edit the DB file and add a row for
-#the new map (here: markveggy.1)
-#Then check connection:
-v.db.connect input=markveggy.1
-
-#Query new map with attributes:
-d.what.vect -a
-
-#Or you can display with labels attached to the vector: First we check
-# from which column labels to display:
-db.columns tab=markveggy
-d.vect markveggy.1 att=vegtype display=attr lcolor=red
-</div></pre>
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="ogr_export"></a>
-Example: Vector map export data using OGR
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-#Export of GRASS 5.1 vector map to SHAPE format (generates /tmp/testogr.shp
-#and related files):
-
-<div class="code"><pre>
-v.out.ogr input=multi typ=line dsn=/tmp layer=testogr
-</div></pre>
-<p>
-
-#Export to GML format (generates /tmp/testogr..gml file with layer 'testogr'):
-<div class="code"><pre>
-v.out.ogr input=multi typ=line dsn=/tmp/testogr.gml layer=testogr format=GML
-</div></pre>
-
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="SQL_queries"></a>
-Example: SQL queries
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-
-<b>Example: we want to read attributes from a connected DBF table (see above
-how to connect attribute tables):</b><p>
-
-<div class="code"><pre>
-#Select from table 'markveggy' (in database 'grass51test') all rows where 'VEGTYPE = "IFA"':
-#NOTE: string attributes must be quoted:
-echo "select VEGCNP_ID from markveggy where VEGTYPE = 'IFA'" | db.select -hc
-
-#NOTE: for areas such selections only work when area centroids are present!
-d.erase
-d.vect markveggy.vegtype where="VEGTYPE = 'IFA'"
-d.what.vect -a
-</div></pre>
-
-<b>Example: we want to read attributes from a PostgreSQL table connected through
-ODBC (see above how to connect attribute tables):</b><p>
-
-<div class="code"><pre>
-echo "select VEGCNP_ID from markveggy where VEGTYPE = 'IFA'" | db.select -hc
-
-#NOTE: for areas such selections only work when area centroids are present!
-d.erase
-d.vect markveggy.vegtype where="VEGTYPE = 'Wiii'"
-d.what.vect -a
-</div></pre>
-
-You see that it is the same! After connecting a GRASS 5.1 vector map, all
-db.* modules work in the same way independent from the connected RDBMS.
-<p>
-Hints for GRASS-PostGIS: see e.g. <a
-href="http://grass.itc.it/pipermail/grassuser/2002-November/015822.html">this
-thread</a>.
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="shape_conversion"></a>
-Example: Conversion of SHAPE map to GRASS 5.1 vector map
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-WARNING: don't try this at home. Still pretty unstable.
-
-<div class="code"><pre>
-v.out.ascii markveggy.shp out=test1
-v.in.ascii test1 out=newmap
-v.info newmap
-
-#NOTE: this cannot work properly as SHAPE contains closed polygons
-#v.clean should be used etc...
-
-v.clean newmap out=newmap.clean tool=break,rmdupl
-d.vect newmap.clean ty=area,boundary col=red
-
-#we connect this map to the old DBF file:
-db.connect database=/ssi0/ssi/neteler/grassdata/botswanaLL/shp
-db.describe table=markveggy
-
-#we have to add an entry to the DB file:
-cd `g.gisenv GISDBASE`
-cd `g.gisenv LOCATION_NAME`
-cd `g.gisenv MAPSET`
-
-#if DB file not present, create it. Otherwise add the line:
-newmap 1 markveggy VEGCNP_ID /ssi0/ssi/neteler/grassdata/botswanaLL/PERMANENT/shp dbf
-
-#NOTE: it is sufficent to only write changed entries for 'KEY_COLUMN',
-# 'path' and 'driver' entries because unchanged entries are inherited
-# from the previous line(s). The full DB file make now contain:
-markveggy.shp 1 markveggy VEGCNP_ID /ssi0/ssi/neteler/grassdata/botswanaLL/PERMANENT/shp dbf
-newmap 1 markveggy
-
-#... which links map 'newmap' with field 1 to table 'markveggy' with CAT_COLUMN
-#'VEGCNP_ID' with the DBF file in directory '/ssi0/ssi...' using the 'dbf' driver
-#(yes, we need an DB file editor - do you write it for us?)
-
-#verify some settings:
-g.gisenv
-
-#query
-d.erase
-d.vect newmap
-d.what.vect newmap
-</div></pre>
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="map_from_points"></a>
-Example: Generating a vector map from a point data table
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-Assume you have a CSV (comma separated values) file and want to generate a
-map from that. That's rather easy. Convert the CSV file to dBase format with
-StarOffice or another software. Then store the DBF file into that directory,
-where you keep your DBF files for the current LOCATION (maybe where your
-SHAPE files live). The map be "BOTSD90.dbf". Connect the DBMI to that
-directory:
-
-<div class="code"><pre>
-db.connect driver=shp database=/ssi0/ssi/neteler/grassdata/botswanaLL/shp
-db.columns BOTSD90
-</div></pre>
-
-should display the columns. there must be of course an X and a Y column with
-coordinates inside. Then try to query the table to check if it's really
-working:
-
-<div class="code"><pre>
-echo "select X_COORD, Y_COORD, shp_fid from BOTSD90" | db.select -h
-</div></pre>
-<p>
-Note that "shp_fid" column is generated on the fly. Now we can generate the
-vector points map (which was the sites file in GRASS 5.0):
-
-<div class="code"><pre>
-echo "select X_COORD, Y_COORD, shp_fid from BOTSD90" | db.select -h |v.in.ascii output=BOTSD90map
-</div></pre>
-
-There we are!
-<div class="code"><pre>
-d.vect BOTSD90map
-</div></pre>
-<p>
-Finally we have to connect the table to the new vector map. Edit the DB file
-(if not there, generate it in the MAPSET directory) and add something like:
-
-<div class="code"><pre>
-BOTSD90map 1 BOTSD90 shp_fid /ssi0/ssi/neteler/grassdata/botswanaLL/shp shp
-</div></pre>
-(map field_1 table IDcolumn directory driver)
-<p>
-
-Query some points:
-<div class="code"><pre>
-d.what.vect -a BOTSD90map
-</div></pre>
-
-For amusement we export the map as SHAPE map:
-<div class="code"><pre>
-v.out.ogr BOTSD90map type=point dsn=./ layer=BOTSD90map
-</div></pre>
-
-Cheers.
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="db_copy"></a>
-Example: Copying a database table from one DBMS to another within GRASS 5.1
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-Here we copy a DBF file from current directory into a PostgreSQL database
-table on machine 'pgserver':
-
-<div class="code"><pre>
-db.copy from_driver=dbf from_database=./ from_table=census1990wet \
- to_driver=pg to_database="host=pgserver,dbname=grass51test" \
- to_table=census1990wetB
-</div></pre>
-
-Simultaneously we can watch the data arriving in PostgreSQL (launch select
-command several times):
-
-<div class="code"><pre>
-psql -h pgserver
-grass51test=> select count(*) from census1990wetB;
-</div></pre>
-
-This may take some time depending on network speed and table size.
-
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="postgismapserver"></a>
-Example: Writing the GRASS 5.0 Spearfish 'streams' map into PostGIS for MapServer
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-In this example we want to take the Spearfish 'streams' vector map and write
-it to PostGRASS (PostGIS). This map will be accessible by MapServer.<br>
-<b>Note:</b> PostGRASS data linked to MapServer support only LINE and POINT
-vectors but not POLYGONS (PostGRASS is not doubling boundaries as required
-for non-topological OGR/MapServer).
-<P>
-First we create a new PostGIS database suitable for PostGRASS, then we
-convert the old 5.0 'streams' map to 5.1 native format.
-
-<div class="code"><pre>
-postgisdb -c spearfish -h grass.itc.it -U postgres -G ./
-v.convert input=streams at PERMANENT output=streams
-</div></pre>
-The category labels are automatically written to a new DBF file.
-<br>
-Note that there are two small (!) leftover vector lines in the original 5.0
-streams map. These you may delete with the new 'v.digit', then the topology
-will be clean.
-<br>
-Now we write it to PostGRASS. For that we have to create a 'frmt' file which
-defines the PostGRASS connection and a 'head' file which defines the basic
-vector metadata. Note that special characters including '.' are forbidden
-(SQL restriction):
-
-<div class="code"><pre>
-cd $HOME/grassdata/spearfish/user1/vector
-mkdir streams_pg
-cd streams_pg
-</div></pre>
-
-Here generate the ASCII file 'frmt':
-<div class="code"><pre>
-FORMAT:postgis
-HOST:pgserver.itc.it
-DATABASE:spearfish
-USER:postgres
-GEOM_TABLE:streams_pgg
-CAT_TABLE:streams_pgc
-</div></pre>
-
-Next we need a 'head' file (you may re-use another 'head' file from another
-vector map):
-<div class="code"><pre>
-ORGANIZATION: GRASS Development Team
-DIGIT DATE: 8/23/90
-DIGIT NAME: grass
-MAP NAME: streams_pg
-MAP DATE: 2003
-MAP SCALE: 24000
-OTHER INFO: Map imported from Spearfish 5.0
-ZONE: 0
-MAP THRESH: 0.500000
-</div></pre>
-
-Then we import the map to PostGRASS:
-
-<div class="code"><pre>
-v.out.ascii input=streams out=streams_pg
-v.in.ascii input=streams_pg out=streams_pg
-</div></pre>
-
-Now we may also want to store the attributes in PostgreSQL. The 'v.convert'
-which we used above, had already written a DBF file. This we can copy now to
-PostgreSQL into the same database 'spearfish':
-
-<div class="code"><pre>
-db.copy from_driver=dbf from_database=$HOME/grassdata/spearfish/user1/dbf \
- from_table=streams to_driver=pg \
- to_database="host=pgserver.itc.it,dbname=spearfish,user=postgres" \
- to_table=streams_atts
-</div></pre>
-
-To check the existing tables in PostGIS, we run:
-<div class="code"><pre>
-db.connect driver=pg database="host=pgserver.itc.it,dbname=spearfish,user=postgres"
-db.tables -p
-db.describe -c streams_atts
-</div></pre>
-
-We have the geometry and the attributes table ready now. Finally missing is
-the DB link definition for GRASS which is defined in the 'dbln' file:
-
-<div class="code"><pre>
-cd $HOME/grassdata/spearfish/user1/vector/streams_pg
-</div></pre>
-
-The 'dbln' file contains the connection parameters such as:
-<div class="code"><pre>
-#field table ID database DBMIdriver
-1 streams_atts CAT host=pgserver.itc.it,dbname=spearfish,user=postgres pg
-</div></pre>
-
-Now we should be able to look at and query the map with:
-
-<div class="code"><pre>
-d.vect streams_pg col=green
-d.what.vect streams_pg
-</div></pre>
-
-<P>
-<b>MapServer preparations</b><p>
-
-To make MapServer accept the PostGIS data, we need a table
-'GEOMETRY_COLUMNS' which links the geometry to the attributes within
-PostgreSQL. Generate following SQL ASCII file:
-
-<div class="code"><pre>
-CREATE TABLE GEOMETRY_COLUMNS (
- F_TABLE_CATALOG VARCHAR(256) NOT NULL,
- F_TABLE_SCHEMA VARCHAR(256) NOT NULL,
- F_TABLE_NAME VARCHAR(256) NOT NULL,
- F_GEOMETRY_COLUMN VARCHAR(256) NOT NULL,
- COORD_DIMENSION INTEGER NOT NULL,
- SRID INTEGER NOT NULL,
- TYPE VARCHAR(30) NOT NULL
-);
-</div></pre>
-
-and generate this this table in PostGIS within the 'spearfish' database:
-
-<div class="code"><pre>
-psql -h pgserver.itc.it -U postgres spearfish < geometry_columns.sql
-</div></pre>
-
-<!-- select * from geometry_columns;
- \d geometry_columns
--->
-Then we fill this table with one row:
-<div class="code"><pre>
-psql -h pgserver.itc.it -U postgres spearfish
-INSERT INTO geometry_columns VALUES ('','spearfish','streams_pgg','geom','2','-1','GEOMETRY');
-\q
-</div></pre>
-
-The entry order is:<br>
-CATALOG empty , SCHEMA database, NAME geomtable, COLUMN geometry_column,
-DIMENSION 2, SRID -1, TYPE GEOMETRY
-<p>
-
-Finally MapServer requires a mapfile entry similar to this (polygons are
-unsupported :
-
-<div class="code"><pre>
-LAYER
- NAME "streams_pgg"
- CONNECTIONTYPE postgis
- CONNECTION "user=postgres dbname=spearfish host=127.0.0.1"
- # Column geom_table
- DATA "geom from streams_pgg"
- TYPE LINE
- STATUS DEFAULT
- CLASS
- SYMBOL "pattern1"
- NAME "Soils"
- OUTLINECOLOR 100 50 0
- COLOR 255 100 100
- LABEL
- POSITION CC
- SIZE SMALL
- COLOR 255 0 0
- END
- END
-END
-</div></pre>
-
-And here you can see how it looks like: <br>
-<a href=http://grass.itc.it/cgi-bin/mapserv?map=/var/www/map-script/mapserverjs.map&layer=dem>Demo Online MapServer
-reading GRASS raster, SHAPE, GeoTIFF and PostGRASS/PostGIS data</a>
-
-<P>
-<b>Add a table 'view'</b><p>
-
-Since we still have the geometry and attributes in different tables, we
-can add a 'view' onto both tables (sort of virtual table merging both).
-Note that for a 'view' MapServer 3.7 is required. If you use a version < 3.7
-you should use 'CREATE TABLE' instead:
-
-<div class="code"><pre>
-CREATE VIEW test AS select streams_pgg.id, streams_pgc.cat, streams_pgg.type, streams_pgg.geom \
- from streams_pgg,streams_pgc WHERE streams_pgg.id = streams_pgc.id AND \
- streams_pgc.field = 1;
-INSERT into geometry_columns values ( ' ', ' ', 'test2', 'geom', 2, -1, 'GEOMETRY' );
-</div></pre>
-
-Then change the table name in the 'mapfile' (DATA entry) to the view.
-Should work... As the 'streams' map does not have exciting attributes, you
-may calculate the length of each vector, assign it to the attributes table
-and enjoy the map queries.
-
-<p><table width="100%" cellpadding="3" border="0">
-<tr><td align="left" bgcolor="#B0F0B0"><font color="#00000"><big><b>
-<a name="shortestpath"></a>
-Example: shortest path vector networking with d.path
-<DIV ALIGN=right><a href="#toc">[UP]</a></DIV>
-</b></big></font></td></tr></table>
-
-[for this exercise we use the Spearfish data set from the GRASS web site]
-<P>
-
-A module based on the DGLib vector network library is 'd.path' (see <a
-href="http://mpa.itc.it/radim/g51/d.path.jpg">screenshot</a> which
-calculates shortest path on a vector network map. Traveling costs may be
-either line lengths, or costs saved as attributes in a database table
-(supported are cost assignments for both arcs and nodes).
-
-<P> We want to use the 'roads' map of the Spearfish demo location (if
-needed, convert to 5.1 format with 'v.convert'). A simple example
-is to use the vector length as costs:
-
-<div class="code"><pre>
-d.vect roads
-d.path map=roads
-</div></pre>
-
-More realistic shortest path queries may consider the maximum speed
-according to the road type. If desired to use different traveling costs for
-the two directions of a road, forward and backward costs are read from two
-different table columns. In our example we simply assume the same costs
-(same table column) for both directions. Node that you have to hit a node
-with the mouse in the GRASS monitor to select start/end point when
-using d.path.
-
-As the column 'travelcost' is not yet present in the table 'road' we can use
-StarOffice or OpenOffice to add it (see <a href="attrib_storage.html#toc">here</a>).
-The we can continue and insert values according to the road type (be sure to
-quote strings when using for where statement):
-
-<div class="code"><pre>
-echo "select * from roads" | db.select
-</div></pre>
-... shows that the travelcost column is filled with 0.0. To assign
-new travel costs, we run:<br>
-
-<div class="code"><pre>
-echo "update roads set travelcost=5 where cat=1" | db.execute
-echo "update roads set travelcost=20 where cat=2" | db.execute
-echo "update roads set travelcost=40 where cat=3" | db.execute
-echo "update roads set travelcost=60 where cat=4" | db.execute
-echo "update roads set travelcost=80 where cat=5" | db.execute
-echo "select * from roads" | db.select
-</div></pre>
-
-Then we can run the 'd.path' with our assigned travel costs:
-<div class="code"><pre>
-d.path map=roads afcol=travelcost abcol=travelcost
-</div></pre>
-
-To see the forward directions of the vector lines, use the 'dir' for the
-'display' parameter in 'd.vect'. It will add small arrows indicating the
-forward directions when plotting the vector map.
-
-<hr>
-<P>
-Further <a href=links.html>Links</a> (related software, SQL reference etc).
<!-- FOOTER -->
<p>
Index: index.html
===================================================================
RCS file: /grassrepository/web/grass51/tutorial/index.html,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- index.html 10 Feb 2003 17:34:29 -0000 1.12
+++ index.html 12 Feb 2003 12:13:26 -0000 1.13
@@ -34,23 +34,21 @@
Per default all GRASS 5.1 vectors are stored in native 5.1 format within the
directory $GISDBASE/$LOCATION_NAME/$MAPSET/vector/$MAP/. All map related
-data such as geometry data, spatial index, DB-connection info etc. are
-stored in this directory. The default settings (DBMI driver and DATABASE)
-can be modified with 'v.database'.
-See <i>Geometry management</i> below for details.
-<P>
+data such as geometry data, spatial index, DB-connection (dbln file) info
+etc. are stored in this directory. Maps can be read from SHAPE files
+directly, read and written to PostGRASS (using PostGIS) and in future
+through OGR. See <i>Geometry management</i> below for details. <P>
<b>Default settings for vector attributes</b><P>
Per default all GRASS 5.1 vector attributes are stored in a DBF table within
the directory $GISDBASE/$LOCATION_NAME/$MAPSET/dbf. The default settings
-(DBMI driver and DATABASE) can be modified with 'v.database' (for the vector
-modules) and 'db.connect' (for the db.* modules).
+(DBMI driver and DATABASE) can be modified with 'v.database'.
See <i>Attribute management</i> below for details.
<h2>GRASS 5.1 Tutorial</h2>
-
+<a name="toc"></a>
<ol>
<li><a href="demo.html">A demo tour through GRASS 5.1</a>
<li>Vector data management in GRASS 5.1:
@@ -58,16 +56,20 @@
<li><a href="geom_storage.html">Geometry management, optionally with connections to DBMS</a>
<li><a href="attrib_storage.html">Attribute management with connections to DBMS</a>
</ul>
-<li><a href="examples.html">Examples how to use the GRASS 5.1 vector engine</a>
-<li><a href="techdocs.html">Some vector API docs</a> (not recommended to read)
+<li>Usage examples:
+<ul>
+ <li><a href="ex_basic.html">Basic examples</a>
+ <li><a href="ex_complex.html">Complex examples</a>
+</ul>
<li><a href="links.html">Links</a>
+<li> Appendix: <a href="techdocs.html">Some vector API docs</a> (not recommended to read)
</ol>
<h3>Tutorial Quick Guide</h3>
-The table does not refer to all <a href="examples.html">examples</a>, please
+The table does not refer to all <a href="#toc">examples</a>, please
also check the individual tables of contents. You can combine the different
storage methods for geometry and attributes. Per default "Native format"
geometry storage and DBF attribute storage is used.
More information about the grass-web
mailing list