Skip to content

Commit

Permalink
Pass getCountryInfoByLocation and getCountryCodeByLocation to que…
Browse files Browse the repository at this point in the history
…sts/modules (#5280)

* Pass `getCountryBoundaries` as function

* Pass `getCountryInfoByLocation` to quests

* Pass `getCountryInfoByLocation` and `getCountryCodeByLocation` to modules
  • Loading branch information
FloEdelmann authored Oct 8, 2023
1 parent c6feb46 commit 0d8f4a3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package de.westnordost.streetcomplete.overlays
import de.westnordost.countryboundaries.CountryBoundaries
import de.westnordost.osmfeatures.Feature
import de.westnordost.osmfeatures.FeatureDictionary
import de.westnordost.streetcomplete.data.meta.CountryInfo
import de.westnordost.streetcomplete.data.meta.CountryInfos
import de.westnordost.streetcomplete.data.meta.getByLocation
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.overlays.OverlayRegistry
import de.westnordost.streetcomplete.overlays.address.AddressOverlay
import de.westnordost.streetcomplete.overlays.cycleway.CyclewayOverlay
Expand All @@ -13,6 +16,7 @@ import de.westnordost.streetcomplete.overlays.street_parking.StreetParkingOverla
import de.westnordost.streetcomplete.overlays.surface.SurfaceOverlay
import de.westnordost.streetcomplete.overlays.way_lit.WayLitOverlay
import de.westnordost.streetcomplete.util.ktx.getFeature
import de.westnordost.streetcomplete.util.ktx.getIds
import org.koin.core.qualifier.named
import org.koin.dsl.module
import java.util.concurrent.FutureTask
Expand All @@ -22,8 +26,15 @@ import java.util.concurrent.FutureTask
val overlaysModule = module {
single {
overlaysRegistry(
get(),
get(named("CountryBoundariesFuture")),
{ location ->
val countryInfos = get<CountryInfos>()
val countryBoundaries = get<FutureTask<CountryBoundaries>>(named("CountryBoundariesFuture")).get()
countryInfos.getByLocation(countryBoundaries, location.longitude, location.latitude)
},
{ location ->
val countryBoundaries = get<FutureTask<CountryBoundaries>>(named("CountryBoundariesFuture")).get()
countryBoundaries.getIds(location).firstOrNull()
},
{ tags ->
get<FutureTask<FeatureDictionary>>(named("FeatureDictionaryFuture"))
.get().getFeature(tags)
Expand All @@ -33,16 +44,16 @@ val overlaysModule = module {
}

fun overlaysRegistry(
countryInfos: CountryInfos,
countryBoundariesFuture: FutureTask<CountryBoundaries>,
getCountryInfoByLocation: (location: LatLon) -> CountryInfo,
getCountryCodeByLocation: (location: LatLon) -> String?,
getFeature: (tags: Map<String, String>) -> Feature?,
) = OverlayRegistry(listOf(

0 to WayLitOverlay(),
6 to SurfaceOverlay(),
1 to SidewalkOverlay(),
5 to CyclewayOverlay(countryInfos, countryBoundariesFuture),
5 to CyclewayOverlay(getCountryInfoByLocation),
2 to StreetParkingOverlay(),
3 to AddressOverlay(countryBoundariesFuture),
3 to AddressOverlay(getCountryCodeByLocation),
4 to ShopsOverlay(getFeature),
))
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package de.westnordost.streetcomplete.overlays.address

import de.westnordost.countryboundaries.CountryBoundaries
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.osm.mapdata.Element
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataWithGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.filter
import de.westnordost.streetcomplete.data.user.achievements.EditTypeAchievement.POSTMAN
Expand All @@ -12,11 +12,9 @@ import de.westnordost.streetcomplete.overlays.PointStyle
import de.westnordost.streetcomplete.overlays.PolygonStyle
import de.westnordost.streetcomplete.quests.address.AddHousenumber
import de.westnordost.streetcomplete.util.getShortHouseNumber
import de.westnordost.streetcomplete.util.ktx.getIds
import java.util.concurrent.FutureTask

class AddressOverlay(
private val countryBoundaries: FutureTask<CountryBoundaries>
private val getCountryCodeByLocation: (location: LatLon) -> String?
) : Overlay {

override val title = R.string.overlay_addresses
Expand Down Expand Up @@ -49,7 +47,7 @@ class AddressOverlay(
.filter("ways, relations with building")
.filter {
val center = mapData.getGeometry(it.type, it.id)?.center ?: return@filter false
val country = countryBoundaries.get().getIds(center).firstOrNull()
val country = getCountryCodeByLocation(center)
country !in noAddressesOnBuildings
}
.map { it to PolygonStyle(Color.INVISIBLE, label = getShortHouseNumber(it.tags)) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package de.westnordost.streetcomplete.overlays.cycleway

import de.westnordost.countryboundaries.CountryBoundaries
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.elementfilter.toElementFilterExpression
import de.westnordost.streetcomplete.data.meta.CountryInfo
import de.westnordost.streetcomplete.data.meta.CountryInfos
import de.westnordost.streetcomplete.data.meta.getByLocation
import de.westnordost.streetcomplete.data.osm.mapdata.Element
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataWithGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.filter
import de.westnordost.streetcomplete.data.user.achievements.EditTypeAchievement
Expand All @@ -27,11 +25,9 @@ import de.westnordost.streetcomplete.overlays.Overlay
import de.westnordost.streetcomplete.overlays.PolylineStyle
import de.westnordost.streetcomplete.overlays.StrokeStyle
import de.westnordost.streetcomplete.quests.cycleway.AddCycleway
import java.util.concurrent.FutureTask

class CyclewayOverlay(
private val countryInfos: CountryInfos,
private val countryBoundaries: FutureTask<CountryBoundaries>
private val getCountryInfoByLocation: (location: LatLon) -> CountryInfo,
) : Overlay {

override val title = R.string.overlay_cycleway
Expand All @@ -49,11 +45,7 @@ class CyclewayOverlay(
and area != yes
""").mapNotNull {
val pos = mapData.getWayGeometry(it.id)?.center ?: return@mapNotNull null
val countryInfo = countryInfos.getByLocation(
countryBoundaries.get(),
pos.longitude,
pos.latitude
)
val countryInfo = getCountryInfoByLocation(pos)
it to getStreetCyclewayStyle(it, countryInfo)
} +
// separately mapped ways
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package de.westnordost.streetcomplete.quests
import de.westnordost.countryboundaries.CountryBoundaries
import de.westnordost.osmfeatures.Feature
import de.westnordost.osmfeatures.FeatureDictionary
import de.westnordost.streetcomplete.data.meta.CountryInfo
import de.westnordost.streetcomplete.data.meta.CountryInfos
import de.westnordost.streetcomplete.data.meta.getByLocation
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.osmnotes.notequests.OsmNoteQuestType
import de.westnordost.streetcomplete.data.quest.QuestTypeRegistry
import de.westnordost.streetcomplete.quests.accepts_cards.AddAcceptsCards
Expand Down Expand Up @@ -182,8 +185,11 @@ val questsModule = module {
get(),
get(),
get(),
get(named("CountryBoundariesFuture")),
get(),
{ location ->
val countryInfos = get<CountryInfos>()
val countryBoundaries = get<FutureTask<CountryBoundaries>>(named("CountryBoundariesFuture")).get()
countryInfos.getByLocation(countryBoundaries, location.longitude, location.latitude)
},
{ tags ->
get<FutureTask<FeatureDictionary>>(named("FeatureDictionaryFuture"))
.get().getFeature(tags)
Expand All @@ -195,9 +201,8 @@ val questsModule = module {
fun questTypeRegistry(
trafficFlowSegmentsApi: TrafficFlowSegmentsApi,
trafficFlowDao: WayTrafficFlowDao,
countryInfos: CountryInfos,
countryBoundariesFuture: FutureTask<CountryBoundaries>,
arSupportChecker: ArSupportChecker,
getCountryInfoByLocation: (location: LatLon) -> CountryInfo,
getFeature: (tags: Map<String, String>) -> Feature?,
) = QuestTypeRegistry(listOf(

Expand Down Expand Up @@ -474,7 +479,7 @@ fun questTypeRegistry(
134 to AddSidewalk(), // for any pedestrian routers, needs minimal thinking
135 to AddRoadSurface(), // used by BRouter, OsmAnd, OSRM, graphhopper, HOT map style... - sometimes requires way to be split
136 to AddTracktype(), // widely used in map rendering - OSM Carto, OsmAnd...
137 to AddCycleway(countryInfos, countryBoundariesFuture), // for any cyclist routers (and cyclist maps)
137 to AddCycleway(getCountryInfoByLocation), // for any cyclist routers (and cyclist maps)
138 to AddLanes(), // abstreet, certainly most routing engines - often requires way to be split

// disabled completely because definition is too fuzzy/broad to be useful and easy to answer,
Expand All @@ -500,7 +505,7 @@ fun questTypeRegistry(
// buildings
150 to AddBuildingType(),
151 to AddBuildingLevels(),
152 to AddRoofShape(countryInfos, countryBoundariesFuture),
152 to AddRoofShape(getCountryInfoByLocation),

153 to AddStepCount(), // can only be gathered when walking along this way, also needs the most effort and least useful

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package de.westnordost.streetcomplete.quests.cycleway

import de.westnordost.countryboundaries.CountryBoundaries
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.elementfilter.filters.RelativeDate
import de.westnordost.streetcomplete.data.elementfilter.filters.TagOlderThan
import de.westnordost.streetcomplete.data.elementfilter.toElementFilterExpression
import de.westnordost.streetcomplete.data.meta.CountryInfo
import de.westnordost.streetcomplete.data.meta.CountryInfos
import de.westnordost.streetcomplete.data.meta.getByLocation
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.Element
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataWithGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.filter
import de.westnordost.streetcomplete.data.osm.osmquests.OsmElementQuestType
Expand All @@ -25,11 +23,9 @@ import de.westnordost.streetcomplete.osm.cycleway.applyTo
import de.westnordost.streetcomplete.osm.cycleway.createCyclewaySides
import de.westnordost.streetcomplete.osm.cycleway.isAmbiguous
import de.westnordost.streetcomplete.osm.surface.ANYTHING_UNPAVED
import java.util.concurrent.FutureTask

class AddCycleway(
private val countryInfos: CountryInfos,
private val countryBoundariesFuture: FutureTask<CountryBoundaries>,
private val getCountryInfoByLocation: (location: LatLon) -> CountryInfo,
) : OsmElementQuestType<LeftAndRightCycleway> {

override val changesetComment = "Specify whether there are cycleways"
Expand Down Expand Up @@ -85,12 +81,8 @@ class AddCycleway(
val eligibleRoads = mapData.ways.filter { roadsFilter.matches(it) }
val roadsWithMissingCycleway = eligibleRoads.filter { untaggedRoadsFilter.matches(it) }
val oldRoadsWithKnownCycleways = eligibleRoads.filter { way ->
val countryInfo = mapData.getWayGeometry(way.id)?.center?.let { p ->
countryInfos.getByLocation(
countryBoundariesFuture.get(),
p.longitude,
p.latitude,
)
val countryInfo = mapData.getWayGeometry(way.id)?.center?.let {
getCountryInfoByLocation(it)
}
way.hasOldInvalidOrAmbiguousCyclewayTags(countryInfo) == true
}
Expand All @@ -107,11 +99,7 @@ class AddCycleway(
override fun createForm() = AddCyclewayForm()

override fun applyAnswerTo(answer: LeftAndRightCycleway, tags: Tags, geometry: ElementGeometry, timestampEdited: Long) {
val countryInfo = countryInfos.getByLocation(
countryBoundariesFuture.get(),
geometry.center.longitude,
geometry.center.latitude
)
val countryInfo = getCountryInfoByLocation(geometry.center)
answer.applyTo(tags, countryInfo.isLeftHandTraffic)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package de.westnordost.streetcomplete.quests.roof_shape

import de.westnordost.countryboundaries.CountryBoundaries
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.elementfilter.toElementFilterExpression
import de.westnordost.streetcomplete.data.meta.CountryInfos
import de.westnordost.streetcomplete.data.meta.getByLocation
import de.westnordost.streetcomplete.data.meta.CountryInfo
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.Element
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataWithGeometry
import de.westnordost.streetcomplete.data.osm.osmquests.OsmElementQuestType
import de.westnordost.streetcomplete.data.user.achievements.EditTypeAchievement.BUILDING
import de.westnordost.streetcomplete.osm.BUILDINGS_WITH_LEVELS
import de.westnordost.streetcomplete.osm.Tags
import java.util.concurrent.FutureTask

class AddRoofShape(
private val countryInfos: CountryInfos,
private val countryBoundariesFuture: FutureTask<CountryBoundaries>,
private val getCountryInfoByLocation: (location: LatLon) -> CountryInfo,
) : OsmElementQuestType<RoofShape> {

private val filter by lazy { """
Expand Down Expand Up @@ -58,11 +55,7 @@ class AddRoofShape(

private fun roofsAreUsuallyFlatAt(element: Element, mapData: MapDataWithGeometry): Boolean? {
val center = mapData.getGeometry(element.type, element.id)?.center ?: return null
return countryInfos.getByLocation(
countryBoundariesFuture.get(),
center.longitude,
center.latitude,
).roofsAreUsuallyFlat
return getCountryInfoByLocation(center).roofsAreUsuallyFlat
}

override fun applyAnswerTo(answer: RoofShape, tags: Tags, geometry: ElementGeometry, timestampEdited: Long) {
Expand Down

0 comments on commit 0d8f4a3

Please sign in to comment.