-
Notifications
You must be signed in to change notification settings - Fork 9
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
Update tidy logic #18
Conversation
geojson-tidy.js
Outdated
while (keepIdxs.length > (filter.maximumPoints - 2)) { | ||
// randomly select one of the elements to remove | ||
removeIdx = Math.floor(Math.random() * keepIdxs.length) | ||
keepIdxs = keepIdxs.filter((element, index) => index !== removeIdx) | ||
} |
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.
because of .filter
this is O(n) to remove one point.
I would suggest either use a set for O(1) removals or generate all the points and filter them out in one go
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.
If you generate all the points first, you also have to keep track of which ones you have already "removed". By doing it this way, the list is already modified when you move to the next iteration.
This part won't see much heavy usage, as it comes after the time/distance filtering, which naturally reduces the length of the array. I think for now it's not worth optimising too far.
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.
why not do something equivalent to
const pointsToRemoveSet = new Set(...) // populate somehow
// at the very end call .filter once
keepIdxs = keepIdxs.filter(element => !pointsToRemoveSet.has(element));
I think this would be much cheaper in the end.
I am just thninking what if there are 1000 points and we need to remove all but 100, we cannot afford to make that many .filter calls
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.
also have to keep track of which ones you have already "removed".
not necessarily, I think this can be done efficiently too. For example, shuffle array and take first X points as points to keep/to remove
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.
LGTM!
maximumPoints
coordinates, previously we split the output into multiple features. Now, we randomly samplemaximumPoints
probes from the trace by removing a random probe until we hit the desiredmaximumPoints
. Note this happens after time/distance filtering.minimumDistance
andminimumTime
to be set to 0 (in this case the only effect will be the random sampling if the input length is >maximumPoints
). Previously, supplying 0 for these values would cause them to be set to the defaults of 10 and 5 respectively.coordTimes
, if they are present in the input.