Skip to content

Commit

Permalink
#368 migrate structure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarc committed Jul 14, 2024
1 parent 42cb5a0 commit daf5f16
Show file tree
Hide file tree
Showing 24 changed files with 294 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kpn.server.analyzer.engine.analysis.route.structure

case class NewRouteSegmentElementFragmentGroup(
surface: String,
fragments: Seq[NewRouteSegmentElementFragment]
) {
def nodeIds: Seq[Long] = fragments.head.nodeIds ++ fragments.tail.flatMap(_.nodeIds.tail)
}
11 changes: 11 additions & 0 deletions server/src/main/scala/kpn/api/common/route/RoutePath.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kpn.api.common.route

case class RoutePath(
id: Long,
name: String, // forward, backward, start-tentacle-1, ...
// fromNodeId: Long,
// fromNodeName: String,
// toNodeId: Long,
// toNodeName: String,
elementIds: Seq[Long]
)
12 changes: 12 additions & 0 deletions server/src/main/scala/kpn/api/common/route/RouteSegment.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kpn.api.common.route

import kpn.api.common.Bounds

case class RouteSegment(
id: Long,
startNodeId: Long,
endNodeId: Long,
meters: Long,
bounds: Bounds,
elementIds: Seq[Long]
)
3 changes: 3 additions & 0 deletions server/src/main/scala/kpn/core/doc/RouteDetailDoc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ case class RouteDetailDoc(
nodeRefs: Seq[Long], // networkNodeIds
elementIds: ElementIds,
edges: Seq[RouteEdge],
segments: Seq[RouteDetailSegment],
segmentElements: Seq[RouteDetailSegmentElement],
paths: Seq[RouteDetailPath],
) extends WithId {

def id: Long = summary.id
Expand Down
7 changes: 7 additions & 0 deletions server/src/main/scala/kpn/core/doc/RouteDetailPath.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kpn.core.doc

case class RouteDetailPath(
id: Long,
name: String, // forward, backward, start-tentacle-1, ...
elementIds: Seq[Long]
)
12 changes: 12 additions & 0 deletions server/src/main/scala/kpn/core/doc/RouteDetailSegment.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kpn.core.doc

import kpn.api.common.Bounds

case class RouteDetailSegment(
id: Long,
startNodeId: Long,
endNodeId: Long,
meters: Long,
bounds: Bounds,
elementIds: Seq[Long]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kpn.core.doc

case class RouteDetailSegmentElement(
segmentId: Long,
segmentElementId: Long,
surface: String,
coordinates: String
)
4 changes: 4 additions & 0 deletions server/src/main/scala/kpn/core/doc/RouteDoc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import kpn.api.base.WithId
import kpn.api.common.RouteSummary
import kpn.api.common.common.Ref
import kpn.api.common.route.RouteInfoAnalysis
import kpn.api.common.route.RoutePath
import kpn.api.common.route.RouteSegment
import kpn.api.custom.Day
import kpn.api.custom.Fact
import kpn.api.custom.Timestamp
Expand All @@ -20,6 +22,8 @@ case class RouteDoc(
facts: Seq[Fact],
oldFacts: Seq[Fact],
analysis: RouteInfoAnalysis,
segments: Seq[RouteSegment],
paths: Seq[RoutePath],
) extends WithId {

def id: Long = summary.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package kpn.server.analyzer.engine.analysis.route

import kpn.api.common.Bounds

case class RouteSegment(
case class OldRouteSegment(
id: Long,
startNodeId: Long,
endNodeId: Long,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package kpn.server.analyzer.engine.analysis.route

import kpn.api.common.Bounds
import kpn.api.common.RouteSummary
import kpn.api.common.data.Element
import kpn.api.common.data.Node
import kpn.api.common.data.Way
import kpn.api.common.route.RouteInfoAnalysis
import kpn.api.custom.Fact
import kpn.api.custom.RouteMemberInfo
import kpn.api.custom.Timestamp
import kpn.core.analysis.RouteMemberWay
import kpn.core.doc.RouteDetailDoc
import kpn.core.doc.RouteDetailPath
import kpn.core.doc.RouteDetailSegment
import kpn.core.doc.RouteDetailSegmentElement
import kpn.server.analyzer.engine.analysis.route.domain.RouteDetailAnalysisContext
import kpn.server.analyzer.engine.analysis.route.structure.StructurePath

class RouteDetailDocBuilder(context: RouteDetailAnalysisContext) {

Expand Down Expand Up @@ -115,7 +121,68 @@ class RouteDetailDocBuilder(context: RouteDetailAnalysisContext) {
context.tiles,
routeAnalysis.map.nodeIds,
context.elementIds,
context.edges
context.edges,
buildSegments,
Seq.empty, // TODO redesign
Seq.empty, // TODO redesign
)
}

private def buildSegments: Seq[RouteDetailSegment] = {
val ways = context.relation.wayMembers.map(_.way)
context.segments.map { segment =>
val segmentWayIds = segment.elements.flatMap(_.fragments).map(_.wayId)
val segmentWays = segmentWayIds.flatMap(wayId => ways.find(_.id == wayId))
val meters = segmentWays.map(_.length).sum
val segmentNodes = segmentWays.flatMap(_.nodes)
val bounds = Bounds.from(segmentNodes)
RouteDetailSegment(
segment.id,
segment.fromNodeId,
segment.toNodeId,
meters,
bounds,
segment.elements.map(_.id)
)
}
}

private def buildSegmentElements: Seq[RouteDetailSegmentElement] = {

val nodeMap: Map[Long, Node] = {
val nodeMemberNodes = context.relation.nodeMembers.map(_.node).toSet
val wayMemberNodes = context.relation.wayMembers.flatMap(_.way.nodes).toSet
val nodes = nodeMemberNodes ++ wayMemberNodes
nodes.map(node => node.id -> node)
}.toMap

context.segments.flatMap { segment =>
segment.elements.flatMap { element =>
element.fragmentGroups.map { fragmentGroup =>
val nodes = fragmentGroup.nodeIds.flatMap(nodeId => nodeMap.get(nodeId))
val coordinates = nodes.map(node => s"[${node.latitude},${node.latitude}]").mkString("[", ",", "]")
RouteDetailSegmentElement(
segment.id,
element.id,
fragmentGroup.surface,
coordinates
)
}
}
}
}

private def buildPaths: Seq[RouteDetailPath] = {
Seq(
context.newStructure.forwardPath.toSeq.map(path => toRouteDetailPath(path, "forward")),
context.newStructure.backwardPath.toSeq.map(path => toRouteDetailPath(path, "backward")),
context.newStructure.startTentaclePaths.zipWithIndex.map { case (path, index) => toRouteDetailPath(path, s"start-tentacle-${index + 1}") },
context.newStructure.endTentaclePaths.zipWithIndex.map { case (path, index) => toRouteDetailPath(path, s"end-tentacle-${index + 1}") },
context.newStructure.otherPaths.zipWithIndex.map { case (path, index) => toRouteDetailPath(path, s"other-${index + 1}") },
).flatten
}

private def toRouteDetailPath(path: StructurePath, name: String): RouteDetailPath = {
RouteDetailPath(path.id, name, path.elementIds)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kpn.server.analyzer.engine.analysis.route

import kpn.api.common.route.RoutePath
import kpn.api.common.route.RouteSegment
import kpn.core.doc.RouteDetailDoc
import kpn.core.doc.RouteDoc
import kpn.core.util.Log
Expand All @@ -25,6 +27,25 @@ class RouteMainAnalyzerImpl() extends RouteMainAnalyzer {
@tailrec
private def doAnalyze(analyzers: List[RouteDocAnalyzer], context: RouteDocAnalysisContext): Option[RouteDoc] = {
if (analyzers.isEmpty) {
val segments = context.routeDetailDoc.segments.map { segment =>
RouteSegment(
segment.id,
segment.startNodeId,
segment.endNodeId,
segment.meters,
segment.bounds,
segment.elementIds
)
}

val paths = context.routeDetailDoc.paths.map { path =>
RoutePath(
path.id,
path.name,
path.elementIds
)
}

Some(
RouteDoc(
context.routeDetailDoc._id, // routeId
Expand All @@ -37,7 +58,9 @@ class RouteMainAnalyzerImpl() extends RouteMainAnalyzer {
context.routeDetailDoc.lastSurvey,
context.routeDetailDoc.facts,
context.routeDetailDoc.oldFacts,
context.routeDetailDoc.analysis
context.routeDetailDoc.analysis,
segments,
paths
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import org.locationtech.jts.geom.LineString

case class RouteSegmentData(
id: Int,
segment: RouteSegment,
segment: OldRouteSegment,
lineStrings: Seq[LineString]
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package kpn.server.analyzer.engine.analysis.route.analyzers
import kpn.api.common.data.WayMember
import kpn.core.util.Haversine
import kpn.core.util.Log
import kpn.server.analyzer.engine.analysis.route.OldRouteSegment
import kpn.server.analyzer.engine.analysis.route.RouteNodeAnalysis
import kpn.server.analyzer.engine.analysis.route.RouteSegment
import kpn.server.analyzer.engine.analysis.route.RouteSegmentAnalysis
import kpn.server.analyzer.engine.analysis.route.RouteSegmentData
import kpn.server.analyzer.engine.analysis.route.domain.RouteDetailAnalysisContext
Expand Down Expand Up @@ -75,7 +75,7 @@ class OldRouteSegmentAnalyzer(routeNodeAnalysis: RouteNodeAnalysis) {

val geoJson = MonitorRouteAnalysisSupport.toGeoJson(geometryCollection)

val segment = RouteSegment(
val segment = OldRouteSegment(
id = index + 1,
startNodeId,
endNodeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ class RouteAnalysisBuilder(context: RouteDetailAnalysisContext) {
context.tiles,
routeAnalysis.map.nodeIds,
context.elementIds,
context.edges
context.edges,
Seq.empty, // TODO redesign
Seq.empty, // TODO redesign
Seq.empty, // TODO redesign
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ case class NewRouteSegmentElement(
toNetworkNode: Option[RouteNodeData],
fromNodeId: Long,
toNodeId: Long,
fragments: Seq[NewRouteSegmentElementFragment]
fragmentGroups: Seq[NewRouteSegmentElementFragmentGroup]
) {
def fragments: Seq[NewRouteSegmentElementFragment] = {
fragmentGroups.flatMap(_.fragments)
}

def nodeIds: Seq[Long] = {
fragments.headOption match {
case Some(firstLink) => firstLink.nodeIds ++ fragments.tail.flatMap(link => link.nodeIds.tail)
case Some(firstFragment) => firstFragment.nodeIds ++ fragments.tail.flatMap(_.nodeIds.tail)
case None => Seq.empty
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ case class NewRouteSegmentElementFragment(
wayId: Long,
link: Link,
role: Option[String],
surface: String,
nodeIds: Seq[Long]
) {
def fromNodeId: Long = nodeIds.head
Expand Down

This file was deleted.

Loading

0 comments on commit daf5f16

Please sign in to comment.