Select to view content in your preferred language

Where is the point?

186
0
2 weeks ago
Labels (1)
DanPatterson
MVP Esteemed Contributor
8 0 186

Start at a point, in this case a point midway along a line.  (seg0, and pnt0)

Find the point on the next line that is closest to it.  (seg1 and pnt1)

Return basic information, like azimuth and distance.

Basic functions.

    import numpy as np

    def _pnt_on_seg_(seg, pnt):
        """Mini pnt_on_seg function normally required by pnt_on_poly."""
        x0, y0, x1, y1, dx, dy = *pnt, *seg[0], *(seg[1] - seg[0])
        dist_ = dx * dx + dy * dy  # squared length
        u = ((x0 - x1) * dx + (y0 - y1) * dy) / dist_
        u = max(min(u, 1), 0)  # u must be between 0 and 1
        xy = (np.array([dx, dy]) * u) + [x1, y1]  # noqa
        return xy

    def _line_dir_(orig, dest, azimuth):
        """Mini line direction function."""
        orig = np.atleast_2d(orig)
        dest = np.atleast_2d(dest)
        dxy = dest - orig
        ang = np.degrees(np.arctan2(dxy[:, 1], dxy[:, 0]))
        if azimuth:  # if True, correct to North, otherwise return the angle
            ang = np.mod((450.0 - ang), 360.)
        return ang

 

Do a run

# planar coordinates in, euclidean values out
seg0 = np.array([[0., 0.], [10., 0.]])
seg1 = np.array([[0., 5.], [10., 6.]])
pnt0 = np.mean(seg0, axis=0)
pnt1 = _pnt_on_seg_(seg1, pnt0)
azim = _line_dir_(pnt0, pnt1, True)  # azimuth, use ...False to get angle
d_01 = np.linalg.norm(pnt0 - pnt1)   # euclidean distance

azim, d_01
 354.29, 5.472704546154941

 

Now that you have reached your destination, perhaps you want to do something with it, or move on to the next segment or a polygon boundary.  The possibilities are endless

pnt_on_line.png

And another couple of options.

The first is with the first segment location specified, then the location of the remaining points are calculated. 

The second is with each segment having a specified location (50% along line) and closest points calculated between each.  The final line extending upwards connects those in sequence.

pnt_on_line2.png

 

 

 

Tags (3)
Contributors
About the Author
Retired Geomatics Instructor (also DanPatterson_Retired). Currently working on geometry projects (various) as they relate to GIS and spatial analysis. I use NumPy, python and kin and interface with ArcGIS Pro.
Labels