Aufbau der Serie
Diese Tutorialreihe ist in drei separate Beiträge unterteilt und gibt Ihnen einige Einblicke in den Umgang mit File-Geodatabases in Ihren C#-Lösungen.
Hinweis: Als Testdaten für die folgenden Beispiele verwenden wir den Urban Atlas 2012 für Österreich, den Sie auch im Beispielcode dieses Blogbeitrags finden. Alle Screenshots und Ausgaben in diesem Beitrag stammen aus dieser File-Geodatabase; bei anderen Datensätzen sollten die Ergebnisse aber sehr ähnlich aussehen.
Datenanalyse und Indizierung
In den folgenden Kapiteln untersuchen wir den Inhalt des Layers und analysieren Attribute und geometrische Eigenschaften genauer. Darüber hinaus leiten wir aggregierte Kennzahlen als Indizes für die spätere Verwendung ab.
Attributindizierung
Schauen wir uns die Attribute genauer an. Wir wissen, dass die Klassifizierung des Urban Atlas 2012 für jedes Polygon im Feld CODE2012 steht. Unsere nächste Aufgabe besteht also darin, herauszufinden, welche Werte in diesem Feld vorkommen. Wir schreiben eine kleine Schleife, die die Features im Datensatz durchläuft, die Werte aus dem Feld CODE2012 extrahiert und ihr Vorkommen zählt:
private static void AttributeIndex(string dataSetPath)
{
var values = new Dictionary<string, int>()
var fileGdbDriver = Ogr.GetDriverByName("OpenFileGDB")
var dataSource = fileGdbDriver.Open(dataSetPath, 0)
var layer = dataSource.GetLayerByIndex(0)
var feature = layer.GetNextFeature()
while (feature != null)
{
var classification = feature.GetFieldAsString("CODE2012")
if (!values.ContainsKey(classification))
{
values.Add(classification, 1)
}
else
{
values[classification]++
}
feature = layer.GetNextFeature()
}
var result = values.OrderBy(x => x.Key)
foreach (var keyValuePair in result)
{
Console.WriteLine($"Value: {keyValuePair.Key} occurs {keyValuePair.Value} times.")
}
}
Ergebnis:
Classification 11100 occurs 6416 times.
Classification 11210 occurs 24024 times.
Classification 11220 occurs 29536 times.
Classification 11230 occurs 23425 times.
Classification 11240 occurs 10800 times.
Classification 11300 occurs 27735 times.
Classification 12100 occurs 21215 times.
Classification 12210 occurs 1569 times.
Classification 12220 occurs 44160 times.
Classification 12230 occurs 3563 times.
Classification 12300 occurs 176 times.
Classification 12400 occurs 32 times.
Classification 13100 occurs 1355 times.
Classification 13300 occurs 659 times.
Classification 13400 occurs 3568 times.
Classification 14100 occurs 4297 times.
Classification 14200 occurs 3099 times.
Classification 21000 occurs 33946 times.
Classification 22000 occurs 2695 times.
Classification 23000 occurs 39897 times.
Classification 31000 occurs 19154 times.
Classification 32000 occurs 547 times.
Classification 33000 occurs 126 times.
Classification 40000 occurs 53 times.
Classification 50000 occurs 1994 times.
Classification 91000 occurs 81 times.
Räumliche Indizierung
Nachdem wir nun die zahlenmäßige Verteilung der Klassen im Datensatz kennen, interessiert uns ihre räumliche Abdeckung bzw. Verteilung. Dafür ist nur eine recht einfache Anpassung nötig. Anstatt das Vorkommen eines Werts zu zählen, berechnen wir einfach die Fläche der Geometrie des jeweiligen Features und speichern diese. Schließlich dividieren wir sie durch die Gesamtfläche aller Features, um den Prozentsatz für jede Klasse zu erhalten.
private static void SpatialIndex(string dataSetPath)
{
var values = new Dictionary<string, double>()
var fileGdbDriver = Ogr.GetDriverByName("OpenFileGDB")
var dataSource = fileGdbDriver.Open(dataSetPath, 0)
var layer = dataSource.GetLayerByIndex(0)
var feature = layer.GetNextFeature()
while (feature != null)
{
var classification = feature.GetFieldAsString("CODE2012")
var geometry = feature.GetGeometryRef()
var area = geometry.Area()
if (!values.ContainsKey(classification))
{
values.Add(classification, area)
}
else
{
values[classification] += area
}
feature = layer.GetNextFeature()
}
var totalArea = values.Sum(x => x.Value)
var result = values
.OrderBy(x => x.Key)
.ToDictionary(o => o.Key, o => o.Value / totalArea)
foreach (var keyValuePair in result)
{
Console.WriteLine($"Value: {keyValuePair.Key} occupies {keyValuePair.Value,7:P} of the total area.")
}
}
Ergebnis:
Classification 11100 occupies 0,27 % of the total area.
Classification 11210 occupies 1,48 % of the total area.
Classification 11220 occupies 1,96 % of the total area.
Classification 11230 occupies 1,38 % of the total area.
Classification 11240 occupies 0,41 % of the total area.
Classification 11300 occupies 0,67 % of the total area.
Classification 12100 occupies 1,70 % of the total area.
Classification 12210 occupies 0,20 % of the total area.
Classification 12220 occupies 1,71 % of the total area.
Classification 12230 occupies 0,25 % of the total area.
Classification 12300 occupies 0,05 % of the total area.
Classification 12400 occupies 0,12 % of the total area.
Classification 13100 occupies 0,28 % of the total area.
Classification 13300 occupies 0,04 % of the total area.
Classification 13400 occupies 0,13 % of the total area.
Classification 14100 occupies 0,45 % of the total area.
Classification 14200 occupies 0,46 % of the total area.
Classification 21000 occupies 30,65 % of the total area.
Classification 22000 occupies 1,43 % of the total area.
Classification 23000 occupies 15,09 % of the total area.
Classification 31000 occupies 35,08 % of the total area.
Classification 32000 occupies 3,93 % of the total area.
Classification 33000 occupies 0,33 % of the total area.
Classification 40000 occupies 0,25 % of the total area.
Classification 50000 occupies 1,63 % of the total area.
Classification 91000 occupies 0,08 % of the total area.
Nächste Schritte
Durch den Blick auf die eigentlichen Daten im Layer haben wir mehr über die Features erfahren und Statistiken zur Häufigkeit jeder Klasse und zu ihrer räumlichen Verteilung abgeleitet. In Teil 3 unserer Serie sehen wir uns Zugriffsmethoden genauer an, mit denen wir gezielt einzelne Features aus den Daten extrahieren.