Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for offline downloading of tilesets #3633

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -51,6 +51,7 @@ class TileRegionPack(var name: String, var state: TileRegionPackState = TileRegi

// stored in metadata for resume functionality
var styleURI: String? = null
var tilesets: List<String> = emptyList()
var bounds: Geometry? = null
var zoomRange: ZoomRange? = null

Expand Down Expand Up @@ -79,17 +80,20 @@ class TileRegionPack(var name: String, var state: TileRegionPackState = TileRegi
name: String,
state: TileRegionPackState = TileRegionPackState.UNKNOWN,
styleURI: String,
tilesets: List<String>,
bounds: Geometry,
zoomRange: ZoomRange,
metadata: JSONObject
) : this(name= name, state= state,progress= null, metadata= metadata) {
) : this(name= name, state= state, progress= null, metadata= metadata) {
val rnmeta = JSONObject()
rnmeta.put("styleURI", styleURI)
this.styleURI = styleURI
rnmeta.put("bounds", bounds.toJSONObject())
this.bounds = bounds
rnmeta.put("zoomRange", JSONArray(arrayOf(zoomRange.minZoom, zoomRange.maxZoom)))
this.zoomRange = zoomRange
rnmeta.put("tilesets", JSONArray(tilesets))
this.tilesets = tilesets
this.metadata.put(RNMapboxInfoMetadataKey, rnmeta);
}
}
Expand Down Expand Up @@ -143,9 +147,11 @@ class RNMBXOfflineModule(private val mReactContext: ReactApplicationContext) :
val boundsFC = FeatureCollection.fromJson(boundsStr)
val bounds = convertPointPairToBounds(boundsFC)

val tilesets = options.getArray("tilesets")?.toArrayList()?.map { it as String } ?: emptyList()
val actPack = TileRegionPack(
name = id,
styleURI = options.getString("styleURL")!!,
tilesets = tilesets,
bounds = bounds,
zoomRange = ZoomRange(
minZoom = options.getInt("minZoom").toByte(),
Expand Down Expand Up @@ -280,30 +286,25 @@ class RNMBXOfflineModule(private val mReactContext: ReactApplicationContext) :
?: return Result.failure(IllegalArgumentException("startLoading failed as there is no styleURI in pack"))
val metadata = pack.metadata
?: return Result.failure(IllegalArgumentException("startLoading failed as there is no metadata in pack"))

val stylePackOptions = StylePackLoadOptions.Builder()
.glyphsRasterizationMode(GlyphsRasterizationMode.IDEOGRAPHS_RASTERIZED_LOCALLY)
.metadata(metadata.toMapboxValue())
.build()

val descriptorOptions = TilesetDescriptorOptions.Builder()
.styleURI(styleURI)
.minZoom(zoomRange.minZoom)
.maxZoom(zoomRange.maxZoom)
.stylePackOptions(stylePackOptions)
.pixelRatio(2.0f)
.build()
val tilesetDescriptor = offlineManager.createTilesetDescriptor(descriptorOptions)

val descriptors = getTilesetDescriptors(
offlineManager = offlineManager,
styleURI = styleURI,
minZoom = zoomRange.minZoom,
maxZoom = zoomRange.maxZoom,
stylePackOptions = stylePackOptions,
tilesets = pack.tilesets)
val loadOptions = TileRegionLoadOptions.Builder()
.geometry(bounds)
.descriptors(arrayListOf(tilesetDescriptor))
.descriptors(descriptors)
.metadata(metadata.toMapboxValue())
.acceptExpired(true)
.networkRestriction(NetworkRestriction.NONE)
.averageBytesPerSecond(null)
.build()

var lastProgress: TileRegionLoadProgress? = null
val task = this.tileStore.loadTileRegion(
id, loadOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import com.mapbox.common.toValue
import com.mapbox.maps.OfflineManager
import com.mapbox.maps.OfflineRegionManager
import com.mapbox.maps.ResourceOptions
import com.mapbox.maps.StylePackLoadOptions
import com.mapbox.maps.TilesetDescriptorOptions
import com.mapbox.maps.TilesetDescriptorOptionsForTilesets
import com.mapbox.common.TilesetDescriptor

fun getOfflineRegionManager(getAccessToken: () -> String): OfflineRegionManager {
return OfflineRegionManager(ResourceOptions.Builder().accessToken(getAccessToken()).build())
Expand All @@ -20,6 +24,28 @@ fun getOfflineManager(tileStore: TileStore, getAccessToken: () -> String): Offli
)
}

fun getTilesetDescriptors(offlineManager: OfflineManager, styleURI: String, minZoom: Byte, maxZoom: Byte, stylePackOptions: StylePackLoadOptions, tilesets: List<String>): ArrayList<TilesetDescriptor>{
val descriptorOptions = TilesetDescriptorOptions.Builder()
.styleURI(styleURI)
.minZoom(minZoom)
.maxZoom(maxZoom)
.stylePackOptions(stylePackOptions)
.pixelRatio(2.0f)
.build()
val descriptor = offlineManager.createTilesetDescriptor(descriptorOptions)
val tilesetDescriptorOptions = TilesetDescriptorOptionsForTilesets.Builder()
.tilesets(tilesets)
.minZoom(minZoom)
.maxZoom(maxZoom)
.build()
val tilesetDescriptor = offlineManager.createTilesetDescriptor(tilesetDescriptorOptions)
val descriptors = arrayListOf(descriptor)
if (tilesets.isNotEmpty()) {
descriptors.add(tilesetDescriptor)
}
return descriptors
}

fun TileStore.setAccessToken(token: String) {
this.setOption(TileStoreOptions.MAPBOX_ACCESS_TOKEN, token.toValue());
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,45 @@ import com.mapbox.common.MapboxOptions
import com.mapbox.common.TileStore
import com.mapbox.maps.OfflineManager
import com.mapbox.maps.OfflineRegionManager
import com.mapbox.maps.StylePackLoadOptions
import com.mapbox.maps.TilesetDescriptorOptions
import com.mapbox.common.TilesetDescriptor

fun getOfflineRegionManager(getAccessToken: () -> String): OfflineRegionManager {
return OfflineRegionManager()
}

fun getOfflineManager(tileStore: TileStore, getAccessToken: () -> String): OfflineManager {
return OfflineManager()
}

fun getTilesetDescriptors(offlineManager: OfflineManager, styleURI: String, minZoom: Byte, maxZoom: Byte, stylePackOptions: StylePackLoadOptions, tilesets: List<String>): ArrayList<TilesetDescriptor>{
if (tilesets.isNotEmpty()) {
val descriptorOptions = TilesetDescriptorOptions.Builder()
.styleURI(styleURI)
.minZoom(minZoom)
.maxZoom(maxZoom)
// TODO: When tilesets is passed in the mappack doesn't save -- not sure why
// .tilesets(tilesets)
.stylePackOptions(stylePackOptions)
.pixelRatio(2.0f)
.build()
val descriptor = offlineManager.createTilesetDescriptor(descriptorOptions)
val descriptors = arrayListOf(descriptor)
return descriptors
}
val descriptorOptions = TilesetDescriptorOptions.Builder()
.styleURI(styleURI)
.minZoom(minZoom)
.maxZoom(maxZoom)
.stylePackOptions(stylePackOptions)
.pixelRatio(2.0f)
.build()
val descriptor = offlineManager.createTilesetDescriptor(descriptorOptions)
val descriptors = arrayListOf(descriptor)
return descriptors
}

fun TileStore.setAccessToken(token: String) {
MapboxOptions.accessToken = token
}
2 changes: 1 addition & 1 deletion example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ org.gradle.jvmargs=-Xmx2560m -XX:MaxMetaspaceSize=1024m
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true

RNMBX11=false
RNMBX11=true

# Use this property to specify which architecture you want to build.
# You can also override it from the CLI using
Expand Down
Loading
Loading