Beginner question: How to find line intersections

Oct 4, 2013 at 4:35 PM
Edited Oct 4, 2013 at 4:54 PM
Hello, I'm using the following code to try to find the intersection point between two lines (I know they do intersect)
            var geoproj = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984;

            var d1 = new FeatureSet { Projection = geoproj };
            var c1 = new Coordinate(31.877484, 34.736723);
            var c2 = new Coordinate(31.879607, 34.732362);
            var line1 = new LineString(new[] { c1, c2 });
            d1.AddFeature(line1);

            var d2 = new FeatureSet { Projection = geoproj };
            var c3 = new Coordinate(31.882391, 34.73352);
            var c4 = new Coordinate(31.875502, 34.734851);
            var line2 = new LineString(new[] { c3, c4 });
            d2.AddFeature(line2);

            var inters = d1.Intersection(d2, FieldJoinType.All, null);
            var feats = inters.Features;
            foreach (var feat in feats)
            {
                Console.WriteLine("{0}", feat.ToString());
            }
The resulting feature set is always empty.

What am I doing wrong?

EDIT: I also tried to swap the X and Y components for each coordinate (in case the first was assumed to be the longitude instead of the latitude)

Thanks!
Oct 10, 2013 at 11:40 AM
I didn't try to debug your code, but why don't you just solve the problem directly?
You basically have two line equations which you can derive from your points coordinates:

y=mx+n
y=qx+t

the intersecting point is given by the following formulas:

x_intersect= (t-n)/(m-q)

y_intersect= m(t-n)/(m-q) + n

beware thet if the lines are parallel m-q=0 so intersection will not occur and the two formulas give error (divide by zero).

however, there is for sure a correct way to do it in dotspatial, at your choice

Oscar
Oct 10, 2013 at 11:45 AM
Geographic lines are actually arcs, they don't follow the Cartesian equations you mention. Also, the earth is not a perfect sphere or a spheroid, it's more complex (which is approximated by the WGS84 model), so even sphere geometries are not exact.

What I'm asking for is how to use DotSpatial to calculate the intersection point in a WGS84 model.