Mapbox-gl-native: unable to open database when trying to merge a .db file

Created on 19 Dec 2018  路  4Comments  路  Source: mapbox/mapbox-gl-native

Hello!

I'm trying to merge a .db file that contains an offline region. I'm finding this error when trying to merge it.

unable to open database: /storage/emulated/0/granada_offline_map.db

These are the steps I'm following

Steps to reproduce

1.- Obtain the offline map using this sample code:

    // Create a bounding box for the offline region
    LatLngBounds latLngBounds = new LatLngBounds.Builder()
            .include(new LatLng(37.1784930, -3.5921612)) // Northeast
            .include(new LatLng(37.154457, -3.572780)) // Southwest
            .build();

    // Define the offline region
    OfflineTilePyramidRegionDefinition definition = new OfflineTilePyramidRegionDefinition(
            map.getStyleUrl(),
            latLngBounds,
            10,
            20,
            getResources().getDisplayMetrics().density);

    byte[] metadata;
    try {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(JSON_FIELD_REGION_NAME, "Granada");
        String json = jsonObject.toString();
        metadata = json.getBytes(JSON_CHARSET);
    } catch (Exception exception) {
        Log.e(LOG_TAG, "Failed to encode metadata: " + exception.getMessage());
        metadata = null;
    }

    // Create the region asynchronously
    offlineManager.createOfflineRegion(definition, metadata,
            new OfflineManager.CreateOfflineRegionCallback() {
                @Override
                public void onCreate(OfflineRegion offlineRegion) {
                    offlineRegion.setDownloadState(OfflineRegion.STATE_ACTIVE);

                    // Monitor the download progress using setObserver
                    offlineRegion.setObserver(...);
                }

                @Override
                public void onError(String error) {
                    Log.e(LOG_TAG, "Error: " + error);
                }
            });

2.- Copy the Mapbox database from my Android device to computer, using this command

adb -d shell "run-as com.appandweb.prjmapbox cat /data/data/com.appandweb.prjmapbox/files/mbgl-offline.db" > granada_offline_map.db

3.- Uninstall + reinstall the App

adb uninstall com.appandweb.prjmapbox

4.- Copy the map to the phone sdcard, using this command

adb push granada_offline_map.db /sdcard/

5.- Disable internet and launch the App again. Try to re-import the offline map using this code:

private void mergeOfflineRegions() {
    File offlineMap = new File(Environment.getExternalStorageDirectory(), "granada_offline_map.db");
    String path = offlineMap.getAbsolutePath();
    offlineManager.mergeOfflineRegions(path, new OfflineManager.MergeOfflineRegionsCallback() {
        @Override
        public void onMerge(OfflineRegion[] offlineRegions) {
            Log.i(LOG_TAG, String.format("Merged: %d regions", offlineRegions.length));
        }

        @Override
        public void onError(String error) {
            Log.e(LOG_TAG, "Error merging regions: " + error);
        }
    });
}

I can't figure out why the .db file is not being imported.

I've uploaded the .db file to Google Drive as well as the code from the sample project I'm using, if this helps finding the cause

.db file: https://drive.google.com/open?id=1qZ1M5h-PFab_8Q_V0oqK1KhsTQjm9qfV
Sample project: https://drive.google.com/open?id=1fmM_SUygAcAnKM_pU43U1YNNYlL9GLHP

(Note: This issue is similar to #6193, but as suggested by @zugaldia on it, I prefer to open a separate ticket rather than adding more comments to #6193, which is closed)

<3 Mapbox!

Expected behavior

I'd expect to have my offline regions imported

Actual behavior

unable to open database: /storage/emulated/0/granada_offline_map.db

Configuration

Android versions:
7.1.1, 7.1.0, 4.4
Device models:
Xiaomi Redmi 4 (7.1.1), emulator (7.1), BQ Aquaris 5 HD (4.4)

Mapbox SDK versions:
I've tried 6.7.1 and 6.7.2

Android

Most helpful comment

Hello,

I finally managed to generate the offline .db map using CLI, in a friend's computer. The mergeOfflineRegions() method is now importing the region successfully

The command we used is

./mbgl-offline --north 37.1784930 --west -3.572780 --south 37.154457 --east -3.5921612 --minZoom 16 --maxZoom 18 --output ~/granada_offline_map.db --style mapbox://styles/mapbox/streets-v9 --token <my_mapbox_token>

thanks @zugaldia @LukasPaczos for your help :-) the issue can be now closed

All 4 comments

cc @Guardiola31337 @zugaldia

Thanks for reaching out @voghDev! The offline merge API was designed to operate with externally prepared databases, could you try following the sideloading documentation and let us know whether it worked for you?

Please bear in mind, that if you are going to use the CLI tool, you'll need to check out the repository on the release commit equivalent to the version you are trying to use. In other words, to generate an offline database compatible with the 6.7.2, you'll need to check out 6.7.2 release commit and then execute the generation step.

On the other hand, @asheemmamoowala should offline merge API work with the regions downloaded in the runtime and reused later, as described above?

Thanks for the prompt response. So in order to run the mergeOfflineRegions() method mentioned above, the input database must be generated with the CLI command line tool?

I'll try to compile mapbox-gl-native and use a file generated with it :) will post any progress that I achieve

Hello,

I finally managed to generate the offline .db map using CLI, in a friend's computer. The mergeOfflineRegions() method is now importing the region successfully

The command we used is

./mbgl-offline --north 37.1784930 --west -3.572780 --south 37.154457 --east -3.5921612 --minZoom 16 --maxZoom 18 --output ~/granada_offline_map.db --style mapbox://styles/mapbox/streets-v9 --token <my_mapbox_token>

thanks @zugaldia @LukasPaczos for your help :-) the issue can be now closed

Was this page helpful?
0 / 5 - 0 ratings