
After decades of writing my own geospatial math routines I finally modernized and starting using C# and can't tell you how pleased I am to discover your dotSpatial library. I just started experimenting with it and am incredibly impressed with how intuitive
it is to use even for a parttime programmer like me.
I do have one question I'm hoping someone can help with. I'm trying to calculate the shortest distance from a point to a polygon. According to the documentation, the polygon class has a .Distance method that looks like it should calculate the distance from
a polygon geometry to another geometry, like a point. I've defined a Polygon in the program below but don't seem to be able to access the polygon.Distance method (see the bottom of the code below).
Obviously, there is something I don't understand and will appreciate any help.
Thanks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotSpatial;
using DotSpatial.Analysis;
using DotSpatial.Compatibility;
using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Extensions;
using DotSpatial.Modeling;
using DotSpatial.Mono;
using DotSpatial.Positioning;
using DotSpatial.Projections;
using DotSpatial.Serialization;
using DotSpatial.Symbology;
using DotSpatial.Topology;
namespace DotSpatialTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCalcRB_Click(object sender, EventArgs e)
{
Latitude lat1 = new Latitude(36.0);
Longitude lon1 = new Longitude(121.0);
Distance elev1 = new Distance(0.0, DistanceUnit.Meters);
Position3D pos3D_1 = new Position3D(elev1, lat1, lon1);
Position pos2D_1 = new Position(lat1, lon1);
Latitude lat2 = new Latitude(36.0);
Longitude lon2 = new Longitude(131.0);
Distance elev2 = new Distance(0.0, DistanceUnit.Meters);
Position3D pos3D_2 = new Position3D(elev2, lat2, lon2);
Position pos2D_2 = new Position(lat2, lon2);
Distance range1, range2;
Azimuth az1, az2;
Ellipsoid ellip = Ellipsoid.Wgs1972;
az1 = pos2D_1.BearingTo(pos2D_2);
range1 = pos2D_1.DistanceTo(pos2D_2);
Console.WriteLine(az1.ToRadians().ToDegrees() + " " + range1.ToMeters());
az2 = pos2D_1.BearingTo(pos2D_2, ellip);
range2 = pos2D_1.DistanceTo(pos2D_2, ellip);
Console.WriteLine(az2.ToRadians().ToDegrees() + " " + range2.ToMeters());
double azDiff, rangeDiff;
azDiff = az1.DecimalDegrees  az2.DecimalDegrees;
rangeDiff = range1.Value  range2.Value;
Console.WriteLine(azDiff.ToString() + " " + rangeDiff.ToString());
Polygon[] polygon = new Polygon[1];
Coordinate[] coord = new Coordinate[5];
coord[0] = new Coordinate(34, 121);
coord[1] = new Coordinate(34 + .1, 121);
coord[2] = new Coordinate(34+.1, 121+.1);
coord[3] = new Coordinate(34, 121 +.1);
coord[4] = new Coordinate(34, 121);
polygon[0] = new Polygon(coord);
//this doesn't work
//polygon.Distance( ...
}
}
}


Developer
Apr 20, 2014 at 4:58 AM

var coord = new Coordinate[5];
coord[0] = new Coordinate(34, 121);
coord[1] = new Coordinate(34 + .1, 121);
coord[2] = new Coordinate(34 + .1, 121 + .1);
coord[3] = new Coordinate(34, 121 + .1);
coord[4] = new Coordinate(34, 121);
var polygon = new Polygon(coord);
var distance = polygon.Distance(new Point(new Coordinate(34, 1)));



Many thanks for the quick response. Now that you've shown me how to get that working, it seems obvious that coordinates are X, Y pairs and not Longitude and Latitude. I've been trying various ways to construct a Polygon with lat/lon vertices and use the
same approach to get the distance between a Position (defined with a lat/lon) and the lat/lon Polygon. Is that possible? Or do I have to go through the process of projecting lat/lon pairs into Y/X pairs?
Thanks again

