public DataSource CreateDataSourceForBaseMapType(BaseMapInfo baseMapInfo)
        {
            DataSource dataSource = CreateDataSourceForBaseMapType(baseMapInfo.BaseMapType);
            if (baseMapInfo.BaseMapType == BaseMapType.ArcGISServer)
                ((ArcGISServerDataSource)dataSource).ProxyUrl = baseMapInfo.ProxyUrl;

            return dataSource;
        }
        public override Client.TiledMapServiceLayer CreateBaseMapLayer(BaseMapInfo baseMapInfo)
        {
            ArcGISTiledMapServiceLayer agsLayer = new ArcGISTiledMapServiceLayer() { Url = baseMapInfo.Url };
            
            // Apply proxy if necessary
            if (baseMapInfo.UseProxy)
            {
                agsLayer.ProxyURL = ProxyUrl;
                LayerExtensions.SetUsesProxy(agsLayer, true);
            }

            return agsLayer;
        }
        public override void SwitchBaseMapLayer(Client.Layer oldLayer, BaseMapInfo newBaseMapInfo)
        {
            Client.ArcGISTiledMapServiceLayer tiledLayer = oldLayer as Client.ArcGISTiledMapServiceLayer;
            if (tiledLayer != null)
            {
                // Apply proxy if one is in use or clear it if not
                if (newBaseMapInfo.UseProxy)
                {
                    tiledLayer.ProxyURL = ProxyUrl;
                    LayerExtensions.SetUsesProxy(tiledLayer, true);
                }
                else
                {
                    tiledLayer.ProxyURL = null;
                    LayerExtensions.SetUsesProxy(tiledLayer, false);
                }

                tiledLayer.Url = newBaseMapInfo.Url;

                tiledLayer.SetValue(ESRI.ArcGIS.Client.Extensibility.MapApplication.LayerNameProperty, newBaseMapInfo.DisplayName);
            }
        }
Exemplo n.º 4
0
        private void reCreateBaseMapLayer(BaseMapInfo targetBaseMapInfo, Envelope newMapExtent)
        {
            IBaseMapDataSource dataSource = DataSourceProvider.CreateDataSourceForBaseMapType(targetBaseMapInfo) as IBaseMapDataSource;
            if (dataSource == null)
                throw new Exception(string.Format(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionUnableToRetrieveDatasource, targetBaseMapInfo.BaseMapType.ToString()));
            List<Layer> oldBaseMapLayers = new List<Layer>();
            foreach (Layer layer in Map.Layers)
            {
                if ((bool)layer.GetValue(ESRI.ArcGIS.Client.WebMap.Document.IsBaseMapProperty))
                    oldBaseMapLayers.Add(layer);
            }
            if (oldBaseMapLayers.Count == 1 && dataSource.CanSwitchBaseMapLayer(oldBaseMapLayers[0]))
            {
                dataSource.SwitchBaseMapLayer(oldBaseMapLayers[0], targetBaseMapInfo);

                OnBaseMapChangeComplete(EventArgs.Empty);
            }
            else
            {
                TiledMapServiceLayer layer = dataSource.CreateBaseMapLayer(targetBaseMapInfo);
                layer.SetValue(ESRI.ArcGIS.Client.WebMap.Document.IsBaseMapProperty, true);
                layer.SetValue(ESRI.ArcGIS.Client.Extensibility.MapApplication.LayerNameProperty, targetBaseMapInfo.DisplayName);
                checkAndEnsureBingMapsTokenIfRequired(layer);

                // Save current selected layer
                Layer currentSelectedLayer = SelectedLayer;

                saveGraphicsInViewForSelectedLayerInAttributeDisplay();

                // Disable listening for layer changed events because we are re-adding layers to the collection. The initialization events are not fired nor is symbology changed
                Map.Layers.CollectionChanged -= Layers_CollectionChanged;

                switchBaseMapLayer(layer, newMapExtent, oldBaseMapLayers);

                // Re-Enable listening for layer changed events
                Map.Layers.CollectionChanged += Layers_CollectionChanged;

                if (Map.Layers.Count > 1 && Map.Layers.Contains(currentSelectedLayer))
                {
                    // Restore current selected layer
                    SelectedLayer = currentSelectedLayer;
                }
                else
                {
                    SelectedLayer = layer;
                }

                restoreGraphicsInViewForSelectedLayerInAttributeDisplay();

                OnNewMapCreated(new MapRecreatedEventArgs() { NewMap = Map });
                OnBaseMapChangeComplete(EventArgs.Empty);
            }
        }
Exemplo n.º 5
0
        private void CreateNewBaseMapWithSpatialReference(Envelope targetInitialExtent, Envelope targetFullExtent, BaseMapInfo baseMapInfo, SpatialReference targetServiceSpatialReference)
        {
            // Reproject oldMap's extent before comparison
            GeometryServiceOperationHelper geomHelper = new GeometryServiceOperationHelper(
                            new ConfigurationStoreHelper().GetGeometryServiceUrl(ConfigurationStore),
                            baseMapInfo.UseProxy ? baseMapInfo.ProxyUrl : null);
            geomHelper.GeometryServiceOperationFailed += (o, args) =>
            {
                Logger.Instance.LogError(args.Exception);
                MessageBoxDialog.Show(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.MsgUnableToAccessGeometryService + Environment.NewLine + args.Exception != null ? args.Exception.Message : ESRI.ArcGIS.Mapping.Controls.Resources.Strings.MsgUnknownError);
            };
            geomHelper.ProjectExtentCompleted += (o, args) =>
            {
                Envelope targetExtent = null;
                // If the extents (compared in same projection) interesect, set the extent of the new map to the projected extent
                if (targetInitialExtent != null)
                {
                    if (args.Extent.Intersects(targetInitialExtent))
                    {
                        if (isFullyContainedWithin(args.Extent, targetInitialExtent))
                            targetExtent = targetFullExtent; // if the full extent of the new service is fully within, automatically zoom to it
                        else
                            targetExtent = args.Extent;
                    }
                    else
                        targetExtent = targetInitialExtent;
                }
                else if (targetFullExtent != null)
                {
                    if (args.Extent.Intersects(targetFullExtent))
                    {
                        if (isFullyContainedWithin(args.Extent, targetFullExtent))
                            targetExtent = targetFullExtent; // if the full extent of the new service is fully within, automatically zoom to it
                        else
                            targetExtent = args.Extent;
                    }
                    else
                        targetExtent = targetFullExtent;
                }

                // else don't set an extent
                // the map will default to the full extent of the service

                // Since map will not be in a different projection, we have to re-create the map
                BaseMapInfo targetBaseMapInfo = baseMapInfo;
                IBaseMapDataSource dataSoure = DataSourceProvider.CreateDataSourceForBaseMapType(targetBaseMapInfo.BaseMapType) as IBaseMapDataSource;
                if (dataSoure == null)
                    throw new Exception(string.Format(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionDatasourceNotLoadedForBaseMapType, targetBaseMapInfo.BaseMapType.ToString()));
                TiledMapServiceLayer layer = dataSoure.CreateBaseMapLayer(targetBaseMapInfo);
                layer.SetValue(ESRI.ArcGIS.Client.WebMap.Document.IsBaseMapProperty, true);
                layer.SetValue(ESRI.ArcGIS.Client.Extensibility.MapApplication.LayerNameProperty, targetBaseMapInfo.DisplayName);
                checkAndEnsureBingMapsTokenIfRequired(layer);

                // Save current selected layer
                Layer currentSelectedLayer = SelectedLayer;

                saveGraphicsInViewForSelectedLayerInAttributeDisplay();

                // Disable listening for layer changed events because we are re-adding layers to the collection. The initialization events are not fired nor is symbology changed
                Map.Layers.CollectionChanged -= Layers_CollectionChanged;

                List<Layer> oldBaseMapLayers = new List<Layer>();
                foreach (Layer l in Map.Layers)
                {
                    if ((bool)l.GetValue(ESRI.ArcGIS.Client.WebMap.Document.IsBaseMapProperty))
                        oldBaseMapLayers.Add(l);
                }

                switchBaseMapLayer(layer, targetExtent, oldBaseMapLayers);

                // Re-Enable listening for layer changed events
                Map.Layers.CollectionChanged += Layers_CollectionChanged;

                // Restore current selected layer
                SelectedLayer = currentSelectedLayer;

                restoreGraphicsInViewForSelectedLayerInAttributeDisplay();

                OnNewMapCreated(new MapRecreatedEventArgs() { NewMap = Map });
                OnBaseMapChangeComplete(EventArgs.Empty);
            };
            geomHelper.ProjectExtent(Map.Extent, targetServiceSpatialReference);
        }
Exemplo n.º 6
0
        private void createNewMapBasedOnTargetSpatialReferenceForBaseMapLayer(BaseMapInfo baseMapInfo, SpatialReference targetServiceSpatialReference,
                Envelope targetInitialExtent, Envelope targetFullExtent)
        {
            if (Map.SpatialReference.Equals(targetServiceSpatialReference))
            {
                // Spatial references are equal -> extents are valid to compare               
                Envelope targetExtent = Map.Extent;
                if (isFullyContainedWithin(targetExtent, targetFullExtent))
                    targetExtent = targetFullExtent; // if the full extent of the new service is fully within, automatically zoom to it                                
                reCreateBaseMapLayer(baseMapInfo, targetExtent);
            }
            else if (targetServiceSpatialReference != null)
            {
                // Spatial reference mismatch

                // Since cached services cannot re-project on the fly.
                // Check if the application has any cached map services for operational data. 
                // If so, Ask the user if they wish to delete them before changing basemap
                bool hasCachedMapServiceLayer = false;
                for (int i = 1; i < Map.Layers.Count; i++) // we only care about operational data .. not basemap layer (index = 0
                {
                    if (Map.Layers[i] is ArcGISTiledMapServiceLayer)
                    {
                        hasCachedMapServiceLayer = true;
                        break;
                    }
                }
                if (hasCachedMapServiceLayer)
                {
                    MessageBoxDialog.Show(LocalizableStrings.OperationMapServicesLayersWillBeRemoved, LocalizableStrings.OperationMapServicesLayersWillBeRemovedCaption, MessageBoxButton.OKCancel,
                                    new MessageBoxClosedEventHandler(delegate(object obj, MessageBoxClosedArgs args1)
                                    {
                                        if (args1.Result == MessageBoxResult.OK)
                                        {
                                            // Remove all cached map services
                                            for (int i = Map.Layers.Count - 1; i > 0; i--)
                                            {
                                                ArcGISTiledMapServiceLayer tiledMapServiceLayer = Map.Layers[i] as ArcGISTiledMapServiceLayer;
                                                if (tiledMapServiceLayer != null)
                                                    Map.Layers.Remove(tiledMapServiceLayer);
                                            }
                                        }
                                        else
                                        {
                                            OnBaseMapChangeFailed(new ExceptionEventArgs(new Exception(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionCacheMapServicesCannotReproject), null));
                                            return;
                                        }
                                        CreateNewBaseMapWithSpatialReference(targetInitialExtent, targetFullExtent, baseMapInfo, targetServiceSpatialReference);
                                    }));
                }
                else
                    CreateNewBaseMapWithSpatialReference(targetInitialExtent, targetFullExtent, baseMapInfo, targetServiceSpatialReference);
            }
            else
            {
                reCreateBaseMapLayer(baseMapInfo, null);
            }
        }
Exemplo n.º 7
0
        public void ChangeBaseMap(BaseMapInfo baseMapInfo)
        {
            if (baseMapInfo.UseProxy)
                baseMapInfo.ProxyUrl = ProxyUrl;
            else
                baseMapInfo.ProxyUrl = null;

            if (baseMapInfo == null)
                throw new ArgumentNullException("baseMapInfo");
            IBaseMapDataSource dataSource = DataSourceProvider.CreateDataSourceForBaseMapType(baseMapInfo) as IBaseMapDataSource;
            if (dataSource == null)
                throw new Exception(string.Format(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionUnableToInstantiateDatasourceForBaseMapType, baseMapInfo.BaseMapType.ToString()));
            if (Map == null)
                throw new Exception(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionMapHasNotBeenInitializedYet);
            if (Map.SpatialReference == null)
                throw new Exception(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionMapLayersHaveNotBeenInstantiated);
            dataSource.GetMapServiceMetaDataCompleted += new EventHandler<GetMapServiceMetaDataCompletedEventArgs>(dataSourceInstance_GetMapServiceMetaDataComplete);
            dataSource.GetMapServiceMetaDataFailed += (o, e) =>
            {
                MessageBoxDialog.Show(string.Format(Strings.ServiceConnectionError, baseMapInfo.Url), 
                    Strings.ServiceConnectionErrorCaption, MessageType.Error, MessageBoxButton.OK, null, true);
                OnBaseMapChangeFailed(e);
            };

            try
            {
                dataSource.GetMapServiceMetaDataAsync(baseMapInfo.Url, baseMapInfo);
            }
            catch (Exception ex) // One case where this will throw an exception is when a string that is not a URL is specified as the URL
            {
                MessageBoxDialog.Show(string.Format(Strings.ServiceConnectionError, baseMapInfo.Url),
                    Strings.ServiceConnectionErrorCaption, MessageType.Error, MessageBoxButton.OK, null, true);
                OnBaseMapChangeFailed(new ExceptionEventArgs(ex, null));
            }
        }
        private void onAddNewBaseMapCommand(object parameter)
        {
            if (BaseMaps == null || BaseMapsList == null)
                return;

            BaseMapInfo newBaseMap = null;
           // Clone current selected base map
            BaseMapInfo currentSelectedBaseMap = BaseMapsList.SelectedItem as BaseMapInfo;
            if (currentSelectedBaseMap == null)
                currentSelectedBaseMap = BaseMaps.FirstOrDefault();
            if (currentSelectedBaseMap != null)
            {
                newBaseMap = new BaseMapInfo()
                {
                    BaseMapType = currentSelectedBaseMap.BaseMapType,
					DisplayName = ESRI.ArcGIS.Mapping.Controls.Resources.Strings.NewBasemap,
                    ThumbnailImage = currentSelectedBaseMap.ThumbnailImage,
                    BingMapsAppID = BingMapsAppID
                };
                if (currentSelectedBaseMap.BaseMapType == BaseMapType.BingMaps
                    || currentSelectedBaseMap.BaseMapType == BaseMapType.OpenStreetMap)
                {
                    // Keep same name (needed)
                    newBaseMap.Name = currentSelectedBaseMap.Name;
                    newBaseMap.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
                }
                else
                {
                    newBaseMap.Name = Guid.NewGuid().ToString("N"); // needs to be unique for AGS
                    newBaseMap.Url = currentSelectedBaseMap.Url;
                }
            }
            else
            {
                newBaseMap = new BaseMapInfo()
                {
                    BaseMapType = BaseMapType.ArcGISServer,
                    Name = Guid.NewGuid().ToString("N"), // needs to be unique for AGS
					DisplayName = ESRI.ArcGIS.Mapping.Controls.Resources.Strings.NewBasemap,
                    ThumbnailImage = "Images/basemaps/agol_imagery.png",
                    Url = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer",
                };
            }
            BaseMaps.Add(newBaseMap);
            BaseMapsList.SelectedItem = newBaseMap;
        }       
        public void LoadConfiguration(string configData)
        {
            if (string.IsNullOrEmpty(configData))
                return;

            try
            {
                XDocument xDoc = XDocument.Parse(configData);
                ObservableCollection<BaseMapInfo> baseMaps = new ObservableCollection<BaseMapInfo>();
                XElement rootNode = xDoc.Element("BaseMaps");
                if (rootNode != null)
                {
                    IEnumerable<XElement> baseMapNodes = from elem in rootNode.Elements("BaseMapInfo") select elem;
                    foreach (XElement baseMapNode in baseMapNodes)
                    {
                        BaseMapInfo baseMapInfo = new BaseMapInfo();
                        XElement DisplayName = baseMapNode.Element("DisplayName");
                        if (DisplayName != null)
                            baseMapInfo.DisplayName = DisplayName.Value;
                        XElement Name = baseMapNode.Element("Name");
                        if (Name != null)
                            baseMapInfo.Name = Name.Value;
                        XElement ThumbnailImage = baseMapNode.Element("ThumbnailImage");
                        if (ThumbnailImage != null)
                            baseMapInfo.ThumbnailImage = ThumbnailImage.Value;
                        XElement BaseMapType = baseMapNode.Element("BaseMapType");
                        if (BaseMapType != null)
                        {
                            BaseMapType outValue;
                            if (Enum.TryParse<BaseMapType>(BaseMapType.Value, out outValue))
                                baseMapInfo.BaseMapType = outValue;
                        }
                        XElement Url = baseMapNode.Element("Url");
                        if (Url != null)
                            baseMapInfo.Url = Url.Value;
                        baseMaps.Add(baseMapInfo);

                        // Get whether proxy is used
                        XElement UseProxy = baseMapNode.Element("UseProxy");
                        if (UseProxy != null)
                        {
                            bool useProxy;
                            bool.TryParse(UseProxy.Value, out useProxy);
                            baseMapInfo.UseProxy = useProxy;
                        }
                    }
                    BaseMaps = baseMaps;
                }
               
            }
            catch { }
        }
        internal void SelectBaseMap(BaseMapInfo baseMap)
        {
            if (baseMap == null)
                throw new ArgumentNullException("baseMap");

            if (BaseMapsList != null)
                BaseMapsList.SelectedItem = baseMap;
        }