Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ object GeoJsonMapper {
val finalProperties = if (id != null) featureProperties + ("id" to id) else featureProperties

val style = properties?.let { props ->
when (geometry) {
is LineString, is MultiGeometry -> { // MultiGeometry could contain lines
when {
geometry is LineString || (geometry is MultiGeometry && !geometry.isPolygonal()) -> {
// MultiGeometry could contain lines
val strokeColor = props["stroke"]?.let { parseColor(it) }
val strokeWidth = props["stroke-width"]?.toFloatOrNull()
if (strokeColor != null || strokeWidth != null) {
Expand All @@ -102,7 +103,7 @@ object GeoJsonMapper {
)
} else null
}
is ModelPolygon -> {
geometry is ModelPolygon || geometry is MultiGeometry -> {
val strokeColor = props["stroke"]?.let { parseColor(it) }
val strokeWidth = props["stroke-width"]?.toFloatOrNull()
val fillColor = props["fill"]?.let { parseColor(it) }
Expand All @@ -125,7 +126,7 @@ object GeoJsonMapper {
)
} else null
}
is PointGeometry -> {
geometry is PointGeometry -> {
// TODO: Marker styling (marker-color, marker-size, marker-symbol)
null
}
Expand All @@ -136,6 +137,14 @@ object GeoJsonMapper {
return Feature(geometry, style = style, properties = finalProperties)
}

/**
* True for a Polygon or a multi-geometry whose members are all (recursively) polygons — i.e. a
* GeoJSON MultiPolygon — which per the simplestyle-spec carries fill styling rather than line styling.
*/
private fun Geometry.isPolygonal(): Boolean =
this is ModelPolygon ||
(this is MultiGeometry && geometries.isNotEmpty() && geometries.all { it.isPolygonal() })

private fun parseColor(colorString: String): Int? {
if (colorString.startsWith("#")) {
// Handle hex color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,94 @@ class GeoJsonStylingTest {
// Verify stroke width
assertEquals(3.0f, style.width!!, 0.001f)
}

@Test
fun `parse styled multipolygon applies polygon style`() {
val geoJson =
"""
{
"type": "Feature",
"properties": {
"stroke": "#ff0000",
"stroke-width": 2.0,
"fill": "#00ff00",
"fill-opacity": 0.25
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 0.0]
]
],
[
[
[102.0, 2.0],
[103.0, 2.0],
[103.0, 3.0],
[102.0, 2.0]
]
]
]
}
}
""".trimIndent()

val parser = GeoJsonParser()
val geoJsonObject = parser.parse(ByteArrayInputStream(geoJson.toByteArray()))!!
val layer = GeoJsonMapper.toLayer(geoJsonObject)
val feature = layer.features.first()

// A MultiPolygon must carry polygon styling (fill), not line styling.
assertTrue(feature.style is PolygonStyle)
val style = feature.style as PolygonStyle

assertEquals(0xFFFF0000.toInt(), style.strokeColor)
assertEquals(2.0f, style.strokeWidth, 0.001f)

// Fill: green with 0.25 opacity (0.25 * 255 = 63 = 0x3F)
val expectedFillColor = (0x3F shl 24) or 0x00FF00
assertEquals(expectedFillColor, style.fillColor)
}

@Test
fun `parse styled multilinestring keeps line style`() {
val geoJson =
"""
{
"type": "Feature",
"properties": {
"stroke": "#0000ff",
"stroke-width": 3.0
},
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[100.0, 0.0],
[101.0, 1.0]
],
[
[102.0, 2.0],
[103.0, 3.0]
]
]
}
}
""".trimIndent()

val parser = GeoJsonParser()
val geoJsonObject = parser.parse(ByteArrayInputStream(geoJson.toByteArray()))!!
val layer = GeoJsonMapper.toLayer(geoJsonObject)
val feature = layer.features.first()

assertTrue(feature.style is LineStyle)
val style = feature.style as LineStyle
assertEquals(0xFF0000FF.toInt(), style.color)
assertEquals(3.0f, style.width!!, 0.001f)
}
}