Skip to content

Commit

Permalink
Don't use turf to measure shortcut length
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Apr 21, 2024
1 parent c5d46c1 commit 53fab02
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
8 changes: 5 additions & 3 deletions backend/src/shortcuts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ impl Path {
pts.extend(rev);
}
}
let mut f = Feature::from(Geometry::from(
&map.mercator.to_wgs84(&LineString::new(pts)),
));
let linestring = LineString::new(pts);

let length = linestring.euclidean_length();
let mut f = Feature::from(Geometry::from(&map.mercator.to_wgs84(&linestring)));
f.set_property("directness", self.directness);
f.set_property("length_meters", length);
f
}
}
14 changes: 0 additions & 14 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@maptiler/geocoding-control": "^1.2.2",
"@picocss/pico": "^2.0.3",
"@turf/along": "^6.5.0",
"@turf/length": "^6.5.0",
"maplibre-draw-polygon": "github:dabreegster/maplibre-draw-polygon",
"route-snapper": "^0.4.0",
"svelte-maplibre": "^0.8.3"
Expand Down
14 changes: 9 additions & 5 deletions web/src/AnimatePaths.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<script lang="ts">
import turfLength from "@turf/length";
import along from "@turf/along";
import { layerId } from "./common";
import type { FeatureCollection, LineString } from "geojson";
import { GeoJSON, CircleLayer } from "svelte-maplibre";
import { onDestroy } from "svelte";
export let paths: FeatureCollection<LineString, { directness: number }>;
export let paths: FeatureCollection<
LineString,
{ directness: number; length_meters: number }
>;
let totalDirectness = sumWeights();
let numDots = 50;
Expand All @@ -15,7 +18,6 @@
interface Dot {
idx: number;
length: number;
distance: number;
}
Expand Down Expand Up @@ -53,7 +55,6 @@
if (rand < cumulativeWeight) {
return {
idx,
length: turfLength(path, { units: "kilometers" }),
distance: 0,
};
}
Expand All @@ -75,7 +76,10 @@
function animate() {
for (let [idx, dot] of dots.entries()) {
dot.distance += stepKm;
if (dot.distance >= dot.length) {
if (
dot.distance >=
paths.features[dot.idx].properties.length_meters / 1000
) {
dots[idx] = startDot();
}
}
Expand Down

0 comments on commit 53fab02

Please sign in to comment.