Skip to content

Commit

Permalink
fix crash on parsing URLs with templating (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
sargunv authored Jan 12, 2025
1 parent 206671f commit fad1b99
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal class AndroidMap(
lastStyleUri = styleUri
logger?.i { "Setting style URI" }
callbacks.onStyleChanged(this, null)
val builder = MlnStyle.Builder().fromUri(styleUri.correctedAndroidUri().toString())
val builder = MlnStyle.Builder().fromUri(styleUri.correctedAndroidUri())
map.setStyle(builder) {
logger?.i { "Style finished loading" }
callbacks.onStyleChanged(this, AndroidStyle(it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import dev.sargunv.maplibrecompose.core.util.toMLNExpression
import dev.sargunv.maplibrecompose.expressions.ExpressionContext
import dev.sargunv.maplibrecompose.expressions.dsl.const
import io.github.dellisd.spatialk.geojson.GeoJson
import java.net.URI
import org.maplibre.android.style.sources.GeoJsonOptions as MLNGeoJsonOptions
import org.maplibre.android.style.sources.GeoJsonSource as MLNGeoJsonSource

public actual class GeoJsonSource : Source {
override val impl: MLNGeoJsonSource

public actual constructor(id: String, uri: String, options: GeoJsonOptions) {
impl = MLNGeoJsonSource(id, uri.correctedAndroidUri(), buildOptionMap(options))
impl = MLNGeoJsonSource(id, URI(uri.correctedAndroidUri()), buildOptionMap(options))
}

public actual constructor(id: String, data: GeoJson, options: GeoJsonOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import org.maplibre.android.style.sources.RasterSource

public actual class RasterSource actual constructor(id: String, uri: String, tileSize: Int) :
Source() {
override val impl: RasterSource = RasterSource(id, uri.correctedAndroidUri().toString(), tileSize)
override val impl: RasterSource = RasterSource(id, uri.correctedAndroidUri(), tileSize)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import dev.sargunv.maplibrecompose.core.util.correctedAndroidUri
import org.maplibre.android.style.sources.VectorSource

public actual class VectorSource actual constructor(id: String, uri: String) : Source() {
override val impl: VectorSource = VectorSource(id, uri.correctedAndroidUri().toString())
override val impl: VectorSource = VectorSource(id, uri.correctedAndroidUri())
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ import dev.sargunv.maplibrecompose.expressions.ast.StringLiteral
import io.github.dellisd.spatialk.geojson.BoundingBox
import io.github.dellisd.spatialk.geojson.Position
import java.net.URI
import java.net.URISyntaxException
import org.maplibre.android.geometry.LatLng
import org.maplibre.android.geometry.LatLngBounds
import org.maplibre.android.style.expressions.Expression as MLNExpression

internal fun String.correctedAndroidUri(): URI {
val uri = URI(this)
return if (uri.scheme == "file" && uri.path.startsWith("/android_asset/")) {
URI("asset://${uri.path.removePrefix("/android_asset/")}")
} else {
uri
internal fun String.correctedAndroidUri(): String {
return try {
val uri = URI(this)
if (uri.scheme == "file" && uri.path.startsWith("/android_asset/"))
URI("asset://${uri.path.removePrefix("/android_asset/")}").toString()
else this
} catch (_: URISyntaxException) {
this
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dev.sargunv.maplibrecompose.core.layer.Layer
import dev.sargunv.maplibrecompose.core.layer.LineLayer
import dev.sargunv.maplibrecompose.core.source.GeoJsonOptions
import dev.sargunv.maplibrecompose.core.source.GeoJsonSource
import dev.sargunv.maplibrecompose.core.source.VectorSource
import io.github.dellisd.spatialk.geojson.FeatureCollection
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand All @@ -20,7 +21,7 @@ import kotlin.test.assertNull
abstract class StyleNodeTest {
private val testSources by lazy {
listOf(
GeoJsonSource("foo", FeatureCollection(), GeoJsonOptions()),
VectorSource("foo", "https://example.com/{z}/{x}/{y}.pbf"),
GeoJsonSource("bar", FeatureCollection(), GeoJsonOptions()),
GeoJsonSource("baz", FeatureCollection(), GeoJsonOptions()),
)
Expand Down

0 comments on commit fad1b99

Please sign in to comment.