Skip to content

Commit

Permalink
use extension functions to convert element geometry to maplibre data …
Browse files Browse the repository at this point in the history
…structures
  • Loading branch information
westnordost committed Feb 22, 2024
1 parent 61d498b commit 971b91a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.screens.MainActivity
import de.westnordost.streetcomplete.screens.main.map.MainMapFragment
import de.westnordost.streetcomplete.screens.main.map.clear
import de.westnordost.streetcomplete.screens.main.map.maplibre.toPoint
import de.westnordost.streetcomplete.screens.main.map.tangram.KtMapController
import de.westnordost.streetcomplete.util.ktx.getBitmapDrawable
import de.westnordost.streetcomplete.util.ktx.isApril1st
Expand Down Expand Up @@ -183,7 +184,7 @@ class CurrentLocationMapComponent(ctx: Context, mapStyle: Style, private val ctr
p.addProperty("rotation", rotation?.toFloat() ?: 0f)

MainActivity.activity?.runOnUiThread {
locationSource.setGeoJson(Feature.fromGeometry(Point.fromLngLat(pos.longitude, pos.latitude), p))
locationSource.setGeoJson(Feature.fromGeometry(pos.toPoint(), p))
}
if (useLocationComponent) // do nothing instead of crashing
MainMapFragment.mapboxMap?.locationComponent?.forceLocationUpdate(location)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package de.westnordost.streetcomplete.screens.main.map.components

import com.mapbox.geojson.Point
import com.mapbox.geojson.Polygon
import de.westnordost.streetcomplete.ApplicationConstants
import de.westnordost.streetcomplete.data.download.tiles.TilePos
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.osm.mapdata.toPolygon
import de.westnordost.streetcomplete.screens.MainActivity
import de.westnordost.streetcomplete.screens.main.map.MainMapFragment
import de.westnordost.streetcomplete.screens.main.map.maplibre.toPoint
import de.westnordost.streetcomplete.screens.main.map.tangram.KtMapController

class DownloadedAreaMapComponent(private val ctrl: KtMapController) {

fun set(tiles: Collection<TilePos>) = synchronized(this) {
// tangram does not clear a layer properly on re-setting features on it, so let's remove
// and re-add the whole layer

val zoom = ApplicationConstants.DOWNLOAD_TILE_ZOOM
val world = listOf(
LatLon(+90.0, -180.0),
Expand All @@ -27,7 +24,7 @@ class DownloadedAreaMapComponent(private val ctrl: KtMapController) {
val holes = tiles.map { it.asBoundingBox(zoom).toPolygon().asReversed() }
val polygons = listOf(world) + holes

val feature = Polygon.fromLngLats(polygons.map { polygon -> polygon.map { Point.fromLngLat(it.longitude, it.latitude) } })
val feature = Polygon.fromLngLats(polygons.map { polygon -> polygon.map { it.toPoint() } })
MainActivity.activity?.runOnUiThread { MainMapFragment.downloadedAreaSource?.setGeoJson(feature) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import com.mapbox.mapboxsdk.maps.MapboxMap
import de.westnordost.streetcomplete.data.maptiles.toLatLng
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.geometry.ElementPointGeometry
import de.westnordost.streetcomplete.data.osm.geometry.ElementPolygonsGeometry
import de.westnordost.streetcomplete.data.osm.geometry.ElementPolylinesGeometry
import de.westnordost.streetcomplete.screens.MainActivity
import de.westnordost.streetcomplete.screens.main.map.MainMapFragment
import de.westnordost.streetcomplete.screens.main.map.maplibre.CameraPosition
import de.westnordost.streetcomplete.screens.main.map.maplibre.toLatLon
import de.westnordost.streetcomplete.screens.main.map.maplibre.toMapLibreGeometry
import de.westnordost.streetcomplete.screens.main.map.tangram.KtMapController
import kotlin.math.abs
import kotlin.math.max
Expand All @@ -34,29 +33,7 @@ class FocusGeometryMapComponent(private val ctrl: KtMapController, private val m

/** Show the given geometry. Previously shown geometry is replaced. */
fun showGeometry(geometry: ElementGeometry) {

when (geometry) {
is ElementPolylinesGeometry -> {
val points = geometry.polylines.map { it.map { com.mapbox.geojson.Point.fromLngLat(it.longitude, it.latitude) } }
val multilineString = com.mapbox.geojson.MultiLineString.fromLngLats(points)
MainMapFragment.focusedGeometrySource?.setGeoJson(Feature.fromGeometry(multilineString).apply { addStringProperty("way", "yes") })
}
is ElementPolygonsGeometry -> {
val points = geometry.polygons.map { it.map { com.mapbox.geojson.Point.fromLngLat(it.longitude, it.latitude) } }
val polygon = com.mapbox.geojson.Polygon.fromLngLats(points)
// todo: breaks for mulitpolygons at high zoom only
// todo: actually the outline is displayed in the fill layer
// maybe this is what breaks multipolygon display
// just set some Expression.geometryType() filter on the fill layer
val multilineString = com.mapbox.geojson.MultiLineString.fromLngLats(points) // outline
MainMapFragment.focusedGeometrySource?.setGeoJson(
FeatureCollection.fromFeatures(listOf(
Feature.fromGeometry(multilineString), Feature.fromGeometry(polygon))))
}
is ElementPointGeometry -> {
MainMapFragment.focusedGeometrySource?.setGeoJson(com.mapbox.geojson.Point.fromLngLat(geometry.center.longitude, geometry.center.latitude))
}
}
MainMapFragment.focusedGeometrySource?.setGeoJson(geometry.toMapLibreGeometry())
}

/** Hide all shown geometry */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import de.westnordost.streetcomplete.data.osm.geometry.ElementPolylinesGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.screens.MainActivity
import de.westnordost.streetcomplete.screens.main.map.MainMapFragment
import de.westnordost.streetcomplete.screens.main.map.maplibre.toMapLibreGeometry
import de.westnordost.streetcomplete.screens.main.map.maplibre.toPoint
import de.westnordost.streetcomplete.screens.main.map.tangram.KtMapController
import de.westnordost.streetcomplete.util.math.centerPointOfPolyline
Expand Down Expand Up @@ -70,8 +71,7 @@ class GeometryMarkersMapComponent(private val resources: Resources, private val
// polygon / polylines marker(s)
if (geometry is ElementPolygonsGeometry || geometry is ElementPolylinesGeometry) {
if (geometry is ElementPolygonsGeometry) {
val points = geometry.polygons.map { it.map { Point.fromLngLat(it.longitude, it.latitude) } }
features.add(Feature.fromGeometry(Polygon.fromLngLats(points)))
features.add(Feature.fromGeometry(geometry.toMapLibreGeometry()))
}

/* Polygons should be styled to have a more opaque outline. Due to a technical
Expand All @@ -82,8 +82,7 @@ class GeometryMarkersMapComponent(private val resources: Resources, private val
is ElementPolylinesGeometry -> geometry
else -> throw IllegalStateException()
}
val points = polylines.polylines.map { it.map { Point.fromLngLat(it.longitude, it.latitude) } }
features.add(Feature.fromGeometry(Polygon.fromLngLats(points)))
features.add(Feature.fromGeometry(polylines.toMapLibreGeometry()))
}

featuresByPosition[center] = features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.mapbox.mapboxsdk.style.expressions.Expression
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.screens.MainActivity
import de.westnordost.streetcomplete.screens.main.map.MainMapFragment
import de.westnordost.streetcomplete.screens.main.map.maplibre.toPoint
import de.westnordost.streetcomplete.screens.main.map.tangram.KtMapController

/** Takes care of displaying pins on the map, e.g. quest pins or pins for recent edits */
Expand All @@ -29,13 +30,13 @@ class PinsMapComponent(private val ctrl: KtMapController) {
fun set(pins: Collection<Pin>) {
// do sorting here, because we can set the symbolZOrder to SYMBOL_Z_ORDER_SOURCE, which
// is the order in which the source has the features
val mapLibreFeatures = pins.sortedBy { -it.importance }.map {
val p = JsonObject()
p.addProperty("icon-image", it.iconName)
p.addProperty("symbol-sort-key", -it.importance.toFloat()) // still set sort key, because we may want to try it again
it.properties.forEach { p.addProperty(it.first, it.second) }
Feature.fromGeometry(com.mapbox.geojson.Point.fromLngLat(it.position.longitude, it.position.latitude), p)
}
val mapLibreFeatures = pins.sortedBy { -it.importance }.map { pin ->
val p = JsonObject()
p.addProperty("icon-image", pin.iconName)
p.addProperty("symbol-sort-key", -pin.importance.toFloat()) // still set sort key, because we may want to try it again
pin.properties.forEach { p.addProperty(it.first, it.second) }
Feature.fromGeometry(pin.position.toPoint(), p)
}
// todo: for testing the runOnUiThread is ok, but actually it should be handled differently...
MainActivity.activity?.runOnUiThread { MainMapFragment.pinsSource?.setGeoJson(FeatureCollection.fromFeatures(mapLibreFeatures)) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import de.westnordost.streetcomplete.overlays.PolylineStyle
import de.westnordost.streetcomplete.overlays.Style
import de.westnordost.streetcomplete.screens.MainActivity
import de.westnordost.streetcomplete.screens.main.map.MainMapFragment
import de.westnordost.streetcomplete.screens.main.map.maplibre.toMapLibreGeometry
import de.westnordost.streetcomplete.screens.main.map.maplibre.toPoint
import de.westnordost.streetcomplete.screens.main.map.tangram.KtMapController
import de.westnordost.streetcomplete.util.ktx.addTransparency
Expand Down Expand Up @@ -78,20 +79,18 @@ class StyleableOverlayMapComponent(private val resources: Resources, ctrl: KtMap
if (getHeight(element.tags) != null)
p.addProperty("height", getHeight(element.tags))

val points = (geometry as ElementPolygonsGeometry).polygons.map { it.map { Point.fromLngLat(it.longitude, it.latitude) } }
val f = Feature.fromGeometry(Polygon.fromLngLats(points), p)
val f = Feature.fromGeometry(geometry.toMapLibreGeometry(), p)
val label = if (style.label != null) {
Feature.fromGeometry(
Point.fromLngLat(geometry.center.longitude, geometry.center.latitude),
geometry.center.toPoint(),
JsonObject().apply { addProperty("label", style.label) }
)
} else null
listOfNotNull(f, label)
}
is PolylineStyle -> {
// there is no strokeColor for lines, so appearance is different and thinner due to missing "line-outline"
val points = (geometry as ElementPolylinesGeometry).polylines.map { it.map { Point.fromLngLat(it.longitude, it.latitude) } }
val line = MultiLineString.fromLngLats(points)
val line = geometry.toMapLibreGeometry()
val width = getLineWidth(element.tags)
val left = if (style.strokeLeft != null) {
val p2 = p.deepCopy()
Expand Down

0 comments on commit 971b91a

Please sign in to comment.