Quantcast
Channel: DotSpatial
Viewing all articles
Browse latest Browse all 3973

New Post: Calculate distance from point to polygon

$
0
0
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 part-time 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( ...
        }
    }
}

Viewing all articles
Browse latest Browse all 3973

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>