Skip to content

Commit

Permalink
Fix element highlightning in amenity_coverage
Browse files Browse the repository at this point in the history
In the AddAmenityCover-Task,

getHighlightedElements(..) highlighted picnic_tables when asked for bbq nodes.

Mostly copy&paste from the logik used in the CheckExistence-quest
  • Loading branch information
qugebert authored and westnordost committed Aug 27, 2023
1 parent d8589ef commit 548fc8e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fun questTypeRegistry(
9 to AddCarWashType(),

10 to AddBenchBackrest(),
11 to AddAmenityCover(),
11 to AddAmenityCover(featureDictionaryFuture),

12 to AddBridgeStructure(),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
package de.westnordost.streetcomplete.quests.amenity_cover

import de.westnordost.osmfeatures.FeatureDictionary
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.elementfilter.toElementFilterExpression
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.Element
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataWithGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.filter
import de.westnordost.streetcomplete.data.osm.osmquests.OsmFilterQuestType
import de.westnordost.streetcomplete.data.osm.osmquests.OsmElementQuestType
import de.westnordost.streetcomplete.data.user.achievements.EditTypeAchievement.OUTDOORS
import de.westnordost.streetcomplete.osm.Tags
import de.westnordost.streetcomplete.quests.YesNoQuestForm
import de.westnordost.streetcomplete.util.ktx.toYesNo
import java.util.concurrent.FutureTask

class AddAmenityCover : OsmFilterQuestType<Boolean>() {
class AddAmenityCover (
private val featureDictionaryFuture: FutureTask<FeatureDictionary>
) : OsmElementQuestType<Boolean> {

override val elementFilter = """
private val nodesFilter by lazy { """
nodes with
(leisure = picnic_table
or amenity = bbq)
and access !~ private|no
and !covered
and (!seasonal or seasonal = no)
"""
""".toElementFilterExpression() }
override val changesetComment = "Specify whether various amenities are covered"
override val wikiLink = "Key:covered"
override val icon = R.drawable.ic_quest_picnic_table_cover
override val isDeleteElementEnabled = true
override val achievements = listOf(OUTDOORS)

override fun getTitle(tags: Map<String, String>) = R.string.quest_amenityCover_title
override fun getApplicableElements(mapData: MapDataWithGeometry): Iterable<Element> =
mapData.filter { isApplicableTo(it) }

override fun getHighlightedElements(element: Element, getMapData: () -> MapDataWithGeometry) =
getMapData().filter("nodes with leisure = picnic_table")
override fun isApplicableTo(element: Element) =
nodesFilter.matches(element) && hasAnyName(element.tags)

private fun hasAnyName(tags: Map<String, String>): Boolean =
featureDictionaryFuture.get().byTags(tags).isSuggestion(false).find().isNotEmpty()

override fun getHighlightedElements(element: Element, getMapData: () -> MapDataWithGeometry): Sequence<Element> {
/* put markers for objects that are exactly the same as for which this quest is asking for
e.g. it's a ticket validator? -> display other ticket validators. Etc. */
val feature = featureDictionaryFuture.get()
.byTags(element.tags)
.isSuggestion(false) // not brands
.find()
.firstOrNull() ?: return emptySequence()

return getMapData().filter { it.tags.containsAll(feature.tags) }.asSequence()
}

override fun createForm() = YesNoQuestForm()

override fun applyAnswerTo(answer: Boolean, tags: Tags, geometry: ElementGeometry, timestampEdited: Long) {
tags["covered"] = answer.toYesNo()
}
}

private fun <X, Y> Map<X, Y>.containsAll(other: Map<X, Y>) = other.all { this[it.key] == it.value }

0 comments on commit 548fc8e

Please sign in to comment.