Creating GeoJSON Data Using ASP.NET Core and Angular
Hello to everyone 🌍
For a project I was working on, I needed to be able to access my spatial data on a map using asp.net core. I used the geojson data format for this. GeoJSON is a popular format for encoding a variety of geographic data types. Here is an example of geojson; for more information, see my article here.
Postgis is used to store my data, and angular and leaflet are used in the frontend. In this article, I will simply discuss how we can deal with geojson data on Asp.net core, which I realized there aren’t many resources on, but you can access all of my work’s codes here.
Although not yet finished, the final version
Defining a Spatial Entity
Let’s begin by defining a Building entity in our ASP.NET Core application. The Building class will include a MultiPolygon property representing its geometric boundaries. We use the NetTopologySuite, an advanced library for handling spatial data in .NET, to define the MultiPolygon type.
public class Building
{
public int Id { get; set; }
public int? fKey { get; set; }
public string? Blok { get; set; }
public string? Nitelik { get; set; }
public MultiPolygon? geom { get; set; }
}
Converting Spatial Data to GeoJSON
Next, we need to convert our spatial data to GeoJSON. We create a helper method named ConvertGeometryToGeoJSON
for this purpose. This method takes a MultiPolygon from the NetTopologySuite and converts it to a GeoJSON MultiPolygon.
private static GeoJSON.Net.Geometry.MultiPolygon ConvertGeometryToGeoJSON(NetTopologySuite.Geometries.MultiPolygon ntsMultiPolygon)
{
var geoJSONMultiPolygon = new GeoJSON.Net.Geometry.MultiPolygon(
ntsMultiPolygon.Geometries.Select(ntsPolygon =>
new Polygon(
new List<LineString>
{
new LineString(
ntsPolygon.Coordinates.Select(ntsCoordinate =>
new Position(ntsCoordinate.Y, ntsCoordinate.X)).ToList()
)
}
)).ToList()
);
return geoJSONMultiPolygon;
}
We then create a ConvertToGeoJSON
method to convert our Building entities to GeoJSON Feature objects. These GeoJSON Features are then added to a GeoJSON FeatureCollection.
private static GeoJSON.Net.Feature.FeatureCollection ConvertToGeoJSON(IEnumerable<Building> buildings)
{
var featureCollection = new GeoJSON.Net.Feature.FeatureCollection();
foreach (var building in buildings)
{
var properties = new Dictionary<string, object>
{
{ "Id", building.Id },
{ "fKey", building.fKey },
{ "Nitelik", building.Nitelik },
{ "Blok", building.Blok }
};
var geometry = ConvertGeometryToGeoJSON(building.geom);
var feature = new Feature(geometry, properties);
featureCollection.Features.Add(feature);
}
return featureCollection;
}
This function works by looping over each Building
in the list, creating a dictionary for its properties, and converting its geom
property to a GeoJSON MultiPolygon
. These are then used to create a new Feature
, which is added to the FeatureCollection
.
Configuring the Services
To work with GeoJSON data, we need to configure our services. We add the RepositoryContext
to the IServiceCollection
using the AddDbContext
method. We also configure it to use Npgsql and the NetTopologySuite, which provides support for spatial data types in EF Core.
public static class ServicesExtensions
{
public static void ConfigureSqlContext(this IServiceCollection services, IConfiguration configuration
) => services.AddDbContext<
RepositoryContext>(options =>
options.UseNpgsql(configuration.GetConnectionString("sqlConnection"), prj => {
prj.UseNetTopologySuite();
}));
}
To properly handle GeoJSON data, we need to configure our controllers to use NewtonsoftJson. We also need to set up a GeometryConverter which will handle the conversion of our spatial data to GeoJSON.
builder.Services.AddControllers()
.AddApplicationPart(typeof(Presentation.AssemblyReference).Assembly)
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
options.SerializerSettings.Converters.Add(new GeometryConverter(geometryFactory));
});
In the above code, we add NewtonsoftJson to our controllers. We then create a GeometryFactory with SRID 4326 (which stands for the WGS 84 spatial reference system) and add a GeometryConverter to the Converters of our SerializerSettings. This converter will handle the conversion of our spatial data to GeoJSON.
I welcome your ideas and criticism.
I appreciate you reading. Good luck! 🌞