c# - Intersection between two geographic lines -
i'm using dotspatial c# library , i'm trying use following code try find intersection point between 2 lines (i know 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 c1 = new coordinate(0, -1); var c2 = new coordinate(0, 1); 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 c3 = new coordinate(-1, 0); var c4 = new coordinate(1, 0); 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 empty.
what doing wrong?
i tried swap x , y components each coordinate (in case first assumed longitude instead of latitude)
thanks!
edit: per weston's comment below, i've changed coordinates of 2 lines more intersecting ones. result same.
the intersection code using limited shortcut. stuck 2 conflicting ideas. first idea featuresets have same feature type. is, if working polygon featureset, features polygons. second idea "intersection" of 2 lines line. point. featureset intersecting code designed used intersecting polygons resulting shape polygon. work points results points. in case, want find union of intersecting shapes, , throw out shapes not intersect. can accomplish taking control of features in loop ones below. have 3 examples, 1 creates point featureset intersections, , assumes intersections points, 1 keeps original d1 feature if intersects d2 feature, , unions intersecting d2 features each d1 feature. has potential of creating duplication of d2 content if d2 feature intersects more 1 d1 feature. in of these cases, may not clear attributes, since intersection officially possess attributes belong multiple d2 shapes, not one, have handled in custom way meaningful specific application.
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 c1 = new coordinate(0, -1); var c2 = new coordinate(0, 1); 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 c3 = new coordinate(-1, 0); var c4 = new coordinate(1, 0); var line2 = new linestring(new[] { c3, c4 }); d2.addfeature(line2); // create point featureset intersections var result = new featureset(featuretype.point) { projection = geoproj }; foreach (ifeature feature in d1.features) { foreach (ifeature other in d2.features) { if (feature.intersects(other)) { result.addfeature(feature.intersection(other)); } } } // keep d1 lines intersect d2 result = new featureset { projection = geoproj }; foreach(ifeature feature in d1.features){ foreach(ifeature other in d2.features){ if(feature.intersects(other)){ result.addfeature(feature); } } } // alternately combine intersecting lines cross result = new featureset { projection = geoproj }; foreach (ifeature feature in d1.features) { ifeature union = feature; boolean keep = false; foreach (ifeature other in d2.features) { if (feature.intersects(other)) { union = union.union(other); keep = true; } } if (keep) { result.addfeature(union); } }
Comments
Post a Comment