Brief Intro To Netopology in .NET Core

Aizeem Paroya
Trimble Maps Engineering Blog
5 min readFeb 18, 2020

Somehow someway you’ve ended up at this rudimentary blog but it most likely went in the following ways. You started googling along the lines of “SqlGeography .NET Core”, “SqlGeogphraphy functions and types .NET Core ”, “Why won’t SQL geography work in my .NET Core app?” and from there started going down some disappointing rabbit holes to find that SQL geography isn’t supported yet in .NET Core. Now you’re stressed and contemplating how you’re going to port dinosaur code that has a million SQL geography types and functions splattered everywhere over to this wonderful world of .net core (and probably Linux)… times may start to seem bleak….

BUT rest assured! Where there’s a framework problem there’s a Stack Overflow thread with the answer somewhere. This brings us NetopologySuite.

In this blog, I’m mostly going to be talking about some basics that I have found useful. I will be using the 2.0.0 release of the Netopology Suite for the code snippets and will also try to post links to some useful sites that have helped me understand the massive iceberg that is NTS (an abbreviation I’ll use throughout the article.)

Geometries are the bread and butter of dealing with data in the GIS world (useful link on shapes and jargon.) So let’s take a second to talk about creating geometries and playing around with them. The different types of geometries talked about later inherit the base class of geometry. One of the most commonly used geometries is a linestring:

Another commonly used geometry type is polygons as they can be used to represent top-down representations of buildings for example. They are just as easy to create:

One can make a range of different types of polygons and don’t have to be simple enclosed shapes. For example, a donut can be created by passing not just a shell but also an array of coordinates that represent the smaller inner circle as the second parameter when creating the polygon.

Next, let’s take a second to talk about Multi-polygons and Multi-linestrings.

And MultiPolygons:

You might ask “Aizeem, why do I wanna create arrays of geometries”? Well for a few reasons it allows you bundle similar types of geometries that you know will be of a certain type but it also allows you to run functional methods on the class without looping through everything!

However, this is not exclusive meaning its a list of coordinates in both geometries. Therefore, the loop might be necessary for tasks that require special logic and is good to know about. This can be between any two geometries which help on answering questions like “Does my line string(a route) touch this polygon (a house)?”.

Now that brings me to how can you read in SQL geography from the DB easily. Entity Framework provides a pretty simple way to specify the use of NTS to read and write. Take this example:

Now let’s talk about some common functions and uses as well as their limitations. Comparing two coordinates. Sometimes coordinates don’t match up perfectly down to every decimal but are “close enough” — well you can use:

Maybe I want to do distance calculations between two coordinates or two geometries:

“Aizeem I want to create some simple shapes and don’t feel like putting in every coordinate like a chimp”. Simple, use geometric shape factory to create geometries.

The geometric shape factory allows the creation of other geometries as well including rectangles, arcs and the best of them all a squircle.

The above examples are quick ways of creating simple geometries but say you are working with different projected coordinate systems. That requires you to do some math to go from one system to another system. Therefore, might want to create geometries with the right coordinate values (obviously.) This is where one might want to create a common class that handles the transform function which if using dependency injection (useful article) can be injected into components that are interacting with geometries. First, let's create the coordinate system representations.

You can also create coordinate systems with code (link) or well-known text representations (link). Now we can simply use the code given to us on the documentation site to create a filter function from the math transform provided above:

Used:

You might find yourself often comparing geometries with each other and I wanted to take a second and look at some of those comparisons and how they behave. There are a lot of handheld function

GeoJson

If you’re still reading this you probably are interested in knowing how NTS interacts with incoming or outgoing data that is GeoJson. You can easily convert from any geometry to geoJson with:

If we write a quick test like the following:

We’ll see that indeed what we put in is what we get out (life motto as well.)

This is useful as you can pass data in a standard format back to your frontend to be consumed and not have to do a lot of conversions (everything is awesome). Personally, I do like visualizing my geometries if I do not know what a function is doing or if the geometry I am trying to create is close to what I am picture. If you’re gifted at reading a sequence of coordinates and knowing the exact shape of geometry in your MatLab of a brain, well good for you. However, If you can’t auto plot in your head then you can take the output from the above function a paste it into a visualizer like geojson.io

That brings me to the conclusion of the article. There is a lot under the umbrella of netopology suite and it can be daunting to get started. The goal of this “short” intro was to provide a starting point for using netopology suite in your application. Hopefully, this blog helped do exactly that so thanks for reading!

Note: If you find a better way to do something or I got something entirely wrong feel free to comment as it helps everyone! I will update the post accordingly.

--

--