Skip to content

Commit

Permalink
Merge pull request #38 from SteffenHaeussler/add_boolean_point_on_line
Browse files Browse the repository at this point in the history
Add boolean_point_on_line module
  • Loading branch information
diogomatoschaves committed Jun 4, 2020
2 parents 7a6e5f3 + bb64432 commit dc50695
Show file tree
Hide file tree
Showing 37 changed files with 1,284 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Currently, the following modules have been implemented:
- [bbox-polygon](https://github.com/diogomatoschaves/pyturf/tree/master/turf/bbox_polygon)
- [bearing](https://github.com/diogomatoschaves/pyturf/tree/master/turf/bearing)
- [boolean-point-in-polygon](https://github.com/diogomatoschaves/pyturf/tree/master/turf/boolean_point_in_polygon)
- [boolean-point-on-line](https://github.com/diogomatoschaves/pyturf/tree/master/turf/boolean_point_on_line)
- [center](https://github.com/diogomatoschaves/pyturf/tree/master/turf/center)
- [centroid](https://github.com/diogomatoschaves/pyturf/tree/master/turf/centroid)
- [destination](https://github.com/diogomatoschaves/pyturf/tree/master/turf/destination)
Expand Down
6 changes: 6 additions & 0 deletions docs/source/modules/booleans.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ boolean-point-in-polygon
------------------------

.. autofunction:: turf.boolean_point_in_polygon


boolean-point-on-line
---------------------

.. autofunction:: turf.boolean_point_on_line
1 change: 1 addition & 0 deletions turf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from turf.bbox_polygon import bbox_polygon
from turf.bearing import bearing
from turf.boolean_point_in_polygon import boolean_point_in_polygon
from turf.boolean_point_on_line import boolean_point_on_line
from turf.center import center
from turf.centroid import centroid
from turf.destination import destination
Expand Down
1 change: 1 addition & 0 deletions turf/boolean_point_on_line/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from turf.boolean_point_on_line._boolean_point_on_line import boolean_point_on_line
97 changes: 97 additions & 0 deletions turf/boolean_point_on_line/_boolean_point_on_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from typing import Dict, List, Sequence, TypeVar, Union
from decimal import Decimal, getcontext
from math import sqrt

import numpy as np

from turf.helpers import Feature, LineString, Point

from turf.helpers import feature_collection, line_string, multi_line_string
from turf.invariant import get_coords_from_features

ctx = getcontext()
ctx.prec = 28

PointFeature = TypeVar("PointFeature", Dict, Feature, Point)
LineFeature = TypeVar("LineFeature", Dict, Feature, LineString)


def boolean_point_on_line(
point: PointFeature, line: LineFeature, options: Dict = {}
) -> bool:
"""
Returns True if a point is on a line else False.
Accepts a optional parameter to ignore the start and end vertices of the linestring.
:param point: {Point} GeoJSON Point
:param line: {LineString} GeoJSON LineString
:param options: Optional parameters
[options["ignoreEndVertices"]=False] whether to ignore the start and end vertices
:return: boolean True/False if point is on line
"""
if not isinstance(options, dict):
options = {}

point_on_line = False

ignore_end_vertices = options.get("ignoreEndVertices", False)

point_coord = get_coords_from_features(point, ("Point",))
line_coords = get_coords_from_features(line, ("LineString",))

for i in range(len(line_coords) - 1):

if ignore_end_vertices:

# ignore if point_coord are the line start
if (i == 0) and (point_coord == line_coords[i]):
continue

# ignore if point_coord are the line end
if ((i + 1) == (len(line_coords) - 1)) and (
point_coord == line_coords[i + 1]
):
continue

point_on_line = point_on_segment(
point_coord, line_coords[i], line_coords[i + 1]
)

if point_on_line:
break

return point_on_line


def point_on_segment(
point: List, segment_start: List, segment_end: List, epsilon: float = 1.0e-14
) -> bool:
"""
Checks if a given point is on a line or not.
Since this is a comparison of floats, I use the Decimal module of python
:param point: Coordinates of a point
:param segment_start: Coordinates of the start line
:param segment_end: Coordinates of the line end
:return: bool
"""

len_segment = Decimal(
sqrt(
pow(segment_end[0] - segment_start[0], 2)
+ pow(segment_end[1] - segment_start[1], 2)
)
)

len_point_seg_1 = Decimal(
sqrt(pow(point[0] - segment_start[0], 2) + pow(point[1] - segment_start[1], 2))
)

len_point_seg_2 = Decimal(
sqrt(pow(segment_end[0] - point[0], 2) + pow(segment_end[1] - point[1], 2))
)

return abs(len_segment - len_point_seg_1 - len_point_seg_2) <= epsilon
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "FeatureCollection",
"properties": {
"ignoreEndVertices": true
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
0,
0
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
3,
3
]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "FeatureCollection",
"properties": {
"ignoreEndVertices": true
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
3,
3
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
3,
3
]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "FeatureCollection",
"properties": {
"ignoreEndVertices": true
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
-3,
-3
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
-3,
-3
]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "FeatureCollection",
"properties": {
"ignoreEndVertices": true
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
-3,
-6
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
-3,
-6
]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "FeatureCollection",
"properties": {
"ignoreEndVertices": true
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
0,
0
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
-3,
-3
]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "FeatureCollection",
"properties": {
"ignoreEndVertices": true
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
0,
0
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
0,
5
]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "FeatureCollection",
"properties": {
"ignoreEndVertices": true
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
0,
5
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
0,
5
]
]
}
}
]
}

0 comments on commit dc50695

Please sign in to comment.