Create points at equal distance along a polyline

2548
4
Jump to solution
02-18-2019 03:38 AM
Naresh_KumarK
New Contributor II

Hi team,

how to get the points only which were located for every 2 kms on a poly line?

I tried Densify method to create new points along the poly line, but after densify, it returns the whole points list(newly created + old points). I can't figure out which was plotted at exactly 2 kms and which was not. So please suggest me a way to achieve this.

Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
ThadTilton
Esri Contributor

The problem is that your line is measured in degrees of longitude/latitude and the value you're passing in is for meters. The code is reading the distance of "1000.0" as 1000 degrees rather than 1 KM. Try projecting the line to a projected coordinate system for your area (looks like South Africa?). Be sure you know the units for the projected line, it might be feet instead of meters!

Here's an updated example that projects the line before processing, and also "unprojects" the result points back to Lat/Long (warning: this code hasn't been tested much 😞

double distance = 0.0;
Polyline projectedLine = (Polyline)GeometryEngine.Project(_originalPolyline, new SpatialReference(102472));
double lineLength = GeometryEngine.LengthGeodetic(projectedLine);
List<MapPoint> pointsOnLine = new List<MapPoint>();
SimpleMarkerSymbol sym = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.X, Color.Yellow, 16);
GraphicsOverlay go = MyMapView.GraphicsOverlays.First();
while (true)
{
    distance += 1000.0;
    if(distance > lineLength) { break; }
    MapPoint pt = GeometryEngine.CreatePointAlong(projectedLine, distance);
    MapPoint latLongPoint = (MapPoint)GeometryEngine.Project(pt, SpatialReferences.Wgs84);
    pointsOnLine.Add(latLongPoint);
    Graphic g = new Graphic(latLongPoint, sym);
    go.Graphics.Add(g);
}

(Also, check out the "ConvertTo" and "ConvertFrom" methods on the LinearUnits to convert between a variety of units. Works for angles and areas too: Esri.ArcGISRuntime.Geometry.LinearUnits.Kilometers.ConvertFrom(otherUnit, val))

View solution in original post

4 Replies
ThadTilton
Esri Contributor

Give the code below a try. It increments the distance to create a point along the line. When it reaches the end of the line it stops. It assumes the line is measured in meters, so uses 2000 to represent 2 km along the line. 

double distance = 0.0;
double lineLength = GeometryEngine.LengthGeodetic(_originalPolyline);
List<MapPoint> pointsOnLine = new List<MapPoint>();
SimpleMarkerSymbol sym = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.X, Color.Yellow, 16);
GraphicsOverlay go = MyMapView.GraphicsOverlays.First();
while (true)
{
   distance += 2000.0;
   if(distance > lineLength) { break; }

   MapPoint pt = GeometryEngine.CreatePointAlong(_originalPolyline, distance);
   pointsOnLine.Add(pt);
   Graphic g = new Graphic(pt, sym);
   go.Graphics.Add(g);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

HTH!

Thad

0 Kudos
Naresh_KumarK
New Contributor II

Thank you for your response Thad Tilton, 

I tried using your above implementation.

I have a poly line with 7 kms(approx) of length which i'm extracting from a shape file by creating a feature layer, now by following your code i want to create points along poly line at every 1km distance. below is the code

private void UsingCreatePointAlongMethod(Polyline polyline, double distance)
{
 double lineLength = GeometryEngine.LengthGeodetic(polyline);
 List<MapPoint> pointsOnLine = new List<MapPoint>();
 SimpleMarkerSymbol sym = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.X, Color.Yellow, 16);
 GraphicsOverlay go = MyMapView.GraphicsOverlays.First();
 while (true)
  {
   distance += 1000.0;
    if (distance > lineLength) { break; }
   MapPoint pt = GeometryEngine.CreatePointAlong(polyline, distance);
   pointsOnLine.Add(pt);
   Graphic g = new Graphic(pt, sym);
   go.Graphics.Add(g);
  }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

but the code returns the same point every time and it's always pointing at the starting of the poly line. here i'm also attaching the screenshots.

Thanks in advance.

0 Kudos
ThadTilton
Esri Contributor

The problem is that your line is measured in degrees of longitude/latitude and the value you're passing in is for meters. The code is reading the distance of "1000.0" as 1000 degrees rather than 1 KM. Try projecting the line to a projected coordinate system for your area (looks like South Africa?). Be sure you know the units for the projected line, it might be feet instead of meters!

Here's an updated example that projects the line before processing, and also "unprojects" the result points back to Lat/Long (warning: this code hasn't been tested much 😞

double distance = 0.0;
Polyline projectedLine = (Polyline)GeometryEngine.Project(_originalPolyline, new SpatialReference(102472));
double lineLength = GeometryEngine.LengthGeodetic(projectedLine);
List<MapPoint> pointsOnLine = new List<MapPoint>();
SimpleMarkerSymbol sym = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.X, Color.Yellow, 16);
GraphicsOverlay go = MyMapView.GraphicsOverlays.First();
while (true)
{
    distance += 1000.0;
    if(distance > lineLength) { break; }
    MapPoint pt = GeometryEngine.CreatePointAlong(projectedLine, distance);
    MapPoint latLongPoint = (MapPoint)GeometryEngine.Project(pt, SpatialReferences.Wgs84);
    pointsOnLine.Add(latLongPoint);
    Graphic g = new Graphic(latLongPoint, sym);
    go.Graphics.Add(g);
}

(Also, check out the "ConvertTo" and "ConvertFrom" methods on the LinearUnits to convert between a variety of units. Works for angles and areas too: Esri.ArcGISRuntime.Geometry.LinearUnits.Kilometers.ConvertFrom(otherUnit, val))

Naresh_KumarK
New Contributor II

Thank you very much Thad Tilton. Finally you solved my problem.

May God Bless You. 

0 Kudos