Skip to content

Commit

Permalink
#368 migrate route map
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarc committed Jul 17, 2024
1 parent 50372be commit 23e245f
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 51 deletions.
35 changes: 20 additions & 15 deletions frontend/src/app/analysis/route/map/components/route-map.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { inject } from '@angular/core';
import { Injectable } from '@angular/core';
import { RouteMapInfo } from '@api/common/route';
import { OlUtil } from '@app/ol';
import { RouteMapPage } from '@api/common/route';
import { MapPosition } from '@app/ol/domain';
import { ZoomLevel } from '@app/ol/domain';
import { OpenDataLayers } from '@app/ol/layers';
Expand All @@ -11,22 +10,22 @@ import { MapLayerRegistry } from '@app/ol/layers';
import { OsmLayer } from '@app/ol/layers';
import { TileDebug256Layer } from '@app/ol/layers';
import { NetworkVectorTileLayer } from '@app/ol/layers';
import { RouteLayers } from '@app/ol/layers';
import { MapClickService } from '@app/ol/services';
import { OpenlayersMapService } from '@app/ol/services';
import { NodeMapStyle } from '@app/ol/style';
import { Coordinate } from 'ol/coordinate';
import { Extent } from 'ol/extent';
import Map from 'ol/Map';
import { fromLonLat } from 'ol/proj';
import { ViewOptions } from 'ol/View';
import View from 'ol/View';

@Injectable()
export class RouteMapService extends OpenlayersMapService {
private readonly mapClickService = inject(MapClickService);

init(routeMapInfo: RouteMapInfo, mapPositionFromUrl: MapPosition, urlLayerIds: string[]): void {
this.registerLayers(routeMapInfo, urlLayerIds);
init(routeMapPage: RouteMapPage, mapPositionFromUrl: MapPosition, urlLayerIds: string[]): void {
this.registerLayers(routeMapPage, urlLayerIds);

let viewOptions: ViewOptions = {
minZoom: ZoomLevel.minZoom,
Expand Down Expand Up @@ -57,33 +56,39 @@ export class RouteMapService extends OpenlayersMapService {
const view = this.map.getView();

if (!mapPositionFromUrl) {
view.fit(this.buildExtent(routeMapInfo));
view.fit(this.buildExtent(routeMapPage));
}

this.finalizeSetup(true);
}

private registerLayers(routeMapInfo: RouteMapInfo, urlLayerIds: string[]): void {
private registerLayers(page: RouteMapPage, urlLayerIds: string[]): void {
const networkVectorTileLayer = NetworkVectorTileLayer.build(
routeMapInfo.networkType,
page.routeMapInfo.networkType,
new NodeMapStyle().styleFunction()
);
const routeLayers = new RouteLayers(routeMapInfo.map).build();
// const routeLayers = new RouteLayers(page.map).build();
const registry = new MapLayerRegistry();
registry.register(urlLayerIds, BackgroundLayer.build(), true);
registry.register(urlLayerIds, OsmLayer.build(), false);
registry.register(urlLayerIds, networkVectorTileLayer, true);
routeLayers.forEach((mapLayer) => registry.register(urlLayerIds, mapLayer, true));
OpenDataLayers.register(registry, routeMapInfo.networkType, urlLayerIds);
// routeLayers.forEach((mapLayer) => registry.register(urlLayerIds, mapLayer, true));
OpenDataLayers.register(registry, page.routeMapInfo.networkType, urlLayerIds);
registry.register(urlLayerIds, TileDebug256Layer.build(), false);

this.register(registry);
}

private buildExtent(routeMapInfo: RouteMapInfo): Extent {
const bounds = routeMapInfo.map.bounds;
const min = OlUtil.toCoordinate(bounds.latMin, bounds.lonMin);
const max = OlUtil.toCoordinate(bounds.latMax, bounds.lonMax);
private buildExtent(page: RouteMapPage): Extent {
const bounds = page.bounds;
const latExtra = (bounds.maxLat - bounds.minLat) / 5;
const lonExtra = (bounds.maxLon - bounds.minLon) / 5;
const latMinAdjusted = bounds.minLat - latExtra;
const latMaxAdjusted = bounds.maxLat + latExtra;
const lonMinAdjusted = bounds.minLon - lonExtra;
const lonMaxAdjusted = bounds.maxLon + lonExtra;
const min = fromLonLat([lonMinAdjusted, latMinAdjusted]);
const max = fromLonLat([lonMaxAdjusted, latMaxAdjusted]);
return [min[0], min[1], max[0], max[1]];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class RouteMapPageService {
const mapPositionString = this.routerService.queryParam('position');
const mapPositionFromUrl = MapPosition.fromQueryParam(mapPositionString);
this.routeMapService.init(
this.response().result.routeMapInfo,
this.response().result,
mapPositionFromUrl,
this.routerService.urlLayerIds()
);
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/app/api/common/route/route-map-info.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// this file is generated, please do not modify

import { NetworkType } from '@api/custom';
import { RouteMap } from './route-map';
import { RoutePath } from './route-path';
import { RouteSegment } from './route-segment';

export interface RouteMapInfo {
readonly routeId: number;
readonly routeName: string;
readonly networkType: NetworkType;
readonly map: RouteMap;
readonly segments: RouteSegment[];
readonly paths: RoutePath[];
}
2 changes: 2 additions & 0 deletions frontend/src/app/api/common/route/route-map-page.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// this file is generated, please do not modify

import { Bounds } from '@api/common';
import { RouteMapInfo } from './route-map-info';

export interface RouteMapPage {
readonly routeMapInfo: RouteMapInfo;
readonly bounds: Bounds;
readonly changeCount: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ case class RouteMapInfo(
routeId: Long,
routeName: String,
networkType: NetworkType,
map: RouteMap
segments: Seq[RouteSegment],
paths: Seq[RoutePath],
)
3 changes: 3 additions & 0 deletions server/src/main/scala/kpn/api/common/route/RouteMapPage.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package kpn.api.common.route

import kpn.api.common.Bounds

case class RouteMapPage(
routeMapInfo: RouteMapInfo,
bounds: Bounds,
changeCount: Long
)
12 changes: 6 additions & 6 deletions server/src/main/scala/kpn/core/util/Util.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ object Util {

def classNameOf(obj: Any): String = obj.getClass.getSimpleName.filterNot(_ == '$')

def mergeBounds(boundss: Seq[Bounds]): Bounds = {
if (boundss.isEmpty) {
def mergeBounds(boundsCollection: Seq[Bounds]): Bounds = {
if (boundsCollection.isEmpty) {
Bounds()
}
else {
val minLat = boundss.map(_.minLat).min
val maxLat = boundss.map(_.maxLat).max
val minLon = boundss.map(_.minLon).min
val maxLon = boundss.map(_.maxLon).max
val minLat = boundsCollection.map(_.minLat).min
val maxLat = boundsCollection.map(_.maxLat).max
val minLon = boundsCollection.map(_.minLon).min
val maxLon = boundsCollection.map(_.maxLon).max
Bounds(
minLat,
minLon,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package kpn.database.actions.routes

import kpn.api.common.route.RouteMapInfo
import kpn.database.base.Database
import kpn.core.util.Log
import kpn.database.base.Database
import org.mongodb.scala.model.Aggregates.filter
import org.mongodb.scala.model.Aggregates.project
import org.mongodb.scala.model.Filters.equal
import org.mongodb.scala.model.Projections.computed
import org.mongodb.scala.model.Projections.excludeId
import org.mongodb.scala.model.Projections.fields
import org.mongodb.scala.model.Projections.include

object MongoQueryRouteMapInfo {
private val log = Log(classOf[MongoQueryRouteMapInfo])
Expand All @@ -28,12 +29,13 @@ class MongoQueryRouteMapInfo(database: Database) {
computed("routeId", "$_id"),
computed("routeName", "$summary.name"),
computed("networkType", "$summary.networkType"),
computed("map", "$analysis.map"),
include("segments"),
include("paths"),
)
)
)
val routeMapInfo = database.routes.optionAggregate[RouteMapInfo](pipeline, log)
(s"route map info: ${routeMapInfo.size}", routeMapInfo)
(s"route map info", routeMapInfo)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package kpn.server.api.analysis.pages.route

import kpn.api.common.route.RouteMapPage
import kpn.core.util.Util
import kpn.server.repository.ChangeSetRepository
import kpn.server.repository.RouteRepository
import org.springframework.stereotype.Component

trait RouteMapPageBuilder {

def build(routeId: Long): Option[RouteMapPage]
@Component
class RouteMapPageBuilder(
routeRepository: RouteRepository,
changeSetRepository: ChangeSetRepository
) {

def build(routeId: Long): Option[RouteMapPage] = {
routeRepository.mapInfo(routeId).map { routeMapInfo =>
val changeCount = changeSetRepository.routeChangesCount(routeId)
val bounds = Util.mergeBounds(routeMapInfo.segments.map(_.bounds))
RouteMapPage(routeMapInfo, bounds, changeCount)
}
}
}

This file was deleted.

0 comments on commit 23e245f

Please sign in to comment.