Select to view content in your preferred language

Find a point at a given distance along a line

11323
18
Jump to solution
12-28-2016 12:13 PM
GregRieck
Frequent Contributor

Hello everyone,

Does anyone have any experience with or sample code or sample geoprocessing events etc. that will return a point X,Y along a line (polyline) given a specific distance? For example if the polyline is 1000 feet long I want to know where along that line 250 feet is from it's starting vertex. I want the results as a point string format {"x" : -118.15, "y" : 33.80, "spatialReference" : {"wkid" : 4326}} so I can mark that location with an image on the web site.

Thank You,

Greg

0 Kudos
18 Replies
TomSellsted
MVP Regular Contributor

Greg,

Is the basemap in your app also in that projection? or is it web mercator?  If your basemap is in web mercator, you can set the outSpatialReference for the query to web mercator.  Something like:

query.outSpatialReference = new SpatialReference(102100);

Regards,

Tom

TomSellsted
MVP Regular Contributor

Greg,

There was also a post from a couple of years ago that uses a similar method, but might work on your projection.

https://community.esri.com/servlet/JiveServlet/download/525576-1-135992/pointAlongLine.txt.zip 

Regards,

Tom

0 Kudos
GregRieck
Frequent Contributor

Tom,

Thanks for the response, I was able to figure it out and get it working. Still doing some final testing but it's looking great. Again thanks for posting that project it was exactly what I needed.

Greg

0 Kudos
TomSellsted
MVP Regular Contributor

Greg,

You are welcome!  Glad you got things working!

Regards,

Tom

0 Kudos
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

Good stuff!

0 Kudos
GregRieck
Frequent Contributor

Tom,

I have found a small issue with this method. It seems to always determine the position of the point along the line starting from the "From" point of the line. In some cases I need it to start from the other end of the line the "To" point of the line. How can I change the calculatePositionAlong so that it starts from the TO position and works toward the FROM position? I'm pretty sure I can come up with something that can be used to determine which way to start from.

Greg

0 Kudos
TomSellsted
MVP Regular Contributor

Greg,

Probably the simplest solution would be to reverse the order of the path points in the polyline used.  I have updated my example to show how to do this.  Here is a function that will reverse the path order:

function reversePolylineOrder() {
     var newPL = new Polyline(map.spatialReference);
     var newPath = [];
     for (var i = pl.paths[0].length - 1; i >= 0; i--) {
          newPath.push(pl.getPoint(0, i));
     }
     newPL.addPath(newPath);
     pl = newPL;
}

Regards,

Tom

GregRieck
Frequent Contributor

Tom, thank you for the continued support. This is where I got with what you provide me.

I got a few errors and fixed those by adding geometry as indicated below. However, after I  assign pl = newPL, pl has no geometry. Which is important because calculatePositionAlong uses it:

arrayUtils.forEach(pl.geometry.paths[0], function (coords) {

And I use it to create a new graphic used to display myimage at the found point along the line,

 var graphic = new Graphic(pl.geometry, myimage);

var newPL = new Polyline(map.spatialReference);
var newPath = [];
for (var i = pl.geometry.paths[0].length - 1; i >= 0; i--) {
newPath.push(pl.geometry.getPoint(0, i));
}
newPL.addPath(newPath);
pl = newPL;

How can I make sure pl has geometry?

Greg

0 Kudos
TomSellsted
MVP Regular Contributor

Greg,

Sorry for the late reply. I am on the road today and have limited access to the internet.

I would set a breakpoint on the last line and then you can examine the values for both the pl and newPL variables. The code looks correct, the pl variable may not have a value set.

Regards,

Tom

46° 36' 17.86"N 120° 30' 18.23"W

0 Kudos