Select to view content in your preferred language

How to get all points or vertex of polyline using arcgis javascript api

7023
5
03-24-2014 06:44 AM
GaneshSolai_Sambandam
Regular Contributor
Hi Guys,
I wanted to find the nearest point in the polyline from an input location and calculate its distance. At the moment, the application queries based on centroid of a polyline. In my case, I wanted to find all the points in the polyline and then push those points in an array and find the nearest point in the polyline from an input location.

Here is my current code below: All I wanted to do is change the code when esri Geometry is polyline and try find all points in the feature. Can anybody help me on this please.

function GetDistance(featureGeometryType, feature, location)
{
    try
    {
        var x = GetXFromLocationParameter(location);
        var y = GetYFromLocationParameter(location);
        var startPoint = new esri.geometry.Point(x, y, _map.spatialReference);
        var endPoint;

        switch (featureGeometryType)
        {
            case "esriGeometryPoint":
                endPoint = new esri.geometry.Point(feature.geometry.x, feature.geometry.y, _map.spatialReference);
                break;

            case "esriGeometryPolygon":
                var polygonJson = { "rings": feature.geometry.rings, "spatialReference": _map.spatialReference };
                var myPolygon = new esri.geometry.Polygon(polygonJson);
                var polyExtent = myPolygon.getExtent();
                endPoint = polyExtent.getCenter();
                break;

            case "esriGeometryPolyline":
                var polylineJson = { "paths": feature.geometry.paths, "spatialReference": _map.spatialReference };
                var myLine = new esri.geometry.Polyline(polylineJson);
                var lineExtent = myLine.getExtent();
                endPoint = lineExtent.getCenter();

                break;
        }

        return GetPoint2PointDistance(startPoint, endPoint);
    }
    catch(ex)
    {
        HandleError(ex, "method GetDistance of search.js");
    }
}

// Returns the distance from passed in point to the address point.
function GetPoint2PointDistance(startPoint, endPoint)
{
    try
    {
        var distanceInMetres;
        var distanceInMiles;

        distanceInMetres = Math.pow((endPoint.x - startPoint.x), 2) + Math.pow((endPoint.y - startPoint.y), 2);
        distanceInMetres = Math.sqrt(distanceInMetres);

        // Convert to miles.
        distanceInMiles = distanceInMetres * 0.00062137119;
        distanceInMiles = FormatNearestNumber(distanceInMiles);
        return distanceInMiles;
    }
    catch(ex)
    {
        HandleError(ex, "method GetPoint2PointDistance of search.js");
    }
}
0 Kudos
5 Replies
GaneshSolai_Sambandam
Regular Contributor
HI Guys,

In continuation of the above thread, I have a sample which was done using Microsoft Bing maps, where the user could get all the points of the line using poly.getLocations() method. Is there similar method in arcgis javascript api. Can anyone let me know, please.

http://social.msdn.microsoft.com/Forums/en-US/5293337c-52f3-4635-87f9-de1beddc8e10/given-a-point-and...

Hi Guys,
I wanted to find the nearest point in the polyline from an input location and calculate its distance. At the moment, the application queries based on centroid of a polyline. In my case, I wanted to find all the points in the polyline and then push those points in an array and find the nearest point in the polyline from an input location.

Here is my current code below: All I wanted to do is change the code when esri Geometry is polyline and try find all points in the feature. Can anybody help me on this please.

function GetDistance(featureGeometryType, feature, location)
{
    try
    {
        var x = GetXFromLocationParameter(location);
        var y = GetYFromLocationParameter(location);
        var startPoint = new esri.geometry.Point(x, y, _map.spatialReference);
        var endPoint;

        switch (featureGeometryType)
        {
            case "esriGeometryPoint":
                endPoint = new esri.geometry.Point(feature.geometry.x, feature.geometry.y, _map.spatialReference);
                break;

            case "esriGeometryPolygon":
                var polygonJson = { "rings": feature.geometry.rings, "spatialReference": _map.spatialReference };
                var myPolygon = new esri.geometry.Polygon(polygonJson);
                var polyExtent = myPolygon.getExtent();
                endPoint = polyExtent.getCenter();
                break;

            case "esriGeometryPolyline":
                var polylineJson = { "paths": feature.geometry.paths, "spatialReference": _map.spatialReference };
                var myLine = new esri.geometry.Polyline(polylineJson);
                var lineExtent = myLine.getExtent();
                endPoint = lineExtent.getCenter();

                break;
        }

        return GetPoint2PointDistance(startPoint, endPoint);
    }
    catch(ex)
    {
        HandleError(ex, "method GetDistance of search.js");
    }
}

// Returns the distance from passed in point to the address point.
function GetPoint2PointDistance(startPoint, endPoint)
{
    try
    {
        var distanceInMetres;
        var distanceInMiles;

        distanceInMetres = Math.pow((endPoint.x - startPoint.x), 2) + Math.pow((endPoint.y - startPoint.y), 2);
        distanceInMetres = Math.sqrt(distanceInMetres);

        // Convert to miles.
        distanceInMiles = distanceInMetres * 0.00062137119;
        distanceInMiles = FormatNearestNumber(distanceInMiles);
        return distanceInMiles;
    }
    catch(ex)
    {
        HandleError(ex, "method GetPoint2PointDistance of search.js");
    }
}
0 Kudos
derekswingley1
Deactivated User
We don't have a method that returns all points from a polyline. You'll have to loop through polyline.paths and look at points from there.
0 Kudos
GaneshSolai_Sambandam
Regular Contributor
Hi Derek,
Thank you very much for looking at this thread and providing the clue to figure out. However, I figured out the answer to find the nearest point of a polyline by looping all the polyline linesegment  or paths. I will post my working code tomorrow, so that it will help someone to use the code. Once again, thanks for your help.

We don't have a method that returns all points from a polyline. You'll have to loop through polyline.paths and look at points from there.
0 Kudos
JianHuang
Deactivated User
Ganesh,

What you are looking for is snapping. SnappingManager provides exact the same as you described.
https://developers.arcgis.com/javascript/jsapi/snappingmanager-amd.html#getsnappingpoint
0 Kudos
GaneshSolai_Sambandam
Regular Contributor
Ganesh,

What you are looking for is snapping. SnappingManager provides exact the same as you described.
https://developers.arcgis.com/javascript/jsapi/snappingmanager-amd.html#getsnappingpoint


HI Guys
I found the answer finding all the points in a polyline and getting their distance and here is the code below.

If anyone need this, can you use them to suit their needs.

// ********************************************************************
// Gets the distance from user's location for my nearest feature types.
//
// Parameters:
// -----------
// featureGeometryType - the type of feature.
// feature - the feature itself.
// location - the location parameter for my nearest service.
// ********************************************************************
function GetDistance(featureGeometryType, feature, location)
{
    try
    {
        var x = GetXFromLocationParameter(location);
        var y = GetYFromLocationParameter(location);
        var startPoint = new esri.geometry.Point(x, y, _map.spatialReference);
    //    alert(startPoint.x + "," + startPoint.y);
        var endPoint;

        switch (featureGeometryType)
        {
            case "esriGeometryPoint":
                endPoint = new esri.geometry.Point(feature.geometry.x, feature.geometry.y, _map.spatialReference);
                break;

            case "esriGeometryPolygon":
                var polygonJson = { "rings": feature.geometry.rings, "spatialReference": _map.spatialReference };
                var myPolygon = new esri.geometry.Polygon(polygonJson);
                var polyExtent = myPolygon.getExtent();
                endPoint = polyExtent.getCenter();
                break;

            case "esriGeometryPolyline":
                var polylineJson = { "paths": feature.geometry.paths, "spatialReference": _map.spatialReference };
           //    alert(feature.attributes.ROUTE_NAME + "<br/>" + feature.geometry.paths.length.toString() +  "," + feature.geometry.paths.toString());
             //For each path...
             var pointArray =[];
             var nearestPoint = null;
             var nearestDistance = 1000;
        
            for ( var path = 0; path < feature.geometry.paths.length; path ++ ) {
            
            //For each point in the path...
            for ( var pt = 0; pt < feature.geometry.paths[path].length; pt++ ) {
            //Do something with each point in here...
            //X coordinate: geo.paths[path][pt][0]
            //Y coordinate: geo.paths[path][pt][1]
           // alert(feature.geometry.paths[path][pt][0] + "," + feature.geometry.paths[path][pt][1]);
           var linex = feature.geometry.paths[path][pt][0];
           var liney = feature.geometry.paths[path][pt][1];
           
            var linedistance = GetLinePoint2PointDistance(startPoint, linex, liney); 
            if (linedistance < nearestDistance)
            {
                nearestPoint = new esri.geometry.Point(linex, liney, _map.spatialReference);
                nearestDistance = linedistance;
            }
          //  alert(linedistance);
           //  pointArray.push(linedistance);
          // alert(nearestPoint.x + ", " + nearestPoint.y + ", " + nearestDistance);
        
           //  pointArray.sort(function(a,b) {return a-b});
          //  pointArray.push(feature.geometry.paths[path][pt][0],feature.geometry.paths[path][pt][1]);
            console.log(pointArray);
            
              }
           //alert(nearestPoint.x + ", " + nearestPoint.y + ", " + nearestDistance);
              }
            
             // alert( feature.attributes.ROUTE_NAME + "," + nearestPoint.x + ", " + nearestPoint.y + ", " + nearestDistance);
              
            // alert(feature.attributes.ROUTE_NAME + ", " + pointArray[0]);
              
           
                
                var myLine = new esri.geometry.Polyline(polylineJson);
                var lineExtent = myLine.getExtent();
               // endPoint = lineExtent.getCenter();
               endPoint = nearestPoint;

                break;
        }

        return GetPoint2PointDistance(startPoint, endPoint);
    }
    catch(ex)
    {
        C_HandleError(ex, "method GetDistance of search.js");
    }
}

// Returns the distance from  the points in the polyline to the address point.
function GetLinePoint2PointDistance(startPoint, linex, liney)
{
    try
    {
        var distanceInMetres;
        var distanceInMiles;

        distanceInMetres = Math.pow((linex - startPoint.x), 2) + Math.pow((liney - startPoint.y), 2);
        distanceInMetres = Math.sqrt(distanceInMetres);

        // Convert to miles.
        distanceInMiles = distanceInMetres * 0.00062137119;
        distanceInMiles = FormatNearestNumber(distanceInMiles);
        return distanceInMiles;
    }
    catch(ex)
    {
        C_HandleError(ex, "method GetLinePoint2PointDistance of search.js");
    }
}



// Returns the distance from passed in point to the address point.
function GetPoint2PointDistance(startPoint, endPoint)
{
    try
    {
        var distanceInMetres;
        var distanceInMiles;

        distanceInMetres = Math.pow((endPoint.x - startPoint.x), 2) + Math.pow((endPoint.y - startPoint.y), 2);
        distanceInMetres = Math.sqrt(distanceInMetres);

        // Convert to miles.
        distanceInMiles = distanceInMetres * 0.00062137119;
        distanceInMiles = FormatNearestNumber(distanceInMiles);
        return distanceInMiles;
    }
    catch(ex)
    {
        C_HandleError(ex, "method GetPoint2PointDistance of search.js");
    }
}
0 Kudos