Skip to content

Commit

Permalink
#368 migrate location analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarc committed Jul 17, 2024
1 parent 6bf6a65 commit 4e03422
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ import { RouteDetailsPageService } from './route-details-page.service';
RouteStartNodesComponent,
RouteSummaryComponent,
RouterLink,
SymbolComponent,
TagTableComponent,
TimestampComponent,
SymbolComponent,
],
})
export class RouteDetailsPageComponent implements OnInit {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package kpn.server.analyzer.engine.analysis.route.structure

import kpn.api.common.data.Node
import kpn.server.analyzer.engine.analysis.route.structure.RouteAnalysisFragmentGroup.geometryFactory
import org.locationtech.jts.geom.Coordinate
import org.locationtech.jts.geom.GeometryFactory
import org.locationtech.jts.geom.LineString

object RouteAnalysisFragmentGroup {
private val geometryFactory = new GeometryFactory

def apply(surface: String, fragments: Seq[RouteAnalysisFragment]): RouteAnalysisFragmentGroup = {
val nodes: Seq[Node] = fragments.head.nodes ++ fragments.tail.flatMap(_.nodes.tail)
val coordinates = nodes.map(node => new Coordinate(node.lon, node.lat)).toArray
val nodeIds: Seq[Long] = nodes.map(_.id)
val lineString = geometryFactory.createLineString(coordinates)
RouteAnalysisFragmentGroup(surface, fragments, nodeIds, lineString)
}
}

case class RouteAnalysisFragmentGroup(
surface: String,
fragments: Seq[RouteAnalysisFragment],
nodeIds: Seq[Long],
lineString: LineString
)
) {
def nodes: Seq[Node] = fragments.head.nodes ++ fragments.tail.flatMap(_.nodes.tail)

def nodeIds: Seq[Long] = nodes.map(_.id)

def lineString: LineString = {
val coordinates = nodes.map(node => new Coordinate(node.lon, node.lat)).toArray
geometryFactory.createLineString(coordinates)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ class RouteLocatorImpl(locationAnalyzer: LocationAnalyzer) extends RouteLocator

def locate(segments: Seq[RouteAnalysisSegment]): RouteLocationAnalysis = {

val routeGeometries: Seq[Geometry] = toGeometries(segments)

val candidates: Seq[LocationSelector] = locationAnalyzer.locateGeometries(routeGeometries)
val geometries = toGeometries(segments)
val candidates = locationAnalyzer.locateGeometries(geometries)

val locationSelectorCandidates = candidates.map { candidate =>
val distance = calculateDistance(routeGeometries, candidate)
val distance = calculateDistance(geometries, candidate)
LocationSelectorCandidate(candidate, distance)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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
Expand Down Expand Up @@ -144,19 +143,10 @@ class RouteDetailDocBuilder(context: RouteDetailAnalysisContext) {
}

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.longitude},${node.latitude}]").mkString("[", ",", "]")
val coordinates = fragmentGroup.nodes.map(node => s"[${node.longitude},${node.latitude}]").mkString("[", ",", "]")
RouteDetailSegmentElement(
segment.id,
element.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,9 @@ import kpn.api.common.data.Node
import kpn.api.common.data.Way
import kpn.core.analysis.Link

object RouteAnalysisFragment {
def apply(
id: Long,
way: Way,
link: Link,
role: Option[String],
surface: String,
nodeIds: Seq[Long]
): RouteAnalysisFragment = {
val nodes = if (nodeIds.size == way.nodes.size) {
way.nodes
} else {
nodeIds.flatMap { nodeId => way.nodes.find(_.id == nodeId) }
}
RouteAnalysisFragment(
id,
way,
nodes,
link,
role,
surface,
nodeIds
)
}
}

case class RouteAnalysisFragment(
id: Long,
way: Way,
nodes: Seq[Node],
link: Link,
role: Option[String],
surface: String,
Expand All @@ -42,4 +15,8 @@ case class RouteAnalysisFragment(
def fromNodeId: Long = nodeIds.head

def toNodeId: Long = nodeIds.last

def nodes: Seq[Node] = {
nodeIds.flatMap(nodeId => way.nodes.find(_.id == nodeId))
}
}

0 comments on commit 4e03422

Please sign in to comment.