Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Return of "does not work on derived classes?" #97

Closed
devleaks opened this issue Jun 1, 2021 · 3 comments
Closed

The Return of "does not work on derived classes?" #97

devleaks opened this issue Jun 1, 2021 · 3 comments

Comments

@devleaks
Copy link

devleaks commented Jun 1, 2021

Hello,

I have the same problem as #94 with other function calls: An Edge is a subclass of a Feature and point_to_line_distance fails to recognize an edge as a Feature...

I think that there is a more general issue with type/subtype checking.

Also, the documentation does not always reflect the code: For exemple, distance is documented as distance(Feature, Feature). But distance(Point, Point) also works and is not documented.

I have the feeling that more generally, type should be enforced at function parameter level: point_to_line_distance(Feature, Feature) is too loose. The coder must check that the Feature geometry has the appropriate type. point_to_line_distance(Point, Line) is more precise and type checking is done by Python at runtime. There is no need to add more checks inside the code of the function.

BTW, point_to_line_distance should return the closest point somehow as well. May be a function nearest_point_on_line(Point, Line) returns the nearest point, and its distance to the line can be determined with distance.

Here is a piece of code that exhibit the issue:

`
from geojson import Point, LineString, Feature
from turfpy.measurement import point_to_line_distance

class Edge(Feature):
def init(self, node: str, start: Feature, end: Feature):
Feature.init(self, geometry=LineString((start["geometry"]["coordinates"], end["geometry"]["coordinates"])))
self.name = node

p1 = Point((25.25458, 51.623879))
f1 = Feature(geometry=p1)
p2 = Point((25.254626, 51.624053))
f2 = Feature(geometry=p2)

e1 = Edge("e1", start=f1, end=f2)

p0 = Point((25.0, 51.0))
f0 = Feature(geometry=p0)

fline = Feature(geometry=LineString((f1["geometry"]["coordinates"], f2["geometry"]["coordinates"])))
print(point_to_line_distance(f0, fline))
#71.59329853730718

fedge = Edge(node="e1", start=f1, end=f2)
print(isinstance(fedge, Feature))
#True
print(point_to_line_distance(f0, fedge))
Traceback (most recent call last):
File "/Users/pierre/Developer/Internet/js/gip/emitpy/tests/ft3.py", line 28, in
print(point_to_line_distance(f0, fedge))
File "/usr/local/miniconda3/lib/python3.9/site-packages/turfpy/measurement.py", line 1004, in point_to_line_distance
feature_of(line, "LineString", "line")
File "/usr/local/miniconda3/lib/python3.9/site-packages/turfpy/helper.py", line 120, in feature_of
raise Exception(
Exception: Invalid input to line, Feature with geometry required
`

P.

@devleaks
Copy link
Author

devleaks commented Jun 1, 2021

I also have the issue of classes with the following hierarchy:
A Line is a subclass of a LineString (a Line is a LineString with just 2 points, start and end). I create a Feature from the Line.
nearest_point_on_line(Feature, Feature) fails, telling the Line is not a regognized LineString, although isinstance(aLine, LineString) is True.

@sackh
Copy link
Collaborator

sackh commented Jun 3, 2021

@devleaks Thanks again, I think we need to check at the project level and see where ever possible to make types/subtypes specific. It will take some time to fix all the possible functions. Please feel free to open PR. We can do it iteratively each function level.

@devleaks
Copy link
Author

Issue actually resides in GeoJSON package. See here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants