Skip to content

Commit

Permalink
Remove offline map working
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonash committed Sep 5, 2018
1 parent 17a7251 commit c3b282c
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 54 deletions.
56 changes: 54 additions & 2 deletions www/app/maps/map/map-layer.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

// Create a map baselayer or overlay
function createTileLayer(layer, isOnline) {
$log.log('createTileLayer Layer: ',layer);
var isVisible = getLayerVisibility(layer);
var newMapLayer = new ol.layer.Tile();
newMapLayer.setProperties({
Expand Down Expand Up @@ -309,7 +310,8 @@

function setOfflineSource(layer) {
if (!layer.source) return new ol.source.XYZ({'url': ''}); // No basemap layer
return new ol.source.OSM({'tileLoadFunction': tileLoadFunction(layer.id)});
return new ol.source.OSM({'tileLoadFunction': tileLoadFunction(layer.mapid)});
//return new ol.source.OSM({'tileLoadFunction': tileLoadFunction(layer.id)}); //jma 09042019
}

function setOnlineOverlays() {
Expand Down Expand Up @@ -368,6 +370,37 @@
};
}

// tileLoadFunction is used for offline access mode, required by OL3 for specifying how tiles are retrieved
function oldtileLoadFunction(mapProvider) {
return function (imageTile) {
var imgElement = imageTile.getImage(); // the tile we will be loading

/* ToDo: What is going on with get the image coords? x is returning a negative number for low zooms (0-2)
/*var imageCoords = imageTile.getTileCoord(); // the tile coordinates (x,y,z)
var y = (imageCoords[2] * -1) - 1; // y needs to be corrected using (-y - 1)
var z = imageCoords[0];
var x = imageCoords[1];*/

// Switched to this method to get x, y, z since above method having trouble at low zooms
var regex = /\/(\d*)\/(\d*)\/(\d*)\.png/g;
var match = regex.exec(imageTile.src_);
var z = match[1];
var x = match[2];
var y = match[3];

var tileId = z + '/' + x + '/' + y;
imgElement.id = tileId;
//$log.log('Looking for offline tile from mapProvider', mapProvider, 'title:', tileId);
getTile(mapProvider, tileId).then(function (blob) {
//$log.log('Found original tile:', tileId, 'Loading ...');
loadTile(blob, imgElement);
}, function () {
//$log.log('Tile not found:', tileId, 'Attempting to create a tile ...');
getSubstituteTile(mapProvider, imgElement, x, y, z - 1, 2);
});
};
}

// Update layer visibility after a layer 'change:visible' event
function updateLayerVisibility(event, layer) {
var layerProperties = event.target.getProperties();
Expand All @@ -391,10 +424,15 @@
}

// Make sure the visible layers are only layers that are offline layers
function updateVisibleLayersForOffline(mapLayers) {
function updateVisibleLayersForOffline(mapLayers) { //also insert mapid?
var deferred = $q.defer(); // init promise

$log.log('updateVisibleLayersForOffline: ', mapLayers);

OfflineTilesFactory.getOfflineMaps().then(function (offlineMaps) {

$log.log('offlineMaps: ', offlineMaps);

// If the visible baselayer is not an offline layer set visible baselayer to empty
if (!_.isEmpty(visibleLayers.baselayer) &&
!_.chain(offlineMaps).pluck('id').compact().contains(visibleLayers.baselayer.get('id')).value()) {
Expand All @@ -411,6 +449,20 @@
var offlineMapLayers = _.filter(mapLayers, function (mapLayer) {
return _.chain(offlineMaps).pluck('id').compact().contains(mapLayer.id).value();
});

//get mapid to pass to each offline layer
var returnMaps = [];
_.each(offlineMapLayers, function(offlineMapLayer){
_.each(offlineMaps, function(omap){
if(omap.id == offlineMapLayer.id){
offlineMapLayer.mapid = omap.mapid;
returnMaps.push(offlineMapLayer);
}
});
});

$log.log('returnMaps: ', returnMaps);
$log.log('update visible offLineMapLayers: ', offlineMapLayers);
deferred.resolve(offlineMapLayers);
});
return deferred.promise;
Expand Down
1 change: 1 addition & 0 deletions www/app/maps/map/map.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
function setMaps() {
// Load Default Maps
maps = angular.fromJson(angular.toJson(getDefaultMaps()));
$log.log('maps from SetMaps: ', maps);
_.each(maps, function (map, i) {
maps[i] = _.extend(maps[i], getMapProviderInfo(map.source));
if (map.source === 'strabo_spot_mapbox') maps[i].key = defaultMapboxKey;
Expand Down
8 changes: 6 additions & 2 deletions www/app/maps/offline-maps/archive-tiles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
}

function continueDownload() {
$log.log('continueDownload: ', vm.map);
OfflineTilesFactory.checkValidMapName(vm.map).then(function () {

vm.downloadingModal.show().then(function () {
Expand Down Expand Up @@ -441,8 +442,8 @@ strabo mymaps: http://devtiles.strabospot.org/zip?layer=strabomymaps&id=5b75967d
var key = mapToSave.key;
var parts = id.split('/');
var mapusername = parts[0];
var mapid = parts[1];
startZipURL=vm.tilehost+'/asynczip?mapid='+mapid+'&layer=mapboxstyles&username='+mapusername+'&access_token='+key+'&id='+mapid+'&extent='+extentString+'&zoom='+vm.selectedMaxZoom.zoom;
var usermapid = parts[1];
startZipURL=vm.tilehost+'/asynczip?mapid='+mapid+'&layer=mapboxstyles&username='+mapusername+'&access_token='+key+'&id='+usermapid+'&extent='+extentString+'&zoom='+vm.selectedMaxZoom.zoom;
}else if(source=='strabospot_mymaps') {
startZipURL=vm.tilehost+'/asynczip?mapid='+mapid+'&layer=strabomymaps&id='+id+'&extent='+extentString+'&zoom='+vm.selectedMaxZoom.zoom;
}else if(source=='map_warper') {
Expand All @@ -451,6 +452,8 @@ strabo mymaps: http://devtiles.strabospot.org/zip?layer=strabomymaps&id=5b75967d

if(startZipURL!=''){

$log.log('startZipURL: ', startZipURL);

var request = $http({
'method': 'get',
'url': startZipURL
Expand Down Expand Up @@ -536,6 +539,7 @@ strabo mymaps: http://devtiles.strabospot.org/zip?layer=strabomymaps&id=5b75967d


function saveMap(mapToSave) {
$log.log('just inside saveMap: ', mapToSave);
var deferred = $q.defer(); // init promise

vm.map.progress.message = 'Starting Download...';
Expand Down
48 changes: 9 additions & 39 deletions www/app/maps/offline-maps/offline-map.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,55 +134,25 @@
}

function goToMap(map) {
/*

$ionicLoading.show({
'template': '<ion-spinner></ion-spinner>'
});
*/

//fix this to work with file system.

// Check the first 30 tiles and zoom to the tile with the highest zoom

OfflineTilesFactory.getMapCenter(map.mapid).then(function (center) {
$log.log('gotcenter: ', center);
});

/*
if (numTilesToCheck <= 0) {
$ionicPopup.alert({
'title': 'Not Enough Tiles!',
'template': 'More tiles need to be added to this map before attempting to view it.'
});
$ionicLoading.hide();
}
else {
var x, y, z = -1; // Start zoom at -1 so a 0 zoom with meet the condition testZ > z
var pattern = /(\d+)\/(\d+)\/(\d+)/; // Format "15/6285/13283"
for (var i = 0; i < numTilesToCheck; i++) {
var tileNameParts = pattern.exec(map.tileArray[i].tile);
if (tileNameParts && tileNameParts.length === 4) {
var testZ = parseInt(tileNameParts[1]);
if (testZ > z) {
z = testZ;
x = parseInt(tileNameParts[2]);
y = parseInt(tileNameParts[3]);
}
}
}
if (x && y && z) {
OfflineTilesFactory.getMapCenterTile(map.mapid).then(function (centerTile) {
$log.log('gotcenterTile: ', centerTile);
if(centerTile) {
var parts = centerTile.split('_');
var z = Number(parts[0]);
var x = Number(parts[1]);
var y = Number(parts[2]);
var lng = SlippyTileNamesFactory.tile2long(x, z);
var lat = SlippyTileNamesFactory.tile2lat(y, z);
MapLayerFactory.setVisibleBaselayer(map.id);
MapViewFactory.zoomToPoint([lng, lat], z);
$location.path('/app/map');
}
}
*/




});
}


Expand Down
31 changes: 27 additions & 4 deletions www/app/maps/offline-maps/offline-tile.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'clear': clear,
'deleteMap': deleteMap,
'downloadZip': downloadZip,
'getMapCenter': getMapCenter,
'getMapCenterTile': getMapCenterTile,
'getOfflineMaps': getOfflineMaps,
'getOfflineTileCount': getOfflineTileCount,
'read': read,
Expand Down Expand Up @@ -182,6 +182,8 @@

function deleteMap(mapToDelete) {
var deferred = $q.defer();

/*
var promises = [];
var tiles = mapToDelete.tileArray;
var tilesStillUsed = [];
Expand All @@ -203,17 +205,24 @@
});
if (!found) promises.push(deleteTile(mapToDelete.id + '/' + tile.tile));
});
*/

// All tiles associated with this maps name and not used in another maps have been deleted
// now delete the actual map name if there were actually any tiles deleted
$q.all(promises).then(function () {

//new localstorage function here with then
LocalStorageFactory.deleteMapFiles(mapToDelete).then(function() {
//$q.all(promises).then(function () {
LocalStorageFactory.getDb().mapNamesDb.removeItem(mapToDelete.name).then(function () {
// Map is deleted, and this is now fully resolved
offlineMaps = _.reject(offlineMaps, function (offlineMap) {
return offlineMap.name === mapToDelete.name;
});
deferred.resolve();
});
},function(){
$log.log('LocalStorageFactory.deleteMapFiles faild to resolve.');
deferred.resolve();
});
return deferred.promise;
}
Expand Down Expand Up @@ -259,8 +268,8 @@
}

// Get the center of the map based on offline tiles
function getMapCenter(mapid) {
return LocalStorageFactory.getMapCenter(mapid);
function getMapCenterTile(mapid) {
return LocalStorageFactory.getMapCenterTile(mapid);
}

// Get the number of tiles from offline maps
Expand Down Expand Up @@ -293,6 +302,20 @@

// Read from storage
function read(mapProvider, tile, callback) {
$log.log('MapProvider: ', mapProvider);
$log.log('tile: ', tile);
tile=tile.replace(/\//g,'_');
$log.log('tile: ', tile);
var tileId = tile + '.png';
$log.log('tileId: '+ tileId);
LocalStorageFactory.getTile(mapProvider, tileId).then(function (blob) {
callback(blob);
});
}

// Read from storage
function oldread(mapProvider, tile, callback) {
$log.log(mapProvider);
var tileId = mapProvider + '/' + tile;
LocalStorageFactory.getDb().mapTilesDb.getItem(tileId).then(function (blob) {
callback(blob);
Expand Down
Loading

0 comments on commit c3b282c

Please sign in to comment.