Skip to content

Instantly share code, notes, and snippets.

@seydakucuk
Created March 18, 2022 12:33
Show Gist options
  • Save seydakucuk/caba0f79355d4f899374b129a8738e71 to your computer and use it in GitHub Desktop.
Save seydakucuk/caba0f79355d4f899374b129a8738e71 to your computer and use it in GitHub Desktop.
Buffers the given lat, lon coordinate with given radius using NetTopology Suite v1.15.3
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
namespace ConsoleApplication1
{
public static class GeometricOperations
{
/// <summary>
/// Createa a circle with the center:wgsLon,wgsLat and the given radius (in meters)
/// </summary>
/// <param name="wgsLat"></param>
/// <param name="wgsLon"></param>
/// <param name="radius">in meters</param>
public static IGeometry Buffer(double wgsLat, double wgsLon, double radius)
{
var coordinateSystemFactory = new CoordinateSystemFactory();
var coordinateTransformationFactory = new CoordinateTransformationFactory();
var WGS84CoordinateSystem = GeographicCoordinateSystem.WGS84;
var wkt3857 = "PROJCS[\"WGS 84 / World Mercator\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Mercator_1SP\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"3395\"]]";
var webMercatorCS = coordinateSystemFactory.CreateFromWkt(wkt3857);
var transformWGS84toWebMercator = coordinateTransformationFactory.CreateFromCoordinateSystems(WGS84CoordinateSystem, webMercatorCS);
var factoryWebMercator = new GeometryFactory(new PrecisionModel(PrecisionModel.MaximumPreciseValue), 3857);
var webMercatorIncidentCoordinate = transformWGS84toWebMercator.MathTransform.Transform(new double[] { wgsLon, wgsLat });
var webMercatorIncidentPoint = factoryWebMercator.CreatePoint(new Coordinate(webMercatorIncidentCoordinate[0], webMercatorIncidentCoordinate[1]));
int quadrantSegments = 12; // for maximum precision
return NetTopologySuite.Operation.Buffer.BufferOp.Buffer(webMercatorIncidentPoint, radius, quadrantSegments);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment