-
Notifications
You must be signed in to change notification settings - Fork 124
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
optimize concatenation of centroids #989
base: develop
Are you sure you want to change the base?
Changes from 8 commits
cdfeac2
b237166
76989ea
f9121d7
3d1f279
565d42f
8e08be5
68b006e
b0003f3
c56eb8b
d9d1b94
59acc60
d27ae7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,16 +331,21 @@ def from_pnt_bounds(cls, points_bounds, res, crs=DEF_CRS): | |
} | ||
) | ||
|
||
def append(self, centr): | ||
"""Append Centroids | ||
def append(self, *centr): | ||
"""Append Centroids to the current centroid object for concatenation. | ||
|
||
This method check that all centroids use the same CRS, append the list of centroids to | ||
NicolasColombi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
the initial Centroid object and eventually concatenate them to create a single centroid | ||
NicolasColombi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
object with the union of all centroids. | ||
|
||
Note that the result might contain duplicate points if the object to append has an overlap | ||
with the current object. | ||
with the current object. Remove duplicates by either using :py:meth:`union` | ||
or calling :py:meth:`remove_duplicate_points` after appending. | ||
|
||
Parameters | ||
---------- | ||
centr : Centroids | ||
Centroids to append. The centroids need to have the same CRS. | ||
centr : list | ||
List of Centroids to append. The centroids need to have the same CRS. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To my understanding, the code as currently written would work if you call either @NicolasColombi and @peanutfun, should we maybe consider adapting the method so that it would also work when passing a list without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Benoit, I think you are right, I will update the docs strings, it shouldn't be a list. |
||
|
||
Raises | ||
------ | ||
|
@@ -351,32 +356,35 @@ def append(self, centr): | |
union : Union of Centroid objects. | ||
remove_duplicate_points : Remove duplicate points in a Centroids object. | ||
""" | ||
if not u_coord.equal_crs(self.crs, centr.crs): | ||
raise ValueError( | ||
f"The given centroids use different CRS: {self.crs}, {centr.crs}. " | ||
"The centroids are incompatible and cannot be concatenated." | ||
) | ||
self.gdf = pd.concat([self.gdf, centr.gdf]) | ||
for other in centr: | ||
if not u_coord.equal_crs(self.crs, other.crs): | ||
raise ValueError( | ||
f"The given centroids use different CRS: {self.crs}, {other.crs}. " | ||
"The centroids are incompatible and cannot be concatenated." | ||
) | ||
self.gdf = pd.concat([self.gdf] + [other.gdf for other in centr]) | ||
|
||
def union(self, *others): | ||
"""Create the union of Centroids objects | ||
"""Create the union of the current Centroids object with one or more other centroids | ||
objects by passing the list of centroids to :py:meth:`append` for concatenation and then | ||
removes duplicates. | ||
|
||
All centroids must have the same CRS. Points that are contained in more than one of the | ||
Centroids objects will only be contained once (i.e. duplicates are removed). | ||
|
||
Parameters | ||
---------- | ||
others : list of Centroids | ||
Centroids contributing to the union. | ||
others : list | ||
List of Centroids contributing to the union. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as above: if it's an |
||
|
||
Returns | ||
------- | ||
centroids : Centroids | ||
Centroids object containing the union of all Centroids. | ||
""" | ||
centroids = copy.deepcopy(self) | ||
for cent in others: | ||
centroids.append(cent) | ||
centroids.append(*others) | ||
|
||
return centroids.remove_duplicate_points() | ||
|
||
def remove_duplicate_points(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a dedicated test for
append
with multiple arguments