Skip to content

Instantly share code, notes, and snippets.

@oliverheilig
Last active September 12, 2018 10:16
Show Gist options
  • Save oliverheilig/7777382 to your computer and use it in GitHub Desktop.
Save oliverheilig/7777382 to your computer and use it in GitHub Desktop.
C# geo snippets

This is my list of code snippets useful when writing (geo)graphical applications in C#. Of course there are many powerful .NET libraries which handle these problems. But sometimes it is easier to just include some lines of code.

I've written the code to run directly in the browser using JSIL. You can extract the essential lines in your project. It should work for .NET, Mono, Silverlight, WinRT and Windows Phone. I've included the base types needed (Point, Rect) into the snippet. You can replace them with the appropriate type of the framework you are using.

For example: If you use the line simplification for WPF lines, you can take the System.Windows.Point. If you use it for WCF Spatial Library, you can take the System.Spatial.GeometryPoint.

Check whether a point is in range of a line string

You can use this snippet for geo-fencing or for UI-hit-testing. For a given polyline you can check if the distance of a point to the line string is not greater than a certain value.

Source Code / Try it in JSIL

Check whether a point is contained in a polygon

You can use this snippet for geo-fencing or UI-hit-testing. Works for all polygons, even for OGC-invalid ones, corresponding to the "fill mode" alternate.

Source Code / Try it in JSIL

Douglas Peucker Line Simplification

Simplifies a line string for a given precision. Based on the well known Douglas-Peucker algorithm

Source Code / Try it in JSIL

Cohen Sutherland Line Clipping

Clips a line string against a given rectangle. Based on the well known Cohen-Sutherland algorithm

Source Code / Try it in JSIL

Calculate the distance between points on a Mercator Map

Calcluate the airline distance based on mercator distance. This approximation formula is sufficiently accurate for our needs for distances of up to 600 km and 80° latitude (the error is never more than 5% even for extreme values).

Source Code / Try it in JSIL

Convert between "Coordinate Formats"

This snippet contains methods to transform between geographic (WGS84) coordinates and projected (Mercator) coordinates. Also contains methods to transform between the various representations of these formats used by PTV components.

Source Code / Try it in JSIL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment