Skip to content

Commit

Permalink
don't require from user to add surface note if they selected a generi… (
Browse files Browse the repository at this point in the history
#5984, fixes #5330)
  • Loading branch information
westnordost authored Oct 25, 2024
1 parent 0f11187 commit 5dc0450
Show file tree
Hide file tree
Showing 38 changed files with 401 additions and 801 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package de.westnordost.streetcomplete.osm.sidewalk_surface

import de.westnordost.streetcomplete.osm.surface.SurfaceAndNote
import de.westnordost.streetcomplete.osm.surface.Surface

data class LeftAndRightSidewalkSurface(
val left: SurfaceAndNote?,
val right: SurfaceAndNote?,
)
data class LeftAndRightSidewalkSurface(val left: Surface?, val right: Surface?)
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package de.westnordost.streetcomplete.osm.sidewalk_surface

import de.westnordost.streetcomplete.osm.expandSidesTags
import de.westnordost.streetcomplete.osm.surface.parseSurfaceAndNote
import de.westnordost.streetcomplete.osm.surface.parseSurface

fun parseSidewalkSurface(tags: Map<String, String>): LeftAndRightSidewalkSurface? {
val expandedTags = expandRelevantSidesTags(tags)
val expandedTags = tags.toMutableMap()
expandedTags.expandSidesTags("sidewalk", "surface", true)

val left = parseSurfaceAndNote(expandedTags, "sidewalk:left")
val right = parseSurfaceAndNote(expandedTags, "sidewalk:right")
val left = parseSurface(expandedTags["sidewalk:left:surface"])
val right = parseSurface(expandedTags["sidewalk:right:surface"])

if (left == null && right == null) return null

return LeftAndRightSidewalkSurface(left, right)
}

private fun expandRelevantSidesTags(tags: Map<String, String>): Map<String, String> {
val result = tags.toMutableMap()
result.expandSidesTags("sidewalk", "surface", true)
result.expandSidesTags("sidewalk", "surface:note", true)
return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package de.westnordost.streetcomplete.osm.surface

import de.westnordost.streetcomplete.osm.surface.Surface.*

data class SurfaceAndNote(val surface: Surface?, val note: String? = null)

enum class Surface(val osmValue: String?) {
ASPHALT("asphalt"),
CONCRETE("concrete"),
Expand Down Expand Up @@ -80,10 +78,3 @@ val SELECTABLE_WAY_SURFACES = listOf(
// generic surfaces
PAVED, UNPAVED, GROUND
)

private val UNDERSPECIFED_SURFACES = setOf(PAVED, UNPAVED)

val Surface.shouldBeDescribed: Boolean get() = this in UNDERSPECIFED_SURFACES

val SurfaceAndNote.isComplete: Boolean get() =
surface != null && (!surface.shouldBeDescribed || note != null)
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import de.westnordost.streetcomplete.osm.Tags
import de.westnordost.streetcomplete.osm.removeCheckDatesForKey
import de.westnordost.streetcomplete.osm.updateWithCheckDate

/** Apply the surface and note to the given [tags], with optional [prefix], e.g. "footway" for
/** Apply the surface to the given [tags], with optional [prefix], e.g. "footway" for
* "footway:surface.
* By default the check date is also updated if the surface did not change, specified
* [updateCheckDate] = false if this should not be done. */
fun SurfaceAndNote.applyTo(tags: Tags, prefix: String? = null, updateCheckDate: Boolean = true) {
val osmValue = surface?.osmValue
fun Surface.applyTo(tags: Tags, prefix: String? = null, updateCheckDate: Boolean = true) {
requireNotNull(osmValue) { "Surface must be valid and not null" }

val pre = if (prefix != null) "$prefix:" else ""
val key = "${pre}surface"
val previousOsmValue = tags[key]
val hasChanged = previousOsmValue != null && previousOsmValue != osmValue

if (previousOsmValue != null && previousOsmValue != osmValue) {
if (hasChanged) {
// category of surface changed -> likely that tracktype is not correct anymore
if (prefix == null && parseSurfaceCategory(osmValue) != parseSurfaceCategory(previousOsmValue)) {
tags.remove("tracktype")
Expand All @@ -33,10 +33,8 @@ fun SurfaceAndNote.applyTo(tags: Tags, prefix: String? = null, updateCheckDate:
tags[key] = osmValue
}

// add/remove note - used to describe generic surfaces
if (note != null) {
tags["$key:note"] = note
} else {
// remove note if surface has changed
if (hasChanged) {
tags.remove("$key:note")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package de.westnordost.streetcomplete.osm.surface

/** Parse the surface and optional associated note from the given [tags].
* Specify a [prefix] if you want for example the surface of the "footway" (then looks for
* "footway:surface") */
fun parseSurfaceAndNote(tags: Map<String, String>, prefix: String? = null): SurfaceAndNote? {
val pre = if (prefix != null) "$prefix:" else ""
val note = tags["${pre}surface:note"]
val surface = parseSurface(tags["${pre}surface"])
if (surface == null && note == null) return null
return SurfaceAndNote(surface, note)
}

fun parseSurface(surface: String?): Surface? {
if (surface == null) return null
if (surface in INVALID_SURFACES) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fun updateCommonSurfaceFromFootAndCyclewaySurface(tags: Tags) {
if (cyclewaySurface != null && footwaySurface != null) {
val commonSurface = getCommonSurface(footwaySurface, cyclewaySurface)
if (commonSurface != null) {
SurfaceAndNote(parseSurface(commonSurface), tags["surface:note"]).applyTo(tags)
parseSurface(commonSurface)?.applyTo(tags)
} else {
tags.remove("surface")
tags.remove("surface:note")
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5dc0450

Please sign in to comment.