<table cellspacing="12" width="100%">
<tr><td colspan="2">
<table width="100%" bgcolor="#f0f0f8">
<tr><td align="center">
<h1>Topology-Geometry: an advanced tutorial</h1>

<table width="100%"><tr>
<td width="33%" align="left"><a
<td align="center"><a to
<td width="33%" align="right"><a

<h3>TopoLayers and TopoFeatures</h3>
In the previous tutorial we've examined a first couple of SQL functions intended to export a GeoTable out from an existing Topology: <b>TopoGeo_ToGeoTable()</b> and <b>TopoGeo_ToGeoTableGeneralize()</b>.<br>
Anyway SpatiaLite supports an more sophisticated (and complex) approach based on <b>TopoLayers</b> and <b>TopoFeatures</b> specifically intended to support a very flexible mechanism for exporting a full set of GeoTable(s) directly deriving from a Topology.
<h4>a conceptual reference framework</h4>
<ul>
<li>an ISO Topology is just intended to support a consistent set of <b>Nodes</b>, <b>Edges</b> and <b>Faces</b> primitives fully covering the plane.
Topology primitives correspond to pure geometries, and can never be directly  associated to a corresponding set of information attributes.
Both Node and Edge primitives directly correspond to a <u><i>real but Face primitives simply correspond to a <u><i>virtual thus necessarily requiring to be dynamically recontructed every time that they are referenced (and this could easily become a real bottleneck).</li>
<li>on the other hand a GeoTable (aka as in the common GIS parlance) is always based on many distinct features, and for each feature a Geometry and an associated set of information attributes are expected to be declared.</li>
<li>there is further difference to be taken in proper account: Topology primitives necessarily are elementary. Feature Geometries can usually represent some complex type: MULTIPOINT, MULTILINESTRING, MULTIPOLYGON and, may be, GEOMETRYCOLLECTION. So it should be absolutely clear that a Feature Geometry does not necessarily corresponds to a single Topology primitive ; a Feature Geometry could easily be derived from aggregating many different Topology primites.</li>
<li>that's not all: many different GeoTables could evenatually be derived from a single Topology; and the same Topology primitive could play a very different role in everyone of these GeoTables.<br>
Just a basically simple example: imagine a Topology corresponding to some cadastral map. We'll surely have many potential layers: buildings, agricultural land, populated places, administrative boundaries, roads, ponds, rivers, fences and so on.<br>
In such a context a fence could easily correspond to a Feature within the layer, could probably delimit two adjacent agricultural areas and could eventually be part of some administrative boundary.</li>
<li><b>TopoLayers</b> and <b>TopoFeatures</b> represent a complex and flexible structure intended to establish permanent explicit correspondences between Topology Primitives and GeoTable Features.
<ul>
<li>each <b>TopoLayer</b> is uniquely identified by its <b>name</b> and directly corresponds to a single GeoTable to be exported from the Topology<br>
An arbitrary number of TopoLayers can be created on the same Topology, and each one of them will act as a separate container.</li>
<li>each <b>TopoFeature</b> is uniquely identified by its <b>fid</b> (<i>feature-id</i>) and corresponds to a single Feature containing an individual set of information attributes.<br>
The Geometry for each TopoFeature is always indirectly defined by specifying a list of Topology primitives individually referenced by their IDs.<br>
Such a geometry obviously is a one, and will be actually materialized (by aggregating all referenced Topology primitives) only when exporting the destination GeoTable.</li>

<li>The TopoLayers / TopoFeatures structure is practically implemented as a set of several closely related DB-tables )we'll examine later their respective layouts in full detail):
<ul>
this first table simply is a catalog of all TopoLayers supporting the same Topology.<br>
Each TopoLayer is identified by its <b>name</b> and by its <b>ID</b>; both identifiers are granted to be
this second table is intended to permanently store all cross-references existing between Topology primitives and TopoFeatures.</li>
for each single TopoLayer a separate table is required. The actual correspondence is established via the <b>TopoLayer-ID</b> suffix.<br>
All these tables are intended to permanently store the information attributes for each TopoFeature in the same TopoLayer.
Different TopoLayers will obviosuly support a different set of information attributes thus requiring an individual table layout.<br>
Anyway all these tables are always expected to declare a <b>Primary Key</b> of the <b>INTEGER</b> type named <b>fid</b> and intended to be an unique identifier for each TopoFeature.</li>

<li>Several SQL functions are specifically intended to support processing operations based on TopoLayers and TopoFeatues; we'll examine later all them in full detail.</li>
</ul><br><br>
<hr>
<h2>a quick practical exercise</h2>
Prerequisites:
<ol>
<li>download the sample DB-file from <a
It contains Census data (2011) kindly released by ISTAT under <b>CC-BY</b> license terms.<br>
The original datasets have been slightly rearranged in a more convenient form.
All geometries are in the <b>SRID 32632</b> (<i>WGS 84 / UTM zone 32N</i>):
<ul>
<li><b>census_2011</b>: all Census Areas (2011) covering Tuscany.<br>
<u>Note:</u> several Census Areas are completely uninhabited (lakes, marshlands, high mountains and alike).</li>
<li><b>ppl_2011</b>: Populated Places (defined as an aggregation of Census Areas).<br>
<u>Note</u>: not all Census Areas belong to a Populated Place: there are many self-standing dispersed rural areas.</li>
<li><b>com_2011</b>: Tuscany Municipalities / Local Councils 2011 (defined as an aggregations of Census Areas).</li>
<li><b>com_2014_15</b>: few new Municipalities created during years 2014/15 by merging pre-existing smaller Municipalities.</li>
<li><b>prov_2011</b>: Tuscany Provinces / Counties (defined as an aggregation of Municipalities).</li>
<li><b>reg_2011</b>: Tuscany Region (defined as an aggregation of Provinces).</li>
<li>just <b>census_2011</b> alone supports geometries; any other administrative level is simply defined by relational codes.</li>

<li>uncompress the downloaded db-file
<li>now you can finally start a SpatiaLite session using your preferred SpatiaLite front end tool:
<ul>
<li><u>Attention</u>: you must not directly connect to you are expected instead to connect a new (empty) DB-file named

</ol>
<verbatim>
SELECT CreateTopology('census2011', 32632, 0, 0);
1

ATTACH DATABASE "./tuscany-census-2011.sqlite" AS istat;

SELECT TopoGeo_FromGeoTable('census2011', 'istat', 'census_2011', NULL, 0, 512, -1);
1

SELECT ST_ValidateTopoGeo('census2011');
NULL

SELECT * FROM TEMP.census2011_validate_topogeo;

SELECT TopoGeo_CreateTopoLayer('census2011', 'istat', 'census_2011', NULL, 'census_areas');
1

DETACH DATABASE istat;
</verbatim>

<br><br>
<table width="100%"><tr>
<td width="33%" align="left"><a
<td align="center"><a to
<td width="33%" align="right"><a