PrefetchToFolder fails when loading an image as map

Hi There!
I’m trying to use an image as map. This is my code:

Map1.Projection = PROJECTION_NONE
Map1.TileProvider = ProviderNone

Debug.Print Map1.VersionNumber
Dim Ext As New Extents

Ext.SetBounds Lon0, Lat0, 0, Lon1, Lat1, 10
Map1.Tiles.PrefetchToFolder Ext, 1, 0, App.Path & "\mapa", ".png", Nothing

Set Ext = Nothing

Map1.Latitude = Lat0
Map1.Longitude = Lon0
Map1.CurrentZoom = 1

The error shows:
-1 , Error in ‘PrefetchToFolder’ method of ‘ITiles’ object
Fails if the ProviderID = 0

What I’m doing wrong?

You’re not setting the projection nor the tile provider so the PrefetchToFolder method doesn’t know what to do.

Hi Paul! very thank you for your response. I was playing with both variables when this error raises.

Like this:
Map1.TileProvider = ProviderCustom

I try this too:

Map1.TileProvider = ProviderCustom
Debug.Print Map1.VersionNumber

Dim Ext As New Extents

        'Ext.SetBounds X0, Y1, 0, X1, Y1, 10
        Ext.SetBounds Lon0, Lat0, 0, Lon1, Lat1, 10
        Map1.Tiles.PrefetchToFolder Ext, 10, ProviderCustom, Mapfile, "jpg", Nothing

With ProviderCustom reference in both variables shows a OpenStreetMap
The error raise when the provider ID is set to 0

I try playing with that and I get the same error.
The map is only one image. If the method try to get a file collection it will never works.

Best Regards!

You need to set a proper TileProvider or else it doesn’t work.

Here’s an example we used in an old unit test:

        private void Run()
            var latLongExtents = new Extents();
            latLongExtents.SetBounds(3.3700, 50.7500, 0, 7.2100, 53.4700, 0); // The Netherlands in WGS84:

            Helper.DebugMsg("PrefetchToFolderDutchOSM Maxzoom: 11");
            PrefetchToFolder(tkTileProvider.OpenStreetMap, 5, latLongExtents);

        private void PrefetchToFolder(tkTileProvider tileProvider, int maxZoom, Extents latLongExtents)
            // Set tiles provider:
            _axMap1.Tiles.Provider = tileProvider;
            _axMap1.Tiles.GlobalCallback = this;

            DebugMsg("Tiles projection status: " + _axMap1.Tiles.ProjectionStatus);
            DebugMsg("_axMap1.Tiles.ProjectionIsSphericalMercator: " + _axMap1.Tiles.ProjectionIsSphericalMercator);

            var outputFolder = $@"D:\tmp\axmap.tiles\{_axMap1.Tiles.Provider.ToString()}";
            if (!Directory.Exists(outputFolder)) Directory.CreateDirectory(outputFolder);

            var providerId = Convert.ToInt32(tileProvider);
            var numRounds = 0; // To prevent endless loops
            var maximizedZoom = Math.Min(maxZoom, _axMap1.Tiles.MaxZoom); // Prevent asking higher zoom than allowed

            var stopWatch = new Stopwatch();
            while (true)
                var numTilesToCache = 0;
                for (var i = _axMap1.Tiles.MinZoom; i < maximizedZoom; i++)
                    numTilesToCache +=
                        _axMap1.Tiles.PrefetchToFolder(latLongExtents, i, providerId, outputFolder, ".png", this);
                    DebugMsg($"numTilesToCache: {numTilesToCache} for zoom {i}");
                    // Wait a moment:
                DebugMsg($"Finished round {numRounds}");

                // Wait before doing another round:
                if (numTilesToCache > 0) Thread.Sleep(2000);
                if (numTilesToCache == 0 || numRounds > 9) break;

            Helper.DebugMsg("Time it took: " + stopWatch.Elapsed);

Some interesting links:

Hi Paul!

What i’m trying to do is use one image from a sector as map, so i’m think I must set in the world-view where goes my image.
What I’m seeing in that example code is how to cache an particular map portion from some lat&lon positions.
It will work off-line
I’m working with the cache control to use not a SQL server so the example will help me with that :wink:

Best Regards!!

Hi Paul!
I wrote this code into Basic and works great, I was missing a /in the folder path.
I have all the images in every folder index by zoom level.
Now, how in the name of god :rofl: how these files will rebuild the map?

I set :

Map1.Tiles.DoCaching(RAM) = False
Map1.Tiles.UseCache(RAM) = False
Map1.Tiles.DoCaching(Disk) = True
Map1.Tiles.UseCache(Disk) = True
Map1.Tiles.MaxCacheSize(Disk) = 1000

I need a SQL server rigth?

Best Regarsd!

Perhaps we first need to determine what you want to do because I don’t understand it yet.

You have an image file. What format? Tiff, ECW.
Next you open this image file in MapWinGIS using AddLayerFromFilename(), right?
So why do you want to prefetch the files? That is meant for online tile services like OSM or Bing.

If you want to speed up the rendering of the image and the image does not have overviews, you can create them using GdalUtils.GdalBuildOverviews()

Hi Pau!. I’m sorry I will be more clear.
Only two things, the first one is open an image and use it as map (your last response is what I need for this)

And the second one is set-up and turn-on local cache to use the map in off-line mode. (I’m thinking for this I need a SQL server, cache config like my last message and prefect files ?)

Best Regards!

First try:

Image = App.path & "\mymap.bmp"
Dim pic As New MapWinGIS.Image
pic.Open Image, BITMAP_FILE
pic.Extents.SetBounds Lon0, Lat0, 10, Lon1, Lat1, 10
Map1.AddLayer pic, True


Image = App.path & "\mymap.bmp"
Map1.AddLayerFromFilename Imagen, fosAutoDetect, True
Map1.Extents.SetBounds Lon0, Lat0, 10, Lon1, Lat1, 10
Map1.Latitude = Lat0
Map1.Longitude = Lon0
Map1.CurrentZoom = 10


Result: Map shows correct coordinates but not the image.

What I’ missing?

Hi Paul, please close this issue I fix it finally.