Wiki page
[topo-advanced] by
sandro
2015-11-16 13:27:56.
D 2015-11-16T13:27:56.877
L topo-advanced
P 7108405b00a2c828166603c265e0de9e6486076c
U sandro
W 15484
<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>
</td></tr></table>
<table width="100%"><tr>
<td width="33%" align="left"><a href="https://www.gaia-gis.it/fossil/libspatialite/wiki?name=topo-intermediate">previous page</a></td>
<td align="center"><a href="https://www.gaia-gis.it/fossil/libspatialite/wiki?name=misc-docs">back to index</a></td>
<td width="33%" align="right"><a href="https://www.gaia-gis.it/fossil/libspatialite/wiki?name=topo-advanced">next page</a></td>
</tr></table>
<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 geometry</i></u>, but Face primitives simply correspond to a <u><i>virtual geometry</i></u> 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 <u><i>layer</i></u>, 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 "<i><b>fences</b></i>" 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 <u><i>virtual</i></u> one, and will be actually materialized (by aggregating all referenced Topology primitives) only when exporting the destination GeoTable.</li>
</ul></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>
<li><b><<i>topo-prefix</i>>_topolayers</b>: 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 <i><u>unique</u></i>.</li>
<li><b><<i>topo-prefix</i>>_topofeatures</b>: this second table is intended to permanently store all cross-references existing between Topology primitives and TopoFeatures.</li>
<li><b><<i>topo-prefix</i>>_topofeatures_<<i>topolayer-id</i>></b>: 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>
</ul></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 href="https://www.gaia-gis.it/gaia-sins/toposamples/tuscany-census-2011.7z">here</a><br>
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>
</ul></li>
<li>uncompress the downloaded db-file (<b><i>tuscany-census-2011.sqlite</i></b>).</li>
<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 <b><i>tuscany-census-2011.sqlite</i></b>; you are expected instead to connect a new (empty) DB-file named <b>tuscany-topo-2011.sqlite</b></li>
</ul></li>
</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
SELECT TopoGeo_InitTopoLayer('census2011', 'istat', 'pop_ppl_2011', 'ppl');
1
SELECT TopoGeo_InitTopoLayer('census2011', 'istat', 'pop_com_2011', 'com');
1
SELECT TopoGeo_InitTopoLayer('census2011', 'istat', 'pop_prov_2011', 'prov');
1
SELECT TopoGeo_InitTopoLayer('census2011', 'istat', 'pop_reg_2011', 'reg');
1
DETACH DATABASE istat;
</verbatim>
We'll start this practical tutorial by duly replicating the same steps we've already examined in the intermetiate tutorial:
<ul>
<li>we'll create a new 2D Topology named <b>census2011</b> and located into <b>SRID 32632</b>.</li>
<li>then we'll <i>attach</i> the <b>tuscany-topo-2011.sqlite</b> external DB-file.</li>
<li>now we'll populate the <b>census2011</b> Topology by importing the <b>istat.census_2011</b> GeoTable, and then we'll immediatly check if this Topology is fully valid.</li>
</ul><br>
Now we are finally ready to start defining all <b>TopoLayers</b> based on the <b>census2011</b> Topology; each single TopoLayer will precisely represent some administrative level, and all TopoLayers alltogether will completely represent the whole Tuscan administrative hierarchy.
<ol>
<li>so we'll duly invoke <b>TopoGeo_CreateTopoLayer()</b> in order to completely define a first TopoLayer representing <b>Census Areas</b>.<br>
<u>Recall</u>: the <b>istat.census_2011</b> is the unique real GeoTable we have in our initial dataset, because it contains both information attributes and geometries; and each Census Area is expected to directly correspond to a single Topology Face. (<i>we'll examine later in more depth how TopoLayer / TopoFeature relationships have been actually defined</i>).</li>
<li>then we'll continue by defining several further TopoLayers respectively corresponding to <b>Populated Places</b>, <b>Municipalities</b>, <b>Provinces</b> and <b>Region</b> administrative levels.<br>
<u>Recall</u>: all these administrative levels in the initial dataset are simply defined by relational codes intended to aggregate lower level entities; in this case there are no explicit geometries at all. So are are not directly allowed to create a fully defined TopoLayer, and calling <b>TopoGeo_CreateTopolayer()</b> is just impossible because we lack any appropriate Geometry column.<br>
Anyway we can invoke <b>TopoGeo_InitTopoLayer()</b>; this will initialize an only partially defined TopoLayer, but we can easily complete in a second time any missing information so to get a properly working TopoLayer (<i>as we'll see later in full detail).</li>
<li>finally we'll <i>detach</i> the now useless external db-file.</li>
</ol>
<table width="100%" cellspan="4" cellpadding="6" bgcolor="#f8f8f0" border="1">
<tr><td colspan="2" align="center"><b>TopoGeo_ExportTopoLayer: exporting a full TopoLayer into a GeoTable</b></td></tr>
<tr><td>
<verbatim>
SELECT TopoGeo_ExportTopoLayer('census2011', 'census_areas', 'out_census_2011', 1);
</verbatim>
As you can easily check by yourself a new GeoTable named <b>out_census_2011</b> has been created by <b>TopoGeo_ExportTopoLayer()</b>:
<ul>
<li>this GeoTable contains all TopoFeatures defined into the TopoLayer.</li>
<li>each TopoFeature faithfully preserves its initial information attributes.</li>
<li>all Geometries are precisely built by aggregating the corresponding Topology primitives as specified by TopoLayer / TopoFeatures relationships.</li>
</ul><br><br>
The image examplies a choropleth centered around the Island of Elba and based on the <b>out_census_2011</b> GeoTable: all Census Areas are displayed by adopting a colour directly corresponding to their population density.
</td><td>
<img src="https://www.gaia-gis.it/gaia-sins/topofigs/elba-choropleth.png" alt="elba-chloropleth">
</td></tr>
</table><hr>
<h3>TopoLayers / TopoFeatures: layout of DB Tables and corresponding relationships</h3>
<b><<i>topo-prefix</i>>_topolayers</b> e.g. "<i>census2011_topolayers</i>"
<table cellspan="4" cellpadding="6" bgcolor="#f8f8f0" border="1">
<tr><td bgcolor="#f0f0f8"><b>topolayer_id</b></td>
<td bgcolor="#f0f0f8"><b>topolayer_name</b></td></tr>
<tr><td align="right">1</td><td>census_areas</td></tr>
<tr><td align="right">2</td><td>ppl</td></tr>
<tr><td align="right">3</td><td>com</td></tr>
<tr><td align="right">4</td><td>prov</td></tr>
<tr><td align="right">5</td><td>reg</td></tr>
</table>
<br><br>
<b><<i>topo-prefix</i>>_topofeatures</b> e.g. "<i>census2011_topofeatures</i>"
<table cellspan="4" cellpadding="6" bgcolor="#f8f8f0" border="1">
<tr><td bgcolor="#f0f0f8"><b>uid</b></td>
<td bgcolor="#f0f0f8"><b>node_id</b></td>
<td bgcolor="#f0f0f8"><b>edge_id</b></td>
<td bgcolor="#f0f0f8"><b>face_id</b></td>
<td bgcolor="#f0f0f8"><b>topolayer_id</b></td>
<td bgcolor="#f0f0f8"><b>fid</b></td></tr>
<tr><td align="right">1</td><td>NULL</td><td>NULL</td><td align="right">1</td><td align="right">1</td><td align="right">1</td></tr>
<tr><td align="right">2</td><td>NULL</td><td>NULL</td><td align="right">2</td><td align="right">1</td><td align="right">2</td></tr>
<tr><td align="right">3</td><td>NULL</td><td>NULL</td><td align="right">3</td><td align="right">1</td><td align="right">3</td></tr>
<tr><td colspan="6" align="center"><hr></td></tr>
<tr><td align="right">28870</td><td>NULL</td><td>NULL</td><td align="right">28864</td><td align="right">1</td><td align="right">28867</td></tr>
<tr><td align="right">28871</td><td>NULL</td><td>NULL</td><td align="right">28868</td><td align="right">1</td><td align="right">28868</td></tr>
</table>
<br><br>
<b><<i>topo-prefix</i>>_topofeatures_<<i>topolayer-id</i>></b> e.g. "<i>census2011_topofeatures_1</i>"
<table cellspan="4" cellpadding="6" bgcolor="#f8f8f0" border="1">
<tr><td bgcolor="#f0f0f8"><b>fid</b></td>
<td bgcolor="#f0f0f8"><b>section_id</b></td>
<td bgcolor="#f0f0f8"><b>cod_com</b></td>
<td bgcolor="#f0f0f8"><b>cod_ppl</b></td>
<td bgcolor="#f0f0f8"><b>pop_2011</b></td>
<td bgcolor="#f0f0f8"><b>m_2011</b></td>
<td bgcolor="#f0f0f8"><b>f_2011</b></td></tr>
<tr><td align="right">1</td><td align="right">450010000001</td><td align="right">45001</td><td align="right">205142706</td><td align="right">10</td><td align="right">7</td><td align="right">3</td></tr>
<tr><td align="right">2</td><td align="right">450010000002</td><td align="right">45001</td><td align="right">205142706</td><td align="right">155</td><td align="right">69</td><td align="right">86</td></tr>
<tr class="d0"><td align="right">3</td><td align="right">450010000003</td><td align="right">45001</td><td align="right">205142706</td><td align="right">208</td><td align="right">97</td><td align="right">111</td></tr>
<tr><td colspan="7" align="center"><hr></td></tr>
<tr><td align="right">28867</td><td align="right">1000070000064</td><td align="right">100007</td><td>NULL</td><td align="right">0</td><td align="right">0</td> <td align="right">0</td></tr>
<tr><td align="right">28868</td><td align="right">1000070000066</td><td align="right">100007</td><td align="right">1410775412</td><td align="right">0</td><td align="right">0</td><td align="right">0</td></tr>
</table>
<br><br>
<table width="100%"><tr>
<td width="33%" align="left"><a href="https://www.gaia-gis.it/fossil/libspatialite/wiki?name=topo-intermediate">previous page</a></td>
<td align="center"><a href="https://www.gaia-gis.it/fossil/libspatialite/wiki?name=misc-docs">back to index</a></td>
<td width="33%" align="right"><a href="https://www.gaia-gis.it/fossil/libspatialite/wiki?name=topo-advanced">next page</a></td>
</tr></table>
Z 095db681ce63cacb47991516ae93f064