ThinkGeo.com    |     Documentation    |     Premium Support

GetClosestPointTo method not working while applying UTM Projection

Hi,
I am getting the closest point on a line WITH PROJECTION. while same thing applying in UTMPROJECTION. it’s not coming on line.

PointShape pt2 = ln.GetClosestPointTo(pt1, wfMap.MapUnit);
(I applied mercator projection to layer)

With Out Projection

With Projection

Please any one suggest me to solve the issue.

Thanks,
Riyaz

Hi Riyaz,

Does the “With Out Projection” here means epsg 4326?

And do you make sure you you put the result point into the projection you are using?

Please paste your mainly code here and let me know your detail dll version, I can help you looks into it.

Regards,

Don

Hi Don,

       With Out Projection means epsg 4326.

I adjusted the code(missed result point into projection). Now, GetClosestPointTo mehtod working fine.
But one more thing, I want to check result point intersects line or not. it’s coming false.

Note : I am using 9.0.0.0 version.

Please check bellow code.

ManagedProj4Projection proj = new ManagedProj4Projection();
proj.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
proj.ExternalProjectionParametersString = Proj4Projection.GetEsriParametersString(54004);

InMemoryFeatureLayer imftl = new InMemoryFeatureLayer();
//imftl.FeatureSource.Projection = proj;
imftl.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = new PointStyle(PointSymbolType.Cross, new GeoSolidBrush(GeoColors.Red), 10);
imftl.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = new LineStyle(new GeoPen(GeoColors.Blue, 1));
imftl.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            LayerOverlay lo = new LayerOverlay();
            lo.Layers.Add("Layer", imftl);
            wfMap.Overlays.Add("RoutesOverlay", lo);

            LineShape ln = new LineShape("LINESTRING(10 10,20 20)");
            Feature ftLn = new Feature(ln);
            imftl.InternalFeatures.Add(proj.ConvertToExternalProjection(ftLn));

            PointShape pt1 = new PointShape("POINT(16 16)");
            Feature ftPt1 = new Feature(pt1);

            PointShape pt2 = proj.ConvertToExternalProjection(ln).GetClosestPointTo(proj.ConvertToExternalProjection(pt1), wfMap.MapUnit);
            Feature ftPt2 = new Feature(pt2);
            imftl.InternalFeatures.Add(ftPt2);

            //bool isIntersects = proj.ConvertToInternalProjection(pt2).Intersects(ln);
            //bool isIntersects = pt2.Intersects(proj.ConvertToExternalProjection(ln));
            bool isIntersects = ftPt2.Intersects(proj.ConvertToExternalProjection(ftLn));
            if (isIntersects)
                MessageBox.Show("Intersects");
            else
                MessageBox.Show("Not Intersects");

suggest me to solve this issue?

Thanks,
Riyaz

Hi Riyaz,

Thanks for your code, we reproduced that.

Our developer will work for it, any update I will let you know.

Regards,

Don

Hi Riyaz,

As below is the update from our developer.

This issue is caused from NTS. It is not a problem of projection or GetClosestPointTo method.

The problem is the implementation of getting intersection relationship between a point and line.

We directly tested the scenario in the latest NTS 1.14 version and still recreated the issue.

After that our developer calculate the expected value depending on the line angle, it is perfect matched. We attached the detail code in item 2.

As below is something I think we can do for this scenario.

  1. When we need to calculate the relative position, convert the shapes to decimal degree and do the intersection. That might be get correct result.

  2. You can override the Intersects method for class PointShape and use the custom code handle the relationship. Please refer the code as below:

    string wktPoint = “POINT(1777767.0780895958 1796198.0377165582)”;
    string wktLine = “LINESTRING(1113194.9079327358 1111475.1028522244,2226389.8158654715 2258423.6490963805)”;

    WKTReader reader = new WKTReader();
    var ntsPoint = (Point)reader.Read(wktPoint);
    var ntsLine = (LineString)reader.Read(wktLine);

    bool r1 = ntsPoint.Intersects(ntsLine);
    bool r2 = ntsLine.Intersects(ntsPoint);
    // Here r1 and r2 are both false

    double angle = Math.Atan2(ntsLine[1].Y - ntsLine[0].Y, ntsLine[1].X - ntsLine[0].X);
    double expectedY = Math.Tan(angle) * (ntsPoint.X - ntsLine[0].X) + ntsLine[0].Y;
    double actualY = ntsPoint.Y;
    bool isMatched = expectedY == actualY;
    // Here isMatched is true

In fact we also report this as issue to NTS, if they fixed that we will implement that to our map.

Any question please let us know.

Regards,

Don