Aspose.GIS for .NET 24.6 (DLLs only) delivers an improved experience for geospatial application development in C# and VB.NET. This release introduces a major enhancement in integrating the library with persistent geospatial information storage systems.
This C# geospatial library release supports dynamic interactions with various geospatial databases. Developers can utilize the new DatabaseDataSource
and related classes to manage 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));
}
}
Build document conversion apps with shapefile to KML conversion capabilities in .NET. With the latest C# GIS library update, we have improved this functionality for you to enhance the portfolio of your C# and VB.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: