/// <summary> /// Returns a layer for the basemap according to that chosen by the user and saved in settings. If offline and an associated mbtiles file is present, the mbtiles layer is returned. /// </summary> /// <returns></returns> public static MapLayer GetBaseMap() { var dirPath = Path.Combine(App.TileLocation, "mbtiles"); var BL = Preferences.Get("BaseLayer", "swisstopo_pixelkarte"); bool noInternet = MapModel.IsAppDisconnected(); var wmtsLayer = WMSLayer.CreatePixelkarteWMTSTileLayer(); if (BL == "osm") { wmtsLayer = OpenStreetMap.CreateTileLayer(); } else if (BL == "swissimage") { wmtsLayer = WMSLayer.CreateSwissimageWMTSTileLayer(); } var baseLayer = new MapLayer(true, 0, wmtsLayer); if (noInternet) { var filename = "swisstopo_pixelkarte.mbtiles"; if (BL == "osm") { filename = "osm.mbtiles"; } else if (BL == "swissimage") { filename = "swissimage.mbtiles"; } foreach (var file in System.IO.Directory.GetFiles(dirPath)) { if (file.EndsWith(filename)) { var offlineLayer = WMSLayer.CreateOfflineLayer(file) as TileLayer; baseLayer = new MapLayer(true, 0, offlineLayer); return(baseLayer); } } } return(baseLayer); }
/// <summary> /// Creates a dictionary of the offline layers available at the file path /// </summary> /// <param name="dirPath"></param> /// <returns>Dictionary of offline layers</returns> public static Dictionary <string, ILayer> GetOfflineLayers(string dirPath) { //Create a dictionary of all the offline files available var offlineLayers = new Dictionary <string, ILayer>(); foreach (var file in System.IO.Directory.GetFiles(dirPath)) { if (file.EndsWith(".mbtiles")) { try { var offlineLayer = WMSLayer.CreateOfflineLayer(file); if (!offlineLayers.Keys.Contains(offlineLayer.Name)) { offlineLayers.Add(offlineLayer.Name, offlineLayer); } } catch { } } } return(offlineLayers); }
//Mapserver layers public ObservableCollection <MapLayer> MakeArrayOfLayers() { int i = 0; //Add mbtiles layers var dirPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "mbtiles"); //Create directory if it doesn't exist if (!File.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } //Create a dictionary of all the offline files available var offlineLayers = new Dictionary <string, ILayer>(); foreach (var file in System.IO.Directory.GetFiles(dirPath)) { if (file.EndsWith(".mbtiles")) { var offlineLayer = WMSLayer.CreateOfflineLayer(file); offlineLayers.Add(offlineLayer.Name, offlineLayer); } } //Get the online layers var layers = GetLayersForMap(App.CurrentProjectId); //Create an array for adding the layers into in an ordered fashion var mapLayersTemp = new MapLayer[layers.Count]; //Add online wms layers var layerStack = layers.OrderBy(o => o.order).ToList(); foreach (var layer in layerStack) { //Now add the layers in their correct order try { var layerNo = Math.Max(layers.Count - layer.order, 0); var layerWms = WMSLayer.CreateWMSLayer(layer.url, layer.wmsLayer, "EPSG:3857", layer.title); layerWms.Opacity = layer.opacity; layerWms.Enabled = layer.visible; if (Connectivity.NetworkAccess != NetworkAccess.Internet && offlineLayers.Keys.Contains(layerWms.Name)) { //If no internet, check for saved tiles ILayer offlineLayer; offlineLayers.TryGetValue(layerWms.Name, out offlineLayer); if (offlineLayer != null) { var WmsLayer = new MapLayer(true, 0, offlineLayer); WmsLayer.Opacity = layerWms.Opacity; WmsLayer.Enabled = layerWms.Enabled; WmsLayer.LayerZ = layer.order; WmsLayer.Name = layer.title; mapLayersTemp.SetValue(WmsLayer, layerNo); i++; } } else { //If internet, read directly from WMS if (layerWms != null) { var WmsLayer = new MapLayer(true, 0, layerWms); WmsLayer.Opacity = layerWms.Opacity; WmsLayer.Enabled = layerWms.Enabled; WmsLayer.LayerZ = layer.order; WmsLayer.Name = layer.title; mapLayersTemp.SetValue(WmsLayer, layerNo); i++; } } } catch (Exception e) { Console.WriteLine(e); } } var mapLayersTempList = mapLayersTemp.ToList().GetRange(0, i); return(new ObservableCollection <MapLayer>(mapLayersTempList as List <MapLayer>)); }