I would like to reach the mid point of a polyline.
I just tried the centroid and realcentroid properties but I only got a soluction in ArcPy with the code below:
Midpoint = geometry.positionAlongLine(0.50,True).firstPoint
... print Midpoint.X
... print Midpoint.Y
How do I find the correct mid point (not centerline or gravity point of an irregular polyline)?
-
I don't even really know what language this is in. I thought I'd share it in case it helps: Calculate midpoint of PolylineUser1974– User19742019年12月01日 04:33:10 +00:00Commented Dec 1, 2019 at 4:33
2 Answers 2
Do you have to use esri js? Turf.js has a function called "along" that could be used for this, but you'd need to calculate the length of the line beforehand before halving it and sending that length into the .along() parameters. You could calculate the length in turf as well, or use esri if you prefer.
If it has to be esri js... have fun. I suppose that I would first get the length of the entire line using a geometry service (if this doesn't all need to be client side). From there, I would probably break the line into segments by iterating over its paths and then running along a those segments, getting the distance of each and keeping track until I found the right segment to finally drop a point with whatever distance I had left at an angle calculated from the segment endpoints.
Lucky for you, someone else already wrote this and seems to have cut out the step of exploding the geometry for us. How convenient!
This is my way to get the centerpoint of a Line. In this Case for the Lines of esris Measuretool.
var lineCenter = new Point((PointA.x+PointB.x)/2,(PointA.y+PointB.y)/2,map.spatialReference);
Maybe this is not the cleanest way to solve it, but it works.
Explore related questions
See similar questions with these tags.