-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#369 monitor vector tiles - proof-of-concept wip
- Loading branch information
Showing
20 changed files
with
236 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
server/src/main/scala/kpn/core/tools/monitor/support/MonitorCreateRelationsTool.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package kpn.core.tools.monitor.support | ||
|
||
import kpn.core.loadOld.Parser | ||
import kpn.core.overpass.OverpassQueryExecutor | ||
import kpn.core.overpass.OverpassQueryExecutorRemoteImpl | ||
import kpn.core.overpass.QueryRelationTopLevel | ||
import kpn.database.util.Mongo | ||
import kpn.server.analyzer.engine.context.ElementIds | ||
import kpn.server.analyzer.engine.tile.LinesTileCalculator | ||
import kpn.server.analyzer.engine.tile.LinesTileCalculatorImpl | ||
import kpn.server.analyzer.engine.tile.TileCalculatorImpl | ||
import kpn.server.analyzer.engine.tiles.domain.Line | ||
import kpn.server.analyzer.engine.tiles.domain.Point | ||
import kpn.server.monitor.repository.MonitorGroupRepository | ||
import kpn.server.monitor.repository.MonitorGroupRepositoryImpl | ||
import kpn.server.monitor.repository.MonitorRouteRepository | ||
import kpn.server.monitor.repository.MonitorRouteRepositoryImpl | ||
import kpn.server.monitor.MonitorUtil | ||
import kpn.server.monitor.domain.MonitorRelation | ||
import kpn.server.monitor.repository.MonitorRelationRepository | ||
import kpn.server.monitor.repository.MonitorRelationRepositoryImpl | ||
import kpn.server.monitor.route.update.RelationTopLevelDataBuilder | ||
|
||
import scala.xml.XML | ||
|
||
object MonitorCreateRelationsTool { | ||
def main(args: Array[String]): Unit = { | ||
Mongo.executeIn("kpn-monitor") { database => | ||
val groupRepository = new MonitorGroupRepositoryImpl(database) | ||
val routeRepository = new MonitorRouteRepositoryImpl(database) | ||
val relationRepository = new MonitorRelationRepositoryImpl(database) | ||
val overpassQueryExecutor = new OverpassQueryExecutorRemoteImpl() | ||
val linesTileCalculator = new LinesTileCalculatorImpl(new TileCalculatorImpl()) | ||
val tool = new MonitorCreateRelationsTool( | ||
groupRepository, | ||
routeRepository, | ||
relationRepository, | ||
overpassQueryExecutor, | ||
linesTileCalculator | ||
) | ||
tool.createMonitorRelations() | ||
} | ||
} | ||
} | ||
|
||
class MonitorCreateRelationsTool( | ||
groupRepository: MonitorGroupRepository, | ||
routeRepository: MonitorRouteRepository, | ||
relationRepository: MonitorRelationRepository, | ||
overpassQueryExecutor: OverpassQueryExecutor, | ||
linesTileCalculator: LinesTileCalculator | ||
) { | ||
|
||
def createMonitorRelations(): Unit = { | ||
val relationIds = collectionRelationIds() | ||
println(s"collected ${relationIds.size} relation ids") | ||
relationIds.zipWithIndex.foreach { case (relationId, index) => | ||
println(s"${index + 1}/${relationIds.size}") | ||
val monitorRelation = createMonitorRelation(relationId) | ||
relationRepository.save(monitorRelation) | ||
} | ||
} | ||
|
||
private def collectionRelationIds(): Seq[Long] = { | ||
val ids = groupRepository.groups().sortBy(_.name) flatMap { group => | ||
println(s"group ${group.name}") | ||
groupRepository.groupRoutes(group._id).sortBy(_.name).flatMap { route => | ||
println(s" route ${route.name}") | ||
route.relation.toSeq.map(_.relationId) ++ | ||
MonitorUtil.subRelationsIn(route).map(_.relationId) | ||
} | ||
} | ||
ids.distinct.sorted | ||
} | ||
|
||
private def createMonitorRelation(relationId: Long): MonitorRelation = { | ||
val xmlString = overpassQueryExecutor.executeQuery(None, QueryRelationTopLevel(relationId)) | ||
val xml = XML.loadString(xmlString) | ||
val rawData = new Parser().parse(xml.head) | ||
val data = new RelationTopLevelDataBuilder(rawData, relationId).data | ||
val nodes = data.nodes.values.toSeq | ||
val ways = data.ways.values.toSeq | ||
val nodeIds = nodes.map(_.id).toSet | ||
val wayIds = ways.map(_.id).toSet | ||
val subRelationIds = data.relations.values.flatMap(_.raw.relationMembers.map(_.ref)).toSet | ||
val elementIds = ElementIds(nodeIds, wayIds, subRelationIds) | ||
val tiles = ways.flatMap { way => | ||
val lines = way.nodes.sliding(2).map { case Seq(node1, node2) => | ||
Line(Point(node1.lon, node1.lat), Point(node2.lon, node2.lat)) | ||
}.toSeq | ||
(2 to 14).flatMap { z => | ||
linesTileCalculator.tiles(z, lines) | ||
} | ||
}.distinct.sortBy(tile => (tile.z, tile.x, tile.y)) | ||
|
||
MonitorRelation( | ||
relationId, | ||
tiles.map(_.name), | ||
elementIds | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
server/src/main/scala/kpn/server/analyzer/engine/tile/LinesTileCalculator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package kpn.server.analyzer.engine.tile | ||
|
||
import kpn.server.analyzer.engine.tiles.domain.Line | ||
import kpn.server.analyzer.engine.tiles.domain.Tile | ||
|
||
trait LinesTileCalculator { | ||
|
||
def tiles(z: Int, lines: Seq[Line]): Seq[Tile] | ||
} |
51 changes: 51 additions & 0 deletions
51
server/src/main/scala/kpn/server/analyzer/engine/tile/LinesTileCalculatorImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package kpn.server.analyzer.engine.tile | ||
|
||
import kpn.server.analyzer.engine.tiles.domain.Line | ||
import kpn.server.analyzer.engine.tiles.domain.Point | ||
import kpn.server.analyzer.engine.tiles.domain.Tile | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class LinesTileCalculatorImpl(tileCalculator: TileCalculator) extends LinesTileCalculator { | ||
|
||
override def tiles(z: Int, lines: Seq[Line]): Seq[Tile] = { | ||
|
||
val tileQueue = scala.collection.mutable.Queue[Tile]() | ||
val foundTiles = scala.collection.mutable.Set[Tile]() | ||
|
||
val tiles = lines.flatMap(_.points).map { p: Point => | ||
tileCalculator.tileLonLat(z, p.x, p.y) | ||
}.toSet | ||
|
||
foundTiles ++= tiles.toSeq | ||
tileQueue ++= tiles | ||
|
||
while (tileQueue.nonEmpty) { | ||
val tile = tileQueue.dequeue() | ||
explore(tileQueue, foundTiles, lines, tile, tile.bounds.left, -1, 0) | ||
explore(tileQueue, foundTiles, lines, tile, tile.bounds.right, 1, 0) | ||
explore(tileQueue, foundTiles, lines, tile, tile.bounds.top, 0, 1) | ||
explore(tileQueue, foundTiles, lines, tile, tile.bounds.bottom, 0, -1) | ||
} | ||
foundTiles.toSeq | ||
} | ||
|
||
private def explore( | ||
tileQueue: scala.collection.mutable.Queue[Tile], | ||
foundTiles: scala.collection.mutable.Set[Tile], | ||
lines: Seq[Line], | ||
tile: Tile, | ||
side: Line, | ||
xDelta: Int, | ||
yDelta: Int | ||
): Unit = { | ||
val adjecentTile = tileCalculator.tileXY(tile.z, tile.x + xDelta, tile.y + yDelta) | ||
if (!foundTiles.map(_.name).contains(adjecentTile.name)) { | ||
if (lines.exists(_.intersects(side))) { | ||
tileQueue += adjecentTile | ||
foundTiles += adjecentTile | ||
() | ||
} | ||
} | ||
} | ||
} |
44 changes: 2 additions & 42 deletions
44
server/src/main/scala/kpn/server/analyzer/engine/tile/RouteTileCalculatorImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,14 @@ | ||
package kpn.server.analyzer.engine.tile | ||
|
||
import kpn.server.analyzer.engine.tiles.domain.Line | ||
import kpn.server.analyzer.engine.tiles.domain.Point | ||
import kpn.server.analyzer.engine.tiles.domain.Tile | ||
import kpn.server.analyzer.engine.tiles.domain.RouteTileSegment | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class RouteTileCalculatorImpl(tileCalculator: TileCalculator) extends RouteTileCalculator { | ||
class RouteTileCalculatorImpl(linesTileCalculator: LinesTileCalculator) extends RouteTileCalculator { | ||
|
||
override def tiles(z: Int, segments: Seq[RouteTileSegment]): Seq[Tile] = { | ||
|
||
val tileQueue = scala.collection.mutable.Queue[Tile]() | ||
val foundTiles = scala.collection.mutable.Set[Tile]() | ||
|
||
val lines = segments.flatMap(_.lines) | ||
|
||
val tiles = lines.flatMap(_.points).map { p: Point => | ||
tileCalculator.tileLonLat(z, p.x, p.y) | ||
}.toSet | ||
|
||
foundTiles ++= tiles.toSeq | ||
tileQueue ++= tiles | ||
|
||
while (tileQueue.nonEmpty) { | ||
val tile = tileQueue.dequeue() | ||
explore(tileQueue, foundTiles, lines, tile, tile.bounds.left, -1, 0) | ||
explore(tileQueue, foundTiles, lines, tile, tile.bounds.right, 1, 0) | ||
explore(tileQueue, foundTiles, lines, tile, tile.bounds.top, 0, 1) | ||
explore(tileQueue, foundTiles, lines, tile, tile.bounds.bottom, 0, -1) | ||
} | ||
foundTiles.toSeq | ||
} | ||
|
||
private def explore( | ||
tileQueue: scala.collection.mutable.Queue[Tile], | ||
foundTiles: scala.collection.mutable.Set[Tile], | ||
lines: Seq[Line], | ||
tile: Tile, | ||
side: Line, | ||
xDelta: Int, | ||
yDelta: Int | ||
): Unit = { | ||
val adjecentTile = tileCalculator.tileXY(tile.z, tile.x + xDelta, tile.y + yDelta) | ||
if (!foundTiles.map(_.name).contains(adjecentTile.name)) { | ||
if (lines.exists(_.intersects(side))) { | ||
tileQueue += adjecentTile | ||
foundTiles += adjecentTile | ||
() | ||
} | ||
} | ||
linesTileCalculator.tiles(z, lines) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
server/src/main/scala/kpn/server/monitor/domain/MonitorRelation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package kpn.server.monitor.domain | ||
|
||
import kpn.api.base.WithId | ||
import kpn.server.analyzer.engine.context.ElementIds | ||
|
||
case class MonitorRelation( | ||
_id: Long, // relationId | ||
tiles: Seq[String], | ||
elementIds: ElementIds, | ||
) extends WithId |
8 changes: 8 additions & 0 deletions
8
server/src/main/scala/kpn/server/monitor/repository/MonitorRelationRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package kpn.server.monitor.repository | ||
|
||
import kpn.server.monitor.domain.MonitorRelation | ||
|
||
trait MonitorRelationRepository { | ||
|
||
def save(monitorRelation: MonitorRelation): Unit | ||
} |
14 changes: 14 additions & 0 deletions
14
server/src/main/scala/kpn/server/monitor/repository/MonitorRelationRepositoryImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package kpn.server.monitor.repository | ||
|
||
import kpn.core.util.Log | ||
import kpn.database.base.Database | ||
import kpn.server.monitor.domain.MonitorRelation | ||
|
||
class MonitorRelationRepositoryImpl(database: Database) extends MonitorRelationRepository { | ||
|
||
private val log = Log(classOf[MonitorRelationRepositoryImpl]) | ||
|
||
override def save(monitorRelation: MonitorRelation): Unit = { | ||
database.monitorRelations.save(monitorRelation, log) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.