-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from SteffenHaeussler/add_boolean_point_on_line
Add boolean_point_on_line module
- Loading branch information
Showing
37 changed files
with
1,284 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
36 changes: 36 additions & 0 deletions
36
turf/boolean_point_on_line/tests/false/LineWithOnly1SegmentIgnoreBoundary.geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
36 changes: 36 additions & 0 deletions
36
...boolean_point_on_line/tests/false/LineWithOnly1SegmentIgnoreBoundaryEnd.geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
36 changes: 36 additions & 0 deletions
36
...point_on_line/tests/false/LineWithOnly1SegmentIgnoreBoundaryEnd_inverse.geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
36 changes: 36 additions & 0 deletions
36
...nt_on_line/tests/false/LineWithOnly1SegmentIgnoreBoundaryEnd_inverse_v2.geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
36 changes: 36 additions & 0 deletions
36
...an_point_on_line/tests/false/LineWithOnly1SegmentIgnoreBoundary_inverse.geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
36 changes: 36 additions & 0 deletions
36
..._on_line/tests/false/LineWithOnly1SegmentIgnoreBoundary_inverse_v2 copy.geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
36 changes: 36 additions & 0 deletions
36
...point_on_line/tests/false/LineWithOnly1SegmentIgnoreBoundary_inverse_v2.geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.