This update to Aspose.GIS for .NET API (MSI installer) brings significant enhancements for developers working with geospatial data. The library’s integration has been improved with persistent geospatial information storage systems, enabling more efficient data access and manipulation.
The C# geospatial library now offers more robust interaction with various geospatial databases. You can leverage the new DatabaseDataSource
and related classes to work with spatial data stored in databases smoothly, as highlighted in the detailed C# code example given below:
[TestFixture]
public class PostGisTests : TestFixtureBase
{
private DbConnection _conn;
[OneTimeSetUp]
public void SetUp()
{
_conn = new NpgsqlConnection(TestConfiguration.PostGisConnectionString);
var testDataFile = Path.Combine(TestConfiguration.TestDataPath, "postgis", "belarus.sql");
using (var cmd = _conn.CreateCommand())
{
try
{
cmd.CommandText = File.ReadAllText(testDataFile);
_conn.Open();
cmd.ExecuteNonQuery();
}
catch
{
Console.WriteLine("Error during loading file " + testDataFile);
throw;
}
}
}
[OneTimeTearDown]
public void TearDown()
{
_conn?.Dispose();
}
[Test]
public async Task NeighboringRegionsBasicUsage()
{
var builder = new DatabaseDataSourceBuilder();
builder
.FromQuery(@"
SELECT
a.osm_id, a.name, a.admin_level, a.boundary, a.population, a.way_area, ST_AsEWKB(a.way) AS way
FROM
tmp_planet_osm_belarus AS a
JOIN
tmp_planet_osm_belarus AS b ON b.name = 'Гомельская вобласць'
WHERE
ST_Touches(a.way, b.way);
")
.GeometryField("way")
.AddAttribute("osm_id", AttributeDataType.Long)
.AddAttribute("name", AttributeDataType.String);
var layer = await builder.Build().ReadAsync(_conn);
CollectionAssert.AreEquivalent(new[] { "Мінская вобласць", "Брэсцкая вобласць", "Магілёўская вобласць" }, layer.Select(x => (string)x.GetValue("name")));
}
[Test]
public async Task NeighboringRegionsWithSeparateSridField()
{
var builder = new DatabaseDataSourceBuilder();
builder
.FromQuery(@"
SELECT
a.osm_id, a.name, a.admin_level, a.boundary, a.population, a.way_area, ST_AsBinary(a.way) AS way, ST_SRID(a.way) AS srid
FROM
tmp_planet_osm_belarus AS a
JOIN
tmp_planet_osm_belarus AS b ON b.name = 'Гомельская вобласць'
WHERE
ST_Touches(a.way, b.way);
")
.GeometryField("way")
.AddAttribute("osm_id", AttributeDataType.Long)
.AddAttribute("name", AttributeDataType.String)
.SridField("srid");
var layer = await builder.Build().ReadAsync(_conn);
// WebMercator because it is a predefined value in the test data, ST_AsBinary wouldn't return srid information.
Assert.That(layer.Select(x => x.Geometry.SpatialReferenceSystem), Has.All.EqualTo(SpatialReferenceSystem.WebMercator));
}
[Test]
public async Task NeighboringRegionsWithSeparateSridFieldAndEWKB()
{
var builder = new DatabaseDataSourceBuilder();
builder
.FromQuery(@"
SELECT
a.osm_id, a.name, a.admin_level, a.boundary, a.population, a.way_area, ST_AsEWKB(a.way) AS way, 4326 AS srid
FROM
tmp_planet_osm_belarus AS a
JOIN
tmp_planet_osm_belarus AS b ON b.name = 'Гомельская вобласць'
WHERE
ST_Touches(a.way, b.way);
")
.GeometryField("way")
.AddAttribute("osm_id", AttributeDataType.Long)
.AddAttribute("name", AttributeDataType.String)
.SridField("srid");
var layer = await builder.Build().ReadAsync(_conn);
// Wgs84 (4326) is wrong srs, the right one is this WebMercator but we're deliberately overriding to the wrong one to check that despite the use of ST_AsEWKB
// overridden value is preferred.
Assert.That(layer.Select(x => x.Geometry.SpatialReferenceSystem), Has.All.EqualTo(SpatialReferenceSystem.Wgs84));
}
[Test]
public async Task NeighboringRegionsWithAdditionalSrsQuery()
{
var builder = new DatabaseDataSourceBuilder();
var referenceSrs = SpatialReferenceSystem.CreateFromEpsg(4284);
builder
.FromQuery(@"
SELECT
a.osm_id, a.name, a.admin_level, a.boundary, a.population, a.way_area, ST_AsEWKB(ST_Transform(a.way, 4284)) AS way
FROM
tmp_planet_osm_belarus AS a
JOIN
tmp_planet_osm_belarus AS b ON b.name = 'Гомельская вобласць'
WHERE
ST_Touches(a.way, b.way);
")
.GeometryField("way")
.AddAttribute("osm_id", AttributeDataType.Long)
.AddAttribute("name", AttributeDataType.String)
.UseExternalSrsFromQuery(
@"SELECT auth_srid, srtext FROM spatial_ref_sys
WHERE srid = 4284"
)
.ExternalSrsFields("auth_srid", "srtext");
var layer = await builder.Build().ReadAsync(_conn);
CollectionAssert.AreEquivalent(new[] { "Мінская вобласць", "Брэсцкая вобласць", "Магілёўская вобласць" }, layer.Select(x => (string)x.GetValue("name")));
// In case using UseExternalSrsFromQuery we extract information about SRS from db
// avoiding the use of an embedded SRS information
Assert.That(layer.Select(x => referenceSrs.Name == x.Geometry.SpatialReferenceSystem.Name).ToArray(), Is.All.EqualTo(true));
}
}
Developers can expand their document conversion apps’ capabilities with the shapefile to KML conversion option. The latest C# GIS library update improves upon this functionality and allows you to enhance the portfolio of your .NET solutions. The following code example illustrates this conversion:
// out of range latitude test
string sourcePath = @"issues\UkraineControlMapAO02JUN2024.shp";
string destinationPath = "UkraineControlMapAO02JUN2024.kml";
VectorLayer.Convert(sourcePath, Drivers.Shapefile, destinationPath, Drivers.Kml);
using (var layer = VectorLayer.Open(destinationPath, Drivers.Kml))
{
Assert.AreEqual(11, layer.Count);
Assert.AreEqual(8, layer.Attributes.Count);
}
Some of the newly added public API members in this Aspose.GIS for .NET 24.6 release are: