Skip to content

Commit

Permalink
avoid createResultTags for exactRoadType; indexOf is also a speed up …
Browse files Browse the repository at this point in the history
…but not the root; deploy to local maven repo
  • Loading branch information
karussell committed May 21, 2023
1 parent 2472584 commit b46b124
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 52 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

plugins {
kotlin("multiplatform") version "1.7.10" apply false
"maven"
}

allprojects {

group = "de.westnordost"
version = "1.2"
version = "1.3-SNAPSHOT"

repositories {
mavenCentral()
mavenLocal()
}
}
Empty file modified gradlew
100644 → 100755
Empty file.
15 changes: 0 additions & 15 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
kotlin("multiplatform")
id("maven-publish")
id("signing")
id("org.jetbrains.dokka") version "1.7.10"
}

Expand All @@ -11,12 +10,6 @@ kotlin {
kotlinOptions.jvmTarget = "1.8"
}
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
js(BOTH) {
browser()
}
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
Expand All @@ -30,11 +23,6 @@ kotlin {

sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
}
}

Expand Down Expand Up @@ -90,6 +78,3 @@ publishing {
}
}

signing {
sign(publishing.publications)
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ class LegalDefaultSpeeds(
// 1. Try to match tags first
val exactRoadType = findRoadTypeByTags(roadTypes, tags, relationsTags, false, replacerFn)
if (exactRoadType != null) {
return Result(exactRoadType.name, createResultTags(tags, exactRoadType.tags), Certitude.Exact)
val result = exactRoadType.tags.toMutableMap()
result.putAll(tags.filter { !it.isImplicitMaxSpeed })
return Result(exactRoadType.name, result, Certitude.Exact)
}
// if (exactRoadType != null) {
// return Result(exactRoadType.name, createResultTags(tags, exactRoadType.tags), Certitude.Exact)
// }

// 2. If a `maxspeed` is set, try to reverse-search by maxspeed
val maxSpeedRoadType = findRoadTypeByMaxSpeed(roadTypes, tags)
if (maxSpeedRoadType != null) {
Expand Down Expand Up @@ -222,38 +228,33 @@ private fun createResultTags(tags: Map<String, String>, roadTypeTags: Map<String
private val Map.Entry<String, String>.isImplicitMaxSpeed get() =
key == "maxspeed" && value.withOptionalUnitToDoubleOrNull() == null

private val cache = mutableMapOf<String, Regex>()
private val cacheConditional = mutableMapOf<String, Regex>()

private fun MutableMap<String,String>.limitSpeedsTo(key: String, maxspeed: Double?) {
if (maxspeed != null) {
val iter = entries.iterator()
var conditionalRegex = cacheConditional[key]
if (conditionalRegex == null) {
conditionalRegex = Regex("$key:.*conditional")
cacheConditional[key] = conditionalRegex
}
while (iter.hasNext()) {
val entry = iter.next()
/* search & remove through conditionals strings. E.g. if maxspeed=60, turn
maxspeed:hgv:conditional=80 @ (trailer); 40 @ (weight>30t) into
maxspeed:hgv:conditional=40 @ (weight>30t) or delete if no conditionals are left
after removing those that are higher */
if (entry.key.matches(conditionalRegex)) {
val conditionals = entry.value.split("; ").toMutableList()
conditionals.removeAll {
val speed = it.split(" @ ")[0].withOptionalUnitToDoubleOrNull() ?: return@removeAll false
speed >= maxspeed
}
val newConditional = conditionals.joinToString("; ")
if (newConditional.isEmpty()) {
iter.remove()
} else {
entry.setValue(newConditional)
val idx1 = entry.key.indexOf(key + ":")
if (idx1 == 0) {
val idx2 = entry.key.indexOf(":conditional")
if (idx2 > 0) {
/* search & remove through conditionals strings. E.g. if maxspeed=60, turn
maxspeed:hgv:conditional=80 @ (trailer); 40 @ (weight>30t) into
maxspeed:hgv:conditional=40 @ (weight>30t) or delete if no conditionals are left
after removing those that are higher */
val conditionals = entry.value.split("; ").toMutableList()
conditionals.removeAll {
val speed = it.split(" @ ")[0].withOptionalUnitToDoubleOrNull() ?: return@removeAll false
speed >= maxspeed
}
val newConditional = conditionals.joinToString("; ")
if (newConditional.isEmpty()) {
iter.remove()
} else {
entry.setValue(newConditional)
}
}
}
/* remove higher speeds. E.g. if maxspeed=60, remove maxspeed:hgv=80 */
else if(entry.key.startsWith("$key:")) {

/* remove higher speeds. E.g. if maxspeed=60, remove maxspeed:hgv=80 */
val speed = entry.value.withOptionalUnitToDoubleOrNull()
if (speed != null && speed >= maxspeed) {
iter.remove()
Expand All @@ -263,14 +264,10 @@ private fun MutableMap<String,String>.limitSpeedsTo(key: String, maxspeed: Doubl
}
/* recurse down. The same should be done for e.g. maxspeed:hgv:conditional if maxspeed:hgv
* already has a lower speed limit etc. */
var r = cache[key]
if (r == null) {
r = Regex("$key:[a-z_]+")
cache[key] = r
}

val subkeys = keys.filter { r.matches(it) }
for (subkey in subkeys) {
for (key in keys) {
val index = key.indexOf(key + ":")
if(index < 0) continue
val subkey = key.substring(index)
val subMaxspeed = this[subkey]?.withOptionalUnitToDoubleOrNull()
limitSpeedsTo(subkey, listOfNotNull(maxspeed, subMaxspeed).minOrNull())
}
Expand Down
1 change: 0 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
rootProject.name = "osm-legal-default-speeds"

include(":library")
include(":demo")

0 comments on commit b46b124

Please sign in to comment.