Beispiel #1
0
        //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>));
        }
Beispiel #2
0
        /// <summary>
        /// Create an array of map layers: either mbtiles layers if offline and the mbtiles file exists, or direct links to the map servers
        /// </summary>
        /// <returns>An observable collection of map layers</returns>
        public static ObservableCollection <MapLayer> MakeArrayOfLayers()
        {
            int i = 0;
            //Add mbtiles layers
            var dirPath = Path.Combine(App.TileLocation, "mbtiles");

            //Create directory if it doesn't exist
            if (!File.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            //Get the offline layers
            var offlineLayers = GetOfflineLayers(dirPath);

            //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();

            bool noInternet = MapModel.IsAppDisconnected();

            foreach (var layer in layerStack)
            {
                //Now add the layers in their correct order
                try
                {
                    var layerNo = Math.Max(layers.Count - layer.order, 0);


                    bool offlineLayerExists = offlineLayers.Keys.Contains(layer.title);

                    if (noInternet && offlineLayerExists)
                    {
                        //If no internet, check for saved tiles
                        ILayer offlineLayer;
                        offlineLayers.TryGetValue(layer.title, out offlineLayer);

                        if (offlineLayer != null)
                        {
                            offlineLayer.Opacity = layer.opacity;
                            offlineLayer.Enabled = layer.visible;
                            var WmsLayer = new MapLayer(true, 0, offlineLayer);
                            WmsLayer.Opacity = layer.opacity;
                            WmsLayer.Enabled = layer.visible;
                            WmsLayer.LayerZ  = layer.order;
                            WmsLayer.Name    = layer.title;
                            mapLayersTemp.SetValue(WmsLayer, layerNo);
                            var      path = dirPath + "/" + layer.title;
                            FileInfo fi   = new FileInfo(dirPath + "/" + layer.title + ".mbtiles");
                            if (fi != null)
                            {
                                WmsLayer.LocalStorage = fi.Length;
                            }
                            else
                            {
                                WmsLayer.LocalStorage = 0;
                            }
                            i++;
                        }
                    }
                    else
                    {
                        //If internet, read directly from WMS
                        var layerWms = WMSLayer.CreateWMSLayer(layer.url, layer.wmsLayer, "EPSG:3857", layer.title);
                        layerWms.Opacity = layer.opacity;
                        layerWms.Enabled = layer.visible;
                        if (layerWms != null)
                        {
                            var WmsLayer = new MapLayer(true, 0, layerWms);
                            WmsLayer.Opacity = layer.opacity;
                            WmsLayer.Enabled = layer.visible;
                            WmsLayer.LayerZ  = layer.order;
                            WmsLayer.Name    = layer.title;
                            mapLayersTemp.SetValue(WmsLayer, layerNo);
                            if (offlineLayerExists)
                            {
                                var      path = dirPath + "/" + layer.title;
                                FileInfo fi   = new FileInfo(dirPath + "/" + layer.title + ".mbtiles");
                                if (fi != null)
                                {
                                    WmsLayer.LocalStorage = fi.Length;
                                }
                                else
                                {
                                    WmsLayer.LocalStorage = 0;
                                }
                            }
                            i++;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            var mapLayersTempList = mapLayersTemp.ToList().GetRange(0, i);

            return(new ObservableCollection <MapLayer>(mapLayersTempList as List <MapLayer>));
        }