Skip to content

Commit

Permalink
Merge pull request #105 from geoblocks/poi_indexes
Browse files Browse the repository at this point in the history
Set poi indexes
  • Loading branch information
fredj authored Aug 25, 2023
2 parents 51327aa + e953889 commit cad8034
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
11 changes: 8 additions & 3 deletions demos/schm/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export const poiPoint = new Style({
}),
text: new Text({
font: "11px Inter",
text: "99",
offsetY: -10,
fill: new Fill({
color: "#fff",
Expand All @@ -109,7 +108,6 @@ export const poiPointSketchHit = new Style({
}),
text: new Text({
font: "11px Inter",
text: "99",
offsetY: -10,
fill: new Fill({
color: "#fff",
Expand Down Expand Up @@ -164,7 +162,14 @@ export function styleFunction(feature) {
}
return withPointerDevice ? null : sketchControlPoint;
case "POI":
return sketchHitGeometry ? poiPointSketchHit : poiPoint;
if (sketchHitGeometry) {
return poiPointSketchHit
}
const index = feature.get("index");
if (index !== undefined) {
poiPoint.getText().setText((index + 1).toString());
}
return poiPoint;
case "controlPoint":
if (!withPointerDevice && sketchHitGeometry) {
return sketchControlPointHint;
Expand Down
17 changes: 17 additions & 0 deletions src/interaction/TrackData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {equals} from 'ol/coordinate.js';
import Feature from 'ol/Feature.js';
import LineString from 'ol/geom/LineString.js';
import MultiPoint from 'ol/geom/MultiPoint.js';
import type Point from 'ol/geom/Point.js';
import type {Coordinate} from 'ol/coordinate.js';

Expand Down Expand Up @@ -66,6 +67,7 @@ export default class TrackData {
this.segments = segments;
this.pois = pois;
this.controlPoints = controlPoints;
this.updatePOIIndexes();
}

getAdjacentSegments(controlPoint: Feature<Point>): AdjacentSegments {
Expand Down Expand Up @@ -254,6 +256,21 @@ export default class TrackData {
};
}

updatePOIIndexes() {
// build a multi point geometry from all segments coordinates
const points = new MultiPoint(this.segments.map((s) => s.getGeometry().getCoordinates()).flat());
const pointsCoordinates = points.getCoordinates();
const sorted = this.pois.map((poi) => {
// find the closest point to the POI and returns its index; that's it's "distance" from the start
const closestPoint = points.getClosestPoint(poi.getGeometry().getCoordinates());
return {
poi: poi,
index: pointsCoordinates.findIndex((c) => equals(c, closestPoint))
};
}).sort((a, b) => a.index - b.index);
sorted.forEach((s, index) => s.poi.set('index', index));
}

/*
* Remove the last control point.
*/
Expand Down
23 changes: 12 additions & 11 deletions src/interaction/TrackManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,19 @@ class TrackManager {
*
* @param {import ('./TrackInteractionModify').ModifyEvent} event
*/
(event) => {
async (event) => {
const feature = event.feature;
const type = feature.get('type');

if (type === 'POI') {
this.trackData_.updatePOIIndexes();
this.onTrackChanged_();
} else if (type === 'controlPoint') {
this.updater_.updateAdjacentSegmentsGeometries(feature).then(() => {
this.updater_.changeAdjacentSegmentsStyling(feature, '');
this.updater_.computeAdjacentSegmentsProfile(feature);
})
.then(() => this.onTrackChanged_());
await this.updater_.updateAdjacentSegmentsGeometries(feature);
this.updater_.changeAdjacentSegmentsStyling(feature, '');
await this.updater_.computeAdjacentSegmentsProfile(feature);
this.trackData_.updatePOIIndexes();
this.onTrackChanged_();
} else if (type === 'segment') {
const indexOfSegment = this.trackData_.getSegments().indexOf(feature);

Expand All @@ -246,11 +247,11 @@ class TrackManager {
console.assert(!!before && !!after);
this.source_.addFeatures([before, after]);

this.updater_.updateAdjacentSegmentsGeometries(controlPoint).then(() => {
this.updater_.changeAdjacentSegmentsStyling(controlPoint, '');
this.updater_.computeAdjacentSegmentsProfile(controlPoint);
})
.then(() => this.onTrackChanged_());
await this.updater_.updateAdjacentSegmentsGeometries(controlPoint);
this.updater_.changeAdjacentSegmentsStyling(controlPoint, '');
await this.updater_.computeAdjacentSegmentsProfile(controlPoint);
this.trackData_.updatePOIIndexes();
this.onTrackChanged_();
}
});

Expand Down

0 comments on commit cad8034

Please sign in to comment.