GRASS Vector Layers: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
No edit summary
Line 172: Line 172:
[[Image:routes.png|frame|Road network and routes 1 and 4]]
[[Image:routes.png|frame|Road network and routes 1 and 4]]


== References ==
References: <references/>
http://grass.osgeo.org/grass63/manuals/html63_user/vectorintro.html
http://grass.osgeo.org/grass63/manuals/html63_user/vectorintro.html

Revision as of 00:06, 23 May 2008

Introduction

Grass documentation provides basic information about vector attribute management, categories of vector features and vector layers.[1]

The aim of this tutorial is to show how to create, manipulate and display different layers of a vector using relational database.

Problem Description

An organization provides information about routes to be driven by drivers. The organization's database consists of

  • roads (road network)
  • routes driven on that roads

Simple approach can be taken to present the routes to drivers. For every route a vector map could be created and overlayed over the road network vector, i.e.

   $ d.vect map=roads
   $ d.vect map=route01 color=green width=2

Above idea has a major flaw. If route data changes or new route is added then vector maps has to be regenerated or removed.

Assuming that route network and routes data are stored in relational database, the database tables can be linked to road network map as its layers. Such layer information could be utilized to display maps, i.e.

   $ d.vect map=roads
   $ d.vect map=roads layer=2 where="route_id=1" color=green width=2

The tutorial reuses road network provided with Spearfish data. To solve problem defined above, the roads data and sample routes needs to be prepared. This is described in next section.

Road Network and Routes Data

Database Tables

Relation database should contain two tables

  • road network table
  • driven routes table

Road network table has to contain at least two columns

  • road segment id usually provided with road network data by vendor
  • vectory category required by Grass to identify vector features

Road network table can be defined like

   create table road_network (
       cat integer,
       id integer   -- road segment id
   );

Routes table contains route identifier and id of road segment driven on a route

   create table route (
       route_id integer,
       rn_id integer    -- references road_network.id
   );

Routes table also has to provide vector category information. This can be realized with a SQL view

   create view route_rn as
   select rn.cat as cat, rn.id as id, r.route_id as route_id
   from route r left join road_network rn on r.rn_id = rn.id;

Above tables and view allow to provide road network and driven routes information. They can be more complicated depending on application but for purpose of this tutorial such minimal approach is being kept.

Road Network Data

Spearfish data provides road network map in roads vector. It contains few hundred vector lines - road network segments. Data associated with this vector are very simple and they have to be extended

  • unique category id for every vector road segment needs to generated
  • unique id has to be assigned to every road segment

Below, vector rn is created with every vector line having unique category

   $ v.category --o in=roads out=tmpmap option=del
   $ v.category --o in=tmpmap out=rn option=add
   $ g.remove vect=tmpmap

Vector rn is connected to DBF file, which is no longer required. Therefore it has to be removed.

   $ v.db.droptable -f rn

Road network table needs to be associated with road network vector (SQLite driver is used with database located in data.sqlite file; of course, any other RDBMS can be used)

   $ v.db.connect -o map=rn driver=sqlite database=data.sqlite table=road_network

Categories of vector rn has to be uploaded into road network table

   $ v.to.db map=rn option=cat

As it was said above, road network data usually is provided with some road segment identifiers. There is no road segment ids in case of Spearfish data. Therefore, for the purpose of this tutorial they will be generated from categories of vector rn. It can be done easily with SQL query

   update road_network set id = cat;

Routes Data

SQL queries below create four sample routes driven on road network provided in Spearfish data.

   insert into route (route_id, rn_id) values (1, 10);
   insert into route (route_id, rn_id) values (1, 411);
   insert into route (route_id, rn_id) values (1, 412);
   insert into route (route_id, rn_id) values (1, 413);
   insert into route (route_id, rn_id) values (1, 414);
   insert into route (route_id, rn_id) values (2, 407);
   insert into route (route_id, rn_id) values (2, 408);
   insert into route (route_id, rn_id) values (2, 410);
   insert into route (route_id, rn_id) values (2, 413);
   insert into route (route_id, rn_id) values (2, 414);
   insert into route (route_id, rn_id) values (3, 10);
   insert into route (route_id, rn_id) values (3, 408);
   insert into route (route_id, rn_id) values (3, 409);
   insert into route (route_id, rn_id) values (3, 415);
   insert into route (route_id, rn_id) values (4, 407);
   insert into route (route_id, rn_id) values (4, 415);
   insert into route (route_id, rn_id) values (4, 9);


Creating Grass Vector Layers

Previous section described in detail how to prepare sample data to illustrate problem defined in this tutorial. To summarize

  • road network vector map exists in Grass
  • road network database table is associated with road network map; it has columns
    • id - identifies road segment id
    • cat - identifies road segment in Grass vector map
  • routes table is created, it has route id and road segment id columns; four routes are defined
  • routes to road network view exists to associate route information with Grass vector data

Route network table is connected as first layer to rn vector. This can be checked with command

   $ v.db.connect -p map=rn

Let's disconnect vector rn from its first layer

   $ v.db.connect -d map=rn layer=1

Now, without copying database table, vector categories can be populated into two layers (or more if required)

   $ v.category --o input=rn layer=2 output=tmprn
   $ g.remove vect=rn
   $ g.rename vect=tmprn,rn

Above step is very important. Vector categories needs to be populated in all layers.

Road network table can be reconnected as first layer and route information as second layer

   $ v.db.connect -o map=rn driver=sqlite database=data.sqlite table=road_network
   $ v.db.connect -o map=rn layer=2 driver=sqlite database=data.sqlite table=route_rn
   $ v.db.connect -p map=rn
   Vector map <rn> is connected by:
   layer <1> table <road_network> in database <data.sqlite> through driver <sqlite> with key <cat>
   layer <2> table <route_rn> in database <data.sqlite> through driver <sqlite> with key <cat>

Road network map and routes no 1 and 4 can be displayed

   $ d.vect map=rn
   $ d.vect map=rn layer=2 color=green width=8 where='route_id=1'
   $ d.vect map=rn layer=2 color=red width=4 where='route_id=4'

which is rendered as on the map presented below

Road network and routes 1 and 4

References

References:

  1. Vector data processing in GRASS GIS.

http://grass.osgeo.org/grass63/manuals/html63_user/vectorintro.html