Skip to content

Commit

Permalink
Merge pull request #4906 from Helium314/Helium314-patch-1
Browse files Browse the repository at this point in the history
Store tangram geometries in styled element
  • Loading branch information
westnordost committed Mar 27, 2023
2 parents 07e37d4 + e5dddd6 commit bb25d2a
Showing 1 changed file with 67 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package de.westnordost.streetcomplete.screens.main.map.components
import android.content.res.Resources
import android.graphics.Color
import com.mapzen.tangram.MapData
import com.mapzen.tangram.geometry.Geometry
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.geometry.ElementPointGeometry
Expand Down Expand Up @@ -36,64 +37,73 @@ class StyleableOverlayMapComponent(private val resources: Resources, ctrl: KtMap

/** Show given map data with each the given style */
fun set(features: Collection<StyledElement>) {
layer.setFeatures(features.flatMap { (element, geometry, style) ->
val props = HashMap<String, String>()
props[ELEMENT_ID] = element.id.toString()
props[ELEMENT_TYPE] = element.type.name
val layer = element.tags["layer"]?.toIntOrNull()?.takeIf { it.absoluteValue <= 20 } ?: 0
props["layer"] = layer.toString()
when (style) {
is PolygonStyle -> {
getHeight(element.tags)?.let { props["height"] = it.toString() }
props["color"] = getColorWithSomeTransparency(style.color)
props["strokeColor"] = getColorWithSomeTransparency(getDarkenedColor(style.color))
layer.setFeatures(features.flatMap { styledElement ->
styledElement.tangramGeometries?.let { return@flatMap it }

val geometries = createTangramGeometries(styledElement)
styledElement.tangramGeometries = geometries
geometries
})
}

private fun createTangramGeometries(styledElement: StyledElement): List<Geometry> {
val element = styledElement.element
val geometry = styledElement.geometry
val style = styledElement.style
val props = HashMap<String, String>()
props[ELEMENT_ID] = element.id.toString()
props[ELEMENT_TYPE] = element.type.name
val layer = element.tags["layer"]?.toIntOrNull()?.takeIf { it.absoluteValue <= 20 } ?: 0
props["layer"] = layer.toString()
when (style) {
is PolygonStyle -> {
getHeight(element.tags)?.let { props["height"] = it.toString() }
props["color"] = getColorWithSomeTransparency(style.color)
props["strokeColor"] = getColorWithSomeTransparency(getDarkenedColor(style.color))
}
is PolylineStyle -> {
val width = getLineWidth(element.tags)
// thin lines should be rendered on top (see #4291)
if (width <= 2f) props["layer"] = (layer + 1).toString()
props["width"] = width.toString()
style.strokeLeft?.let {
if (it.dashed) props["dashedLeft"] = "1"
props["colorLeft"] = it.color
}
is PolylineStyle -> {
val width = getLineWidth(element.tags)
// thin lines should be rendered on top (see #4291)
if (width <= 2f) props["layer"] = (layer + 1).toString()
props["width"] = width.toString()
style.strokeLeft?.let {
if (it.dashed) props["dashedLeft"] = "1"
props["colorLeft"] = it.color
}
style.strokeRight?.let {
if (it.dashed) props["dashedRight"] = "1"
props["colorRight"] = it.color
}
if (style.stroke != null) {
if (style.stroke.dashed) props["dashed"] = "1"
props["color"] = style.stroke.color
props["strokeColor"] = getDarkenedColor(style.stroke.color)
} else if (style.strokeLeft != null || style.strokeRight != null) {
// must have a color for the center if left or right is defined because
// there are really ugly overlaps in tangram otherwise
props["color"] = resources.getString(R.string.road_color)
props["strokeColor"] = resources.getString(R.string.road_outline_color)
}
style.label?.let { props["text"] = it }
style.strokeRight?.let {
if (it.dashed) props["dashedRight"] = "1"
props["colorRight"] = it.color
}
is PointStyle -> {
style.label?.let { props["text"] = it }
style.icon?.let { props["icon"] = it }
if (style.stroke != null) {
if (style.stroke.dashed) props["dashed"] = "1"
props["color"] = style.stroke.color
props["strokeColor"] = getDarkenedColor(style.stroke.color)
} else if (style.strokeLeft != null || style.strokeRight != null) {
// must have a color for the center if left or right is defined because
// there are really ugly overlaps in tangram otherwise
props["color"] = resources.getString(R.string.road_color)
props["strokeColor"] = resources.getString(R.string.road_outline_color)
}
style.label?.let { props["text"] = it }
}
is PointStyle -> {
style.label?.let { props["text"] = it }
style.icon?.let { props["icon"] = it }
}
}

geometry.toTangramGeometry(props)
} + // workaround for https://github.com/tangrams/tangram-es/issues/2332 and an unreported
return if (style is PolygonStyle && (style.icon != null || style.label != null)) {
// workaround for https://github.com/tangrams/tangram-es/issues/2332 and an unreported
// issue that icons for polygons are shown on every single vertex
features
.filter { it.style is PolygonStyle && (it.style.icon != null || it.style.label != null) }
.flatMap { (element, geometry, style) ->
val props = HashMap<String, String>(4)
val polygonStyle = style as PolygonStyle
props[ELEMENT_ID] = element.id.toString()
props[ELEMENT_TYPE] = element.type.name
polygonStyle.icon?.let { props["icon"] = it }
polygonStyle.label?.let { props["text"] = it }
ElementPointGeometry(geometry.center).toTangramGeometry(props)
}
)
val properties = HashMap<String, String>(4, 1.0f)
properties[ELEMENT_ID] = element.id.toString()
properties[ELEMENT_TYPE] = element.type.name
style.icon?.let { properties["icon"] = it }
style.label?.let { properties["text"] = it }
geometry.toTangramGeometry(props) + ElementPointGeometry(geometry.center).toTangramGeometry(properties)
} else {
geometry.toTangramGeometry(props)
}
}

/** mimics width of line as seen in StreetComplete map style (or otherwise 3m) */
Expand Down Expand Up @@ -148,4 +158,8 @@ data class StyledElement(
val element: Element,
val geometry: ElementGeometry,
val style: Style
)
) {
// geometries may contain road color, which depends on current theme
// however, storing is not an issue as styled elements are cleared on theme switch (both automatic and manual)
var tangramGeometries: List<Geometry>? = null
}

0 comments on commit bb25d2a

Please sign in to comment.