In aRT there are seven types of table, implementing the TerraLib models. They are:
These tables are encapsulated in the class aRTtable, and we can query them using aRTtheme and aRTquerier objects. The three classes are discussed in this document, and we present here how to execute database queries, selecting and joining attributes.
We will create a new layer, populate with some geometries, and, for each type of table, we discuss how to populate the table, and how to make temporal and attributes queries, using the other aRT classes. Note that spatial querier can be done directly using aRTlayer objects.
All non-geometric data in aRT is stored in tables, and the way to exchange this type of information with the database is using aRTtable objects. aRTtable objects are created and opened from aRTlayer objects, using createTable and openTable, respectively. Table data is read from and written to databases using data.frame. row.names are not used to represent IDs in aRT, because IDs can be unsuficient to describe temporal data.
The most basic type of table in aRT is static. Static tables store attributes with no variation in the time, for example the object df below:
To create a static table in a layer we use createTable():
Note that when we create a table it already has 10 rows with the unique ids from the geometries. To add this data to the table we use updateColumns, and it also create new columns using colnames(df).
And finally, getData is used for reading the data from the table:
Note that, as aRT automatically have created the rows, the order of the rows in the result is not the same of the df.
To avoid it, we can create an empty table, and populate it manually. First, we need to use gen=FALSE, to avoid generate the rows of the table:
The argument gen indicates that the function must create one row for each spatial object, and fill it with the id of the spatial object. Now we need to create two columns, one of integer type and other of real type, and then we add some rows to the table.
But addRows only creates new elements in the table, it cannot change the old elements. For example
We can also create columns of string type, and set the maximum size of the string, as:
updateColumns() already calculates the type and the size of the data, before creating the columns.
External tables are tables with no geometry associated. Therefore they are created directly from the database, and we can not use genid. We create external tables also using createTable, as in the next example:
Note that here we define the name of the key and also its length. It can be defined when creating tables from layers too. As texternal is an object of class aRTtable we can use the same functions as described for tables from layers.
Media tables are useful when building databases that will be used in TerraView, or another TerraLib-based GIS. It associates a web page to a double-click in a drawn geometry. This type of table can be created using type="media":
A layer can have one, and only one, media table, and a media table does not have a name. Also, each media table has two, and only two, atributes: object_id, the link to geometries, and media_name, a web address.
A media table can be manipulated as all the other tables, but new columns can not be created. In the next code we associate web pages to each geometry of the layer, and we use addRows() to fill the table.
To check if it is correct, we can use getData():
We can get all the data of a table using getData(). But some operations are useful, for example selecting values that follows a condition, and it can be an attribute, or spatial, or temporal condition. In the case of spatial queries, here we only use the result to get spatial/attribute queries. If you want to see how spatial queries work, see Spatial Queries.
To execute database queries, we need to create aRTtheme objects.
Temporal tables in aRT work as static tables, noting that there are three identifiers, instead of only one in static tables. The two others are the initial and the final time.
These attributes are srtings, but they follows TerraLib model of dates. To convert temporal dates to aRT format we will use toDate(). This function gets as arguments integer variables year = 0, month = 1, day = 1, hour = 0, minute = 0 and second = 0 and returns a string describing the date. It is a bit different from ISOdate.
An event table represents a temporal table which each element has a static geometry and attributes, but it occours in a time interval. When we are using an event table, we do not need static tables because each event is unique, and therefore we can put all attributes in the same table. We will use the same layer to create an event table.
To create an event table, we need to set type="event" at createTable(). The default value of this argument is "static" when creating from layers and "external" when creating directly from databases.
When an event table is created, it already contains three attributes: id, itime and ftime, and they are keys. We recommend not to generate ids (gen=FALSE), because it would also generate itime and ftime, and put zero in all time values (0000-00-00 00:00:00).
To fill the event table we will generate a random attribute value with duration of 59 minutes, all in the same day:
As the table already has three attributes, we need only to create the column value, and then we can add the rows:
Dynamic attribute tables work with geometries where one or more attributes changes with the time. It works such as event tables, with the conceptual difference that the identifier may repeat.
(not implemented yet)
(not implemented yet)
We can get all table data with getData(), but if it is a temporal table, we get it sliced. To do it, we need first to create an aRTtheme object.
Note that the theme has two tables ("static" and "events"), and getData() returns the join of them.
To slice the theme data, we need to create an aRTquerier, with openQuerier(). This function takes as argument chronon, representing the type of slides to be produced. It can be "second", "month", "season", "year", "weekofyear" and others, and the default is "nochronon". To exemplify using aRTquerier, we implement an algorithm to calculate the number of occourences in each hour, and the sums of value. Therefore we need an aRTquerier sliced by hour.
To get data from the querier there are two functions. nextSlide() loads the next slide, returning the number of elements of it, and getData() returns one of the elements of the slide, read from the database. Both functions do not take any argument.