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

Refactor WKT for reuse and speed. #97

Merged
merged 2 commits into from
Jan 31, 2023

Conversation

eseglem
Copy link
Collaborator

@eseglem eseglem commented Jan 30, 2023

What I am changing

  • Refactored WKT code to not instantiate new objects all the way down. The previous method had to construct and validate other geometry types to get the WKT.
  • Updated pre-commit versions because my local repo wouldn't let me commit without updating. Excluding pydocstyle which changed and threw a bunch of errors.

How I did it

  • Created shared functions to reduce code duplication and make code easier to read.
  • Used those functions recursively to avoid other geometries.

How you can test it

  • Kept existing tests, and updated as needed for additional cases.

Related Issues

This doesn't address one issue I noticed while testing: mixed dimensionality. Shapely doesn't allow for it. So a MultiPoint(coordinates=[(0,0),(1,1,1)]) will become MULTIPOINT Z (0 0 0, 1 1 1) but we do not enforce that. Not sure where to go with that.

I would also like to use hypothesis to generate tests with the pydantic hypothesis plugin rather than having so many hard coded hard to read values. It would likely break for the above case, but it would be good to know more about where things are different.

@@ -50,7 +65,11 @@ def _wkt_type(self) -> str:
@property
def wkt(self) -> str:
"""Return the Well Known Text representation."""
return f"{self._wkt_type}{self._wkt_inset}({self._wkt_coordinates})"
return self._wkt_type + (
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this change didn't have to be in this PR, since self.coordinates cannot be None yet.

@@ -22,7 +22,7 @@ def assert_wkt_equivalence(geom: Union[Geometry, GeometryCollection]):
"""Assert WKT equivalence with Shapely."""
# Remove any trailing `.0` to match Shapely format
clean_wkt = re.sub(r"\.0(\D)", r"\1", geom.wkt)
assert shape(geom.dict()).wkt == clean_wkt
assert shape(geom).wkt == clean_wkt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shape uses the __geo_interface__ to construct so don't need .dict(). This also tests our implementation is usable by shapely.

[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)]],
# Two lines, two points each, no Z
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there was a mismatch not being caught without these tests. There was a join with just comma and not comma + space.

@vincentsarago
Copy link
Member

This doesn't address one issue I noticed while testing: mixed dimensionality. Shapely doesn't allow for it. So a MultiPoint(coordinates=[(0,0),(1,1,1)]) will become MULTIPOINT Z (0 0 0, 1 1 1) but we do not enforce that. Not sure where to go with that.

I would also like to use hypothesis to generate tests with the pydantic hypothesis plugin rather than having so many hard coded hard to read values. It would likely break for the above case, but it would be good to know more about where things are different.

Let's open new issues for those case.

As far I'm concerned, this PR is good to go. @eseglem I'll let you set it to Ready for review before I merge it

@eseglem eseglem marked this pull request as ready for review January 31, 2023 13:15
@vincentsarago vincentsarago merged commit b45b771 into developmentseed:main Jan 31, 2023
@eseglem eseglem deleted the feature/wkt-refactor branch February 10, 2023 14:17
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

Successfully merging this pull request may close these issues.

2 participants