Skip to content

Commit

Permalink
Speed up addLineStringEndpoints by 10x. #454
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jan 29, 2024
1 parent 3e12e04 commit caf3778
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/lib/browse/Filters.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
filterTextCopy: string,
filterAuthority: string,
filterFundingProgramme: string,
filterCurrentMilestone: string,
filterCurrentMilestone: string
) {
let filterNormalized = filterTextCopy.toLowerCase();
let filterFeatures = (feature: FeatureUnion) => {
Expand Down Expand Up @@ -136,7 +136,12 @@
$schemesGj = $schemesGj;
counts = counts;
}
$: filtersUpdated($filterText, filterAuthority, filterFundingProgramme, filterCurrentMilestone);
$: filtersUpdated(
$filterText,
filterAuthority,
filterFundingProgramme,
filterCurrentMilestone
);
function metersToMiles(x: number): number {
return x * 0.000621371;
Expand Down
23 changes: 14 additions & 9 deletions src/lib/maplibre/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,30 @@ export async function getStyleSpecification(
}

// Returns all the input features, additionally with a new point for every
// LineString endpoint, with an endpoint=true property.
// LineString endpoint, with an endpoint=true property. Note this drops any
// foreign members on the input, and that it reuses object references of the
// input for performance, rather than making a defensive deep copy.
export function addLineStringEndpoints(
input: FeatureCollection
): FeatureCollection {
let copy = JSON.parse(JSON.stringify(input));
let output: FeatureCollection = {
type: "FeatureCollection",
features: [],
};
// Add points for the ends of every LineString
let endpoints = [];
for (let f of copy.features) {
if (f.geometry.type == "LineString" && !f.properties.hide_while_editing) {
for (let f of input.features) {
output.features.push(f);

if (f.geometry.type == "LineString" && !f.properties?.hide_while_editing) {
for (let pt of [
f.geometry.coordinates[0],
f.geometry.coordinates[f.geometry.coordinates.length - 1],
]) {
endpoints.push({
output.features.push({
type: "Feature",
properties: {
endpoint: true,
scheme_reference: f.properties.scheme_reference,
scheme_reference: f.properties?.scheme_reference,
},
geometry: {
type: "Point",
Expand All @@ -213,8 +219,7 @@ export function addLineStringEndpoints(
}
}
}
copy.features = copy.features.concat(endpoints);
return copy;
return output;
}

// Properties are guaranteed to exist
Expand Down

0 comments on commit caf3778

Please sign in to comment.