GIS & Geospatial

Spatial Queries in .NET with PostGIS and NetTopologySuite

Abid Inamdar January 04, 2026 7 min read 90 views

Why PostGIS?

PostGIS extends PostgreSQL with 500+ spatial functions. Combined with NetTopologySuite, you get strongly-typed geometry in .NET.

World map with spatial data overlay
World map with spatial data overlay

Setup

builder.Services.AddDbContext<GisDbContext>(o =>
    o.UseNpgsql(conn, x => x.UseNetTopologySuite()));

Entity with Geometry

public class Site
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public Point Location { get; set; } = null!; // SRID 4326
}

Distance Query

var userPoint = new Point(72.8777, 19.0760) { SRID = 4326 };
var nearby = await db.Sites
    .Where(s => s.Location.Distance(userPoint) < 5000)
    .OrderBy(s => s.Location.Distance(userPoint))
    .ToListAsync();
💡

Always set SRID on your Point objects. Mixing SRID 4326 (WGS84) and projected coordinates will give wrong distance results.

Conclusion

NetTopologySuite + PostGIS is the most productive GIS stack for .NET developers. No raw SQL, full LINQ support.

Share: Twitter/X LinkedIn

Related Posts

Comments (0)

Leave a Comment
Comments are moderated.