Join Vector Layers using GIS C# Library

Join Vector Layers

Aspose.GIS API lets you to join the vector layers as shown in the code snippet below.

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
//one-to-one left join two layers by 'city' attribute
var options = new JoinOptions
{
JoinAttributeName = "city",
TargetAttributeName = "city"
};
using (var main = Drivers.Kml.OpenLayer(Path.Combine(workFolder, "main.kml")))
using (var second = Drivers.Kml.OpenLayer(Path.Combine(workFolder, "second.kml")))
using (var joined = main.Join(second, options))
{
// read and print joined
var featuresCount = joined.Count;
var attributesCount = joined.Attributes.Count;
var spatialRefSys = joined.SpatialReferenceSystem;
var code = spatialRefSys == null ? "'no srs'" : spatialRefSys.EpsgCode.ToString();
var joinedTempValue = joined[0].GetValue("joined_temp");
Console.WriteLine($"featuresCount: {featuresCount}");
Console.WriteLine($"attributesCount: {attributesCount}");
Console.WriteLine($"spatialRefSys: {code}");
Console.WriteLine($"joinedTempValue: {joinedTempValue}");
}

Join Layer using Few Attributes

Aspose.GIS API lets you to join a few attributes as shown in the code snippet below.

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
//one-to-one left join two layers by 'city' attribute
var options = new JoinOptions
{
JoinAttributeName = "city",
TargetAttributeName = "city",
//define attributes to join
JoinAttributeNames = new List<string> { "temp", "date" }
};
using (var main = Drivers.Kml.OpenLayer(Path.Combine(workFolder, "main.kml")))
using (var second = Drivers.Kml.OpenLayer(Path.Combine(workFolder, "second.kml")))
using (var joined = main.Join(second, options))
{
// read and print joined
var featuresCount = joined.Count;
var attributesCount = joined.Attributes.Count;
var spatialRefSys = joined.SpatialReferenceSystem;
var code = spatialRefSys == null ? "'no srs'" : spatialRefSys.EpsgCode.ToString();
Console.WriteLine($"featuresCount: {featuresCount}");
Console.WriteLine($"attributesCount: {attributesCount}");
Console.WriteLine($"spatialRefSys: {code}");
}

Join Layer using own Comparer

Aspose.GIS API lets you to join a layer using own Comparer as shown in the code snippet below. This approach is good when you need to join data using insensitive strings.

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
//one-to-one left join two layers by 'city' attribute
var options = new JoinOptions
{
JoinAttributeName = "city",
TargetAttributeName = "city",
//use custom comparer
ConditionComparer = new InsensitiveComparer()
};
using (var main = Drivers.Kml.OpenLayer(Path.Combine(workFolder, "main.kml")))
using (var second = Drivers.Kml.OpenLayer(Path.Combine(workFolder, "second.kml")))
using (var joined = main.Join(second, options))
{
// read and print joined
var featuresCount = joined.Count;
var attributesCount = joined.Attributes.Count;
var spatialRefSys = joined.SpatialReferenceSystem;
var code = spatialRefSys == null ? "'no srs'" : spatialRefSys.EpsgCode.ToString();
var cityValue = joined[4].GetValue("city");
var joinedCityValue = joined[4].GetValue("joined_city");
Console.WriteLine($"featuresCount: {featuresCount}");
Console.WriteLine($"attributesCount: {attributesCount}");
Console.WriteLine($"spatialRefSys: {code}");
Console.WriteLine($"cityValue: {cityValue}");
Console.WriteLine($"joinedCityValue: {joinedCityValue}");
}

Comparer implementation.

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
private class InsensitiveComparer : IEqualityComparer<object>
{
bool IEqualityComparer<object>.Equals(object x, object y)
{
if (x == null || y == null)
{
return x == y;
}
var xString = x.ToString();
var yString = y.ToString();
return xString.Equals(yString, StringComparison.InvariantCultureIgnoreCase);
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}