Skip to content

Commit

Permalink
#368 migrate edge analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarc committed Jul 19, 2024
1 parent 563385b commit 91160ad
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import kpn.core.doc.OldRouteDoc
import kpn.core.doc.RouteDetailDoc
import kpn.core.tools.analysis.AnalysisStartConfiguration
import kpn.core.tools.analysis.AnalysisStartToolOptions
import kpn.core.tools.config.Dirs
import kpn.core.tools.next.domain.RouteRelation
import kpn.core.tools.next.support.compare.CompareEdges
import kpn.core.util.Log
import kpn.server.analyzer.engine.analysis.route.RouteDetailDocBuilder
import org.apache.commons.io.FileUtils

import java.io.File

object RouteAnalysisCompareTool {
def main(args: Array[String]): Unit = {
Expand All @@ -23,7 +27,9 @@ class RouteAnalysisCompareTool(config: AnalysisStartConfiguration) {

def analyze(): Unit = {
log.info("Collecting routeIds")
val routeIds = config.oldDatabase.oldRoutes.ids()
val file = new File(Dirs.root, "logs/mismatch-ids.txt")
val routeIds = FileUtils.readFileToString(file, "UTF-8").split("\n").map(_.toLong)
// val routeIds = Seq(17700250L) // config.oldDatabase.oldRoutes.ids()
log.info(s"Comparing ${routeIds.size} routes")
routeIds.zipWithIndex.foreach { case (routeId, index) =>
if (index % 50 == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ object RouteAnalysisTool {

class RouteAnalysisTool(config: AnalysisStartConfiguration) {

import kpn.core.tools.next.support.RouteAnalysisTool.*

private val log = Log(classOf[RouteAnalysisTool])

def analyze(): Unit = {
Expand All @@ -70,7 +68,9 @@ class RouteAnalysisTool(config: AnalysisStartConfiguration) {
// analyzeRoutes(Seq(5491)) // ok route with 2 start tenticles

// analyzeRoutes(essenOkRouteIds)
analyzeRoutes(law9)
// analyzeRoutes(law9)
analyzeRoutes(Seq(13844575L))
analyzeRoutes(Seq(17700250L)) // exception during structure analysis
// analyzeRoutes(Seq(3952592)) // broken route
// analyzeRoutes(Seq(3963819)) // route with roundabout
buildTiles()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ object EdgeRouteAnalyzer extends RouteAnalyzer {
class EdgeRouteAnalyzer(context: RouteDetailAnalysisContext) {

def analyze: RouteDetailAnalysisContext = {
val edges = context.structure.nodeNetworkPaths.map(toEdge)
val edges = if (context.nodeNetwork) {
context.structure.nodeNetworkPaths.map(toEdge)
}
else {
Seq.empty
}
context.copy(edges = edges)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kpn.server.analyzer.engine.analysis.route.analyzers

import kpn.core.tools.config.Dirs
import kpn.server.analyzer.engine.analysis.route.domain.RouteDetailAnalysisContext
import kpn.server.analyzer.engine.analysis.route.report.RouteEdgeReport
import kpn.server.analyzer.engine.analysis.route.report.RouteFactsReport
import kpn.server.analyzer.engine.analysis.route.report.RouteLinksReport
import kpn.server.analyzer.engine.analysis.route.report.RouteNameAnalysisReport
Expand Down Expand Up @@ -38,6 +39,7 @@ class RouteAnalysisContextReport(context: RouteDetailAnalysisContext) {
|${RouteLinksReport.report(context)}
|${RouteSegmentReport.report(context)}
|${RoutePathReport.report(context)}
|${RouteEdgeReport.report(context.edges)}
|</body>
|</html>
|""".stripMargin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package kpn.server.analyzer.engine.analysis.route.analyzers

import kpn.api.custom.Fact
import kpn.api.custom.Fact.RouteAnalysisFailed
import kpn.api.custom.Fact.RouteNodeMissingInWays
import kpn.api.custom.Fact.RouteNotBackward
import kpn.api.custom.Fact.RouteNotContinious
import kpn.api.custom.Fact.RouteNotForward
import kpn.api.custom.Fact.RouteNotOneWay
import kpn.api.custom.Fact.RouteOneWay
import kpn.api.custom.Fact.RouteUnusedSegments
import kpn.api.custom.Fact.RouteWithoutNodes
import kpn.server.analyzer.engine.analysis.route.domain.RouteDetailAnalysisContext
import kpn.server.analyzer.engine.analysis.route.structure.StructureAnalyzer

Expand All @@ -28,55 +30,58 @@ class RouteStructureAnalyzer(context: RouteDetailAnalysisContext) {

val structure = new StructureAnalyzer(context).analyze()

val oneWayRouteForward = context.relation.hasTag("direction", "forward")
val oneWayRouteBackward = context.relation.hasTag("direction", "backward")
if (!Seq(RouteAnalysisFailed, RouteWithoutNodes, RouteNodeMissingInWays).exists(context.facts.contains)) {

val oneWayRoute = context.relation.hasTag("oneway", "yes") || context.relation.hasTag("signed_direction", "yes")
val oneWayRouteForward = context.relation.hasTag("direction", "forward")
val oneWayRouteBackward = context.relation.hasTag("direction", "backward")

val hasValidForwardPath = structure.forwardPath.isDefined // TODO redesign && !structure.forwardPath.exists(_.broken)
val hasValidBackwardPath = structure.backwardPath.isDefined // TODO redesign && !structure.backwardPath.exists(_.broken)
val oneWayRoute = context.relation.hasTag("oneway", "yes") || context.relation.hasTag("signed_direction", "yes")

if (hasValidForwardPath) {
if (hasValidBackwardPath) {
if (oneWayRoute || oneWayRouteForward || oneWayRouteBackward) {
facts += RouteNotOneWay
val hasValidForwardPath = structure.forwardPath.isDefined // TODO redesign && !structure.forwardPath.exists(_.broken)
val hasValidBackwardPath = structure.backwardPath.isDefined // TODO redesign && !structure.backwardPath.exists(_.broken)

if (hasValidForwardPath) {
if (hasValidBackwardPath) {
if (oneWayRoute || oneWayRouteForward || oneWayRouteBackward) {
facts += RouteNotOneWay
}
}
else {
if (oneWayRoute || oneWayRouteForward) {
facts += RouteOneWay
}
else {
facts += RouteNotBackward
}
}
}
else {
if (oneWayRoute || oneWayRouteForward) {
else if (hasValidBackwardPath) {
if (oneWayRoute || oneWayRouteBackward) {
facts += RouteOneWay
}
else {
facts += RouteNotBackward
facts += RouteNotForward
}
}
}
else if (hasValidBackwardPath) {
if (oneWayRoute || oneWayRouteBackward) {
facts += RouteOneWay
}
else {
if (oneWayRoute || oneWayRouteForward || oneWayRouteBackward) {
facts += RouteNotOneWay
}
facts += RouteNotForward
facts += RouteNotBackward
}
}
else {
if (oneWayRoute || oneWayRouteForward || oneWayRouteBackward) {
facts += RouteNotOneWay
}
facts += RouteNotForward
facts += RouteNotBackward
}

if (!Seq(RouteNodeMissingInWays, RouteOneWay).exists(facts.contains)) {
if (structure.forwardPath.isEmpty || /* segmentAnalysis.structure.forwardPath.get.broken ||*/
structure.backwardPath.isEmpty /*|| segmentAnalysis.structure.backwardPath.get.broken*/ ) {
facts += RouteNotContinious
if (!Seq(RouteNodeMissingInWays, RouteOneWay).exists(facts.contains)) {
if (structure.forwardPath.isEmpty || /* segmentAnalysis.structure.forwardPath.get.broken ||*/
structure.backwardPath.isEmpty /*|| segmentAnalysis.structure.backwardPath.get.broken*/ ) {
facts += RouteNotContinious
}
}
}

if (!Seq(RouteNotForward, RouteNotBackward).exists(facts.contains)) {
if (structure.otherPaths.nonEmpty) {
facts += RouteUnusedSegments
if (!Seq(RouteNotForward, RouteNotBackward).exists(facts.contains)) {
if (structure.otherPaths.nonEmpty) {
facts += RouteUnusedSegments
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package kpn.server.analyzer.engine.analysis.route.report

import kpn.api.common.route.RouteEdge
import kpn.server.analyzer.engine.analysis.route.report.ReportUtil.osmNodeLink

object RouteEdgeReport {

def report(edges: Seq[RouteEdge]): String = {
if (edges.nonEmpty) {
edgesTable(edges)
}
else {
noEdges()
}
}

private def edgesTable(edges: Seq[RouteEdge]): String = {
s"""
|<table>
| <tr class="header">
| <td>id</td>
| <td>source</td>
| <td>sink</td>
| <td>meters</td>
| </tr>
| ${edges.map(edgeString).mkString("\n")}
|</table>
|""".stripMargin
}

private def edgeString(edge: RouteEdge): String = {
s"""
|<tr>
| <td>edge-${edge.pathId}</td>
| <td>${osmNodeLink(edge.sourceNodeId)}</td>
| <td>${osmNodeLink(edge.sinkNodeId)}</td>
| <td>${edge.meters}</td>
|</tr>
|""".stripMargin
}

def noEdges(): String = {
s"""
|<table>
| <tr>
| <td>No node network edges</td>
| </tr>
|</table>
|""".stripMargin
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,18 @@ class StructureAnalyzer(context: RouteDetailAnalysisContext, traceEnabled: Boole
private val pathIds = Util.ids

def analyze(): Structure = {
if (context.nodes.nodes.isEmpty) {
analyzeNonNodeNetworkRoute()
if (context.nodeNetwork) {
analyzeNodeNetworkRoute()
}
else {
analyzeNodeNetworkRoute()
analyzeNonNodeNetworkRoute()
}
}

private def analyzeNodeNetworkRoute(): Structure = {

if (context.segments.size > 1) {
val otherElements = context.segments.flatMap(_.elements).map { element =>
StructurePath(
pathIds.next(),
element.fromNodeId,
element.toNodeId,
Seq(
StructurePathElement(
element,
reversed = false
)
)
)
}

Structure(
forwardPath = None,
backwardPath = None,
startTentaclePaths = Seq.empty,
endTentaclePaths = Seq.empty,
otherElements,
)
otherElementsStructure()
}
else {
val structureOption = context.nodes.startNode match {
Expand All @@ -56,7 +36,7 @@ class StructureAnalyzer(context: RouteDetailAnalysisContext, traceEnabled: Boole
)
}
}
structureOption.get // TODO redesign
structureOption.getOrElse(otherElementsStructure())
}
}

Expand Down Expand Up @@ -448,4 +428,27 @@ class StructureAnalyzer(context: RouteDetailAnalysisContext, traceEnabled: Boole
}
}
}

private def otherElementsStructure(): Structure = {
val otherElements = context.segments.flatMap(_.elements).map { element =>
StructurePath(
pathIds.next(),
element.fromNodeId,
element.toNodeId,
Seq(
StructurePathElement(
element,
reversed = false
)
)
)
}
Structure(
forwardPath = None,
backwardPath = None,
startTentaclePaths = Seq.empty,
endTentaclePaths = Seq.empty,
otherElements,
)
}
}

0 comments on commit 91160ad

Please sign in to comment.