-
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 generate route relations and route states
- Loading branch information
Showing
13 changed files
with
392 additions
and
205 deletions.
There are no files selected for viewing
120 changes: 0 additions & 120 deletions
120
server/src/main/scala/kpn/core/tools/monitor/support/MonitorCreateRelationsTool.scala
This file was deleted.
Oops, something went wrong.
84 changes: 0 additions & 84 deletions
84
server/src/main/scala/kpn/core/tools/monitor/support/MonitorExploreRelationsTool.scala
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
server/src/main/scala/kpn/core/tools/next/database/NextDatabase.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,20 @@ | ||
package kpn.core.tools.next.database | ||
|
||
import kpn.core.tools.next.domain.NextRoute | ||
import kpn.core.tools.next.domain.NextRouteRelation | ||
import kpn.core.tools.next.domain.NextRouteState | ||
import kpn.database.base.DatabaseCollection | ||
import org.mongodb.scala.MongoCollection | ||
|
||
import scala.reflect.ClassTag | ||
|
||
trait NextDatabase { | ||
|
||
def getCollection[T: ClassTag](collectionName: String): MongoCollection[T] | ||
|
||
def routes: DatabaseCollection[NextRoute] | ||
|
||
def routeRelations: DatabaseCollection[NextRouteRelation] | ||
|
||
def routeStates: DatabaseCollection[NextRouteState] | ||
} |
30 changes: 30 additions & 0 deletions
30
server/src/main/scala/kpn/core/tools/next/database/NextDatabaseImpl.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,30 @@ | ||
package kpn.core.tools.next.database | ||
|
||
import kpn.core.tools.next.domain.NextRoute | ||
import kpn.core.tools.next.domain.NextRouteRelation | ||
import kpn.core.tools.next.domain.NextRouteState | ||
import kpn.database.base.DatabaseCollection | ||
import kpn.database.base.DatabaseCollectionImpl | ||
import org.mongodb.scala.MongoCollection | ||
import org.mongodb.scala.MongoDatabase | ||
|
||
import scala.reflect.ClassTag | ||
|
||
class NextDatabaseImpl(val database: MongoDatabase) extends NextDatabase { | ||
|
||
override def getCollection[T: ClassTag](collectionName: String): MongoCollection[T] = { | ||
database.getCollection[T](collectionName) | ||
} | ||
|
||
override def routes: DatabaseCollection[NextRoute] = { | ||
new DatabaseCollectionImpl(database.getCollection[NextRoute]("routes")) | ||
} | ||
|
||
override def routeRelations: DatabaseCollection[NextRouteRelation] = { | ||
new DatabaseCollectionImpl(database.getCollection[NextRouteRelation]("route-relations")) | ||
} | ||
|
||
override def routeStates: DatabaseCollection[NextRouteState] = { | ||
new DatabaseCollectionImpl(database.getCollection[NextRouteState]("route-states")) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
server/src/main/scala/kpn/core/tools/next/domain/NextRoute.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,3 @@ | ||
package kpn.core.tools.next.domain | ||
|
||
case class NextRoute() |
9 changes: 9 additions & 0 deletions
9
server/src/main/scala/kpn/core/tools/next/domain/NextRouteRelation.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.core.tools.next.domain | ||
|
||
import kpn.api.base.WithId | ||
import kpn.api.custom.Relation | ||
|
||
case class NextRouteRelation( | ||
_id: Long, | ||
relation: Relation | ||
) extends WithId |
10 changes: 10 additions & 0 deletions
10
server/src/main/scala/kpn/core/tools/next/domain/NextRouteState.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.core.tools.next.domain | ||
|
||
import kpn.api.base.WithId | ||
import kpn.server.analyzer.engine.context.ElementIds | ||
|
||
case class NextRouteState( | ||
_id: Long, // relationId | ||
tiles: Seq[String], | ||
elementIds: ElementIds, | ||
) extends WithId |
76 changes: 76 additions & 0 deletions
76
server/src/main/scala/kpn/core/tools/next/support/NextCreateRouteRelationsTool.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,76 @@ | ||
package kpn.core.tools.next.support | ||
|
||
import kpn.core.data.Data | ||
import kpn.core.loadOld.Parser | ||
import kpn.core.overpass.OverpassQueryExecutor | ||
import kpn.core.overpass.OverpassQueryExecutorRemoteImpl | ||
import kpn.core.tools.next.database.NextDatabase | ||
import kpn.core.tools.next.database.NextDatabaseImpl | ||
import kpn.core.tools.next.domain.NextRouteRelation | ||
import kpn.core.util.Log | ||
import kpn.database.util.Mongo.codecRegistry | ||
import kpn.server.monitor.route.update.RelationTopLevelDataBuilder | ||
import org.apache.commons.io.FileUtils | ||
import org.mongodb.scala.MongoClient | ||
|
||
import java.io.File | ||
import scala.xml.XML | ||
|
||
object NextCreateRouteRelationsTool { | ||
def main(args: Array[String]): Unit = { | ||
val client = MongoClient("mongodb://localhost:27017") | ||
try { | ||
val mongoDatabase = client.getDatabase("kpn-next").withCodecRegistry(codecRegistry) | ||
val database = new NextDatabaseImpl(mongoDatabase) | ||
val overpassQueryExecutor = new OverpassQueryExecutorRemoteImpl() | ||
val tool = new NextCreateRouteRelationsTool(database, overpassQueryExecutor) | ||
tool.createMonitorRelations() | ||
} finally { | ||
client.close() | ||
} | ||
} | ||
} | ||
|
||
class NextCreateRouteRelationsTool(database: NextDatabase, overpassQueryExecutor: OverpassQueryExecutor) { | ||
|
||
private val log = Log(classOf[NextCreateRouteRelationsTool]) | ||
private val batchSize = 100 | ||
|
||
def createMonitorRelations(): Unit = { | ||
val routeIds = collectRouteIds() | ||
log.info(s"collected ${routeIds.size} route ids") | ||
routeIds.sliding(batchSize, batchSize).zipWithIndex.foreach { case (batchRouteIds, index) => | ||
log.info(s"${index * batchSize}/${routeIds.size}") | ||
createMonitorRelations(batchRouteIds) | ||
} | ||
} | ||
|
||
private def collectRouteIds(): Seq[Long] = { | ||
val allRouteIds = FileUtils.readFileToString(new File("/kpn/next/route-ids.txt"), "UTF-8").split("\n").map(_.toLong) | ||
val loadedRouteIds = database.routeRelations.stringIds().map(_.toLong) | ||
(allRouteIds.toSet -- loadedRouteIds.toSet).toSeq.sorted | ||
} | ||
|
||
private def createMonitorRelations(routeRelationIds: Seq[Long]): Unit = { | ||
val data = monitorRelationData(routeRelationIds) | ||
routeRelationIds.foreach { routeRelationId => | ||
data.relations.get(routeRelationId) match { | ||
case Some(relation) => | ||
database.routeRelations.save(NextRouteRelation(routeRelationId, relation)) | ||
|
||
case None => | ||
log.error(s"could note read routeId $routeRelationId") | ||
None | ||
} | ||
} | ||
} | ||
|
||
private def monitorRelationData(relationIds: Seq[Long]): Data = { | ||
val relations = relationIds.map(id => s"relation($id);").mkString | ||
val query = s"($relations);(._;>;);out meta;" | ||
val xmlString = overpassQueryExecutor.execute(query) | ||
val xml = XML.loadString(xmlString) | ||
val rawData = new Parser().parse(xml.head) | ||
new RelationTopLevelDataBuilder(rawData, relationIds).data | ||
} | ||
} |
Oops, something went wrong.