Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is there a .NET library to calculate area enclosed by polygon made up of geo coordinates (lat longs)?

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

SharpMap has a polygon class with an Area method. But before calling that, you should consider projecting the polygon's coordinates into an equal area projection to yield meaningful results. It looks like SharpMap uses ProjNet, which has Albers, but I also see there is now a c# wrapper for Proj4.

share|improve this answer
add comment

Another option is using the Microsoft.SqlServer.Types library. While it isn't supplied in .NET, it is available as a nuget package, or you can find it in your SqlServer (or Express) installation (%programfiles%\Microsoft SQL Server\100\SDK\Assemblies) as Microsoft.SqlServer.Types.dll

Obviously, it relies on Microsoft (and the SQL types library which could have it's own licencing issues), so the other options supplied may well be better options, but if you are already integrating with SqlServer 2008, then this may be a nice way to go.

To do an area calculation is pretty simple (I have included an NUnit test below demonstrating it.

using System;
using System.Text;
using Microsoft.SqlServer.Types;
using System.Data.SqlTypes;
using NUnit.Framework;

namespace YourNamespace
{
    class GeometryTest
    {

        [Test]
        public void CheckAreaTest()
        {
            string polygon = "POLYGON((145 -42 , 147 -42, 147 -40, 145 -40, 145 -42))"; //wkt string over Tasmania, Australia
            SqlGeography geom = new SqlGeography();
            SqlChars geometryString = new SqlChars(new SqlString(polygon));
            geom = SqlGeography.STPolyFromText(geometryString, 4326);
            Assert.AreEqual(37371149986.1, Math.Round(geom.STArea().Value, 1));

        }

    }
}
share|improve this answer
    
While your example helps achieve find the area but it doesn't address the part where I said that I have lat, longs because area needs to be calculated in meters. –  Hasan Khan Oct 3 '11 at 2:55
    
By using SqlGeography, the STArea is automatically in square metres (which is why it is so big). Will ammend to give better values in the string polygon string to avoid confusion. –  Kelso Oct 3 '11 at 3:17
add comment

NetTopologySuite offer what you need, and much more: http://code.google.com/p/nettopologysuite/

share|improve this answer
    
+1 I'd forgotten about nettopologysuite, looks like it has a lot more momentum than SharpMap. –  Kirk Kuykendall Oct 2 '11 at 21:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.