private async Task GenerateRenderer(GenerateRendererParameters rendererParam)
        {
            GenerateRendererResult result = await generateRendererTask.GenerateRendererAsync(rendererParam);

            LayerDrawingOptions          layerDrawingOptions = null;
            LayerDrawingOptionCollection options             = null;

            // If this is the first execution of this sample create a new LayerDrawingOptionsCollection
            if (((ArcGISDynamicMapServiceLayer)map.Layers["USA"]).LayerDrawingOptions == null)
            {
                options = new LayerDrawingOptionCollection();

                // Add a new LayerDrawingOptions for layer ID 2 using the generated renderer
                options.Add(new LayerDrawingOptions()
                {
                    LayerID = 2, Renderer = result.Renderer
                });
            }
            else
            {
                // Otherwise the layer will have an existing LayerDrawingOptionsCollection from a previous button click
                options = ((ArcGISDynamicMapServiceLayer)map.Layers["USA"]).LayerDrawingOptions;

                // Iterate over the LayerDrawingOptionsCollection.
                // For layer ID 2 get the existing LayerDrawingOptions object and apply the newly generated renderer
                foreach (LayerDrawingOptions drawOption in options)
                {
                    if (drawOption.LayerID == 2)
                    {
                        layerDrawingOptions = drawOption;
                        drawOption.Renderer = result.Renderer;
                    }
                }
            }

            // Retrieve the GenerateRendererParameters Where clause and create a new LayerDefinition for layer ID 2
            if (!string.IsNullOrEmpty(rendererParam.Where))
            {
                LayerDefinition layerDefinition = new LayerDefinition()
                {
                    LayerID    = 2,
                    Definition = rendererParam.Where
                };

                ((ArcGISDynamicMapServiceLayer)map.Layers["USA"]).LayerDefinitions =
                    new ObservableCollection <LayerDefinition>()
                {
                    layerDefinition
                };
            }

            // Apply the updated LayerDrawingOptionsCollection to the LayerDrawingOptions property on the layer
            ((ArcGISDynamicMapServiceLayer)map.Layers["USA"]).LayerDrawingOptions = options;
        }
Exemplo n.º 2
0
        private void generateRendererTask_ExecuteCompleted(object sender, GenerateRendererResultEventArgs e)
        {
            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();

            layerDrawOptions.LayerID  = 1;
            layerDrawOptions.Renderer = e.GenerateRendererResult.Renderer;

            layer.LayerDrawingOptions =
                new LayerDrawingOptionsCollection()
            {
                layerDrawOptions
            };
            layer.VisibleLayers = new int[] { 1 };
        }
        void generateRendererTask_ExecuteCompleted(object sender, GenerateRendererResultEventArgs e)
        {
            GenerateRendererResult rendererResult = e.GenerateRendererResult;

            LayerDrawingOptionsCollection options = (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions != null
                ? (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions : new LayerDrawingOptionsCollection();

            LayerDrawingOptions layerDrawingOptionsParcels = null;

            foreach (LayerDrawingOptions drawOption in options)
            {
                if (drawOption.LayerID == 2)
                {
                    layerDrawingOptionsParcels = drawOption;
                    drawOption.Renderer        = rendererResult.Renderer;
                }
            }

            if (e.UserState != null)
            {
                LayerDefinition layerDefinition = new LayerDefinition()
                {
                    LayerID    = 2,
                    Definition = e.UserState as string
                };

                (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDefinitions =
                    new System.Collections.ObjectModel.ObservableCollection <LayerDefinition>()
                {
                    layerDefinition
                };
            }
            else
            {
                (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDefinitions = null;
            }


            if (layerDrawingOptionsParcels == null)
            {
                options.Add(new LayerDrawingOptions()
                {
                    LayerID = 2, Renderer = rendererResult.Renderer
                });
            }

            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions = options;
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).Refresh();
        }
        private void AddLayerClick(object sender, RoutedEventArgs e)
        {
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions = null;

            DynamicLayerInfoCollection myDynamicLayerInfos = (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).DynamicLayerInfos;

            if (myDynamicLayerInfos == null)
            {
                myDynamicLayerInfos = (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).CreateDynamicLayerInfosFromLayerInfos();
            }

            DynamicLayerInfo dli = new DynamicLayerInfo()
            {
                ID     = 4,
                Source = new LayerDataSource()
                {
                    DataSource = new TableDataSource()
                    {
                        WorkspaceID    = "MyDatabaseWorkspaceIDSSR2",
                        DataSourceName = "ss6.gdb.Lakes"
                    }
                }
            };

            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();

            layerDrawOptions.LayerID  = 4;
            layerDrawOptions.Renderer = new SimpleRenderer()
            {
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb((int)255, (int)0, (int)0, (int)255))
                }
            };

            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions =
                new LayerDrawingOptionsCollection()
            {
                layerDrawOptions
            };

            myDynamicLayerInfos.Insert(0, dli);
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).DynamicLayerInfos = myDynamicLayerInfos;
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).VisibleLayers     = new int[] { 3, 4 };
            // Changing VisibleLayers will refresh the layer, otherwise an explicit call to Refresh is needed.
            //(MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).Refresh();
        }
Exemplo n.º 5
0
        public void ResetRenderer(LayerDrawingOptions layerDrawingOptions, bool refreshMap)
        {
            // Exit if the layer has no LayerDrawingOptions
            if (_arcGISLocalDynamicMapServiceLayer.LayerDrawingOptions == null || _arcGISLocalDynamicMapServiceLayer.LayerDrawingOptions.Count == 0)
            {
                return;
            }

            // Remove the LayerDrawingOptions from the layer's LayerDrawingOptions collection
            if (_arcGISLocalDynamicMapServiceLayer.LayerDrawingOptions.Contains(layerDrawingOptions))
            {
                _arcGISLocalDynamicMapServiceLayer.LayerDrawingOptions.Remove(layerDrawingOptions);

                if (refreshMap)
                {
                    _arcGISLocalDynamicMapServiceLayer.Refresh();
                }
            }
        }
Exemplo n.º 6
0
        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // If there is already an instance of the Cities LayerDrawOptions definition remove it from the layer's LayerDrawingOptions collection.
            if (_citiesLayerDrawingOptions != null)
            {
                ResetRenderer(_citiesLayerDrawingOptions, false);
            }

            // Create a new Layer Drawing Options object which will define the new renderer for a specific layer in the underlying local map service
            _citiesLayerDrawingOptions = new LayerDrawingOptions();

            // Set the ID of the map layer in the local map service
            _citiesLayerDrawingOptions.LayerID = 0;

            // Create a new simple renderer with a simple marker symbol (for feature layers of point geometry type)
            _citiesLayerDrawingOptions.Renderer = new SimpleRenderer()
            {
                Symbol = new SimpleMarkerSymbol()
                {
                    Color = new SolidColorBrush(GetRandomColor()),
                    Size  = _random.Next(4, 12),
                    Style = GetRandomEnumValue <SimpleMarkerSymbol.SimpleMarkerStyle>()
                }
            };

            // Create a new layer drawing options collection and set the layer drawing options property on the local dynamic map service layer
            if (_arcGISLocalDynamicMapServiceLayer.LayerDrawingOptions == null)
            {
                _arcGISLocalDynamicMapServiceLayer.LayerDrawingOptions = new LayerDrawingOptionsCollection()
                {
                    _citiesLayerDrawingOptions
                }
            }
            ;
            else
            {
                _arcGISLocalDynamicMapServiceLayer.LayerDrawingOptions.Add(_citiesLayerDrawingOptions);
            }

            // Refresh the layer
            _arcGISLocalDynamicMapServiceLayer.Refresh();
        }
        private void AddLayerClick(object sender, RoutedEventArgs e)
        {
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions = null;

            DynamicLayerInfoCollection myDynamicLayerInfos = (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).DynamicLayerInfos;
            if (myDynamicLayerInfos == null)
                myDynamicLayerInfos = (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).CreateDynamicLayerInfosFromLayerInfos();

            DynamicLayerInfo dli = new DynamicLayerInfo()
            {
                ID = 4,
                Source = new LayerDataSource()
                {
                    DataSource = new TableDataSource()
                    {
                        WorkspaceID = "MyDatabaseWorkspaceIDSSR2",
                        DataSourceName = "ss6.gdb.Lakes"
                    }
                }
            };

            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();
            layerDrawOptions.LayerID = 4;
            layerDrawOptions.Renderer = new SimpleRenderer()
            {
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb((int)255, (int)0, (int)0, (int)255))
                }
            };

            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions =
                new LayerDrawingOptionsCollection() { layerDrawOptions };

            myDynamicLayerInfos.Insert(0, dli);
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).DynamicLayerInfos = myDynamicLayerInfos;
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).VisibleLayers = new int[] { 3,4 };
            // Changing VisibleLayers will refresh the layer, otherwise an explicit call to Refresh is needed.
            //(MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).Refresh();
        }
        private void ApplyUniqueValueClick(object sender, RoutedEventArgs e)
        {
            UniqueValueRenderer newUniqueValueRenderer = new UniqueValueRenderer()
            {
                DefaultSymbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Gray)
                },
                Field = "SUB_REGION"
            };

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value = "Pacific",
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Purple),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value = "W N Cen",
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Green),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value = "W S Cen",
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Cyan),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value = "E N Cen",
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Yellow),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value = "Mtn",
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Blue),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value = "N Eng",
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Red),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value = "E S Cen",
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Brown),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value = "Mid Atl",
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Magenta),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value = "S Atl",
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Orange),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();
            layerDrawOptions.LayerID = 2;
            layerDrawOptions.Renderer = newUniqueValueRenderer;

            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions =
                new LayerDrawingOptionsCollection() { layerDrawOptions };
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).VisibleLayers = new int[] { 2 };
            // Changing VisibleLayers will refresh the layer, otherwise an explicit call to Refresh is needed.
            //(MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).Refresh();
        }
        private async Task<ArcGISDynamicMapServiceLayer> AddFileDatasetToDynamicMapServiceLayer(WorkspaceFactoryType workspaceType, string directoryPath, List<string> fileNames)
        {
            try
            {
                // Create a new WorkspaceInfo object with a unique ID.
                string uniqueId = Guid.NewGuid().ToString();
                WorkspaceInfo workspaceInfo = new WorkspaceInfo(uniqueId, workspaceType, "DATABASE=" + directoryPath);

                // Create and initialize a new LocalMapService instance.
                LocalMapService localMapService = new LocalMapService(_emptyMapPackage) { EnableDynamicLayers = true };
                localMapService.DynamicWorkspaces.Add(workspaceInfo);
                await localMapService.StartAsync();

                // Create and initialize new ArcGISLocalDynamicMapServiceLayer over the local service.
                var dynLayer = new ArcGISDynamicMapServiceLayer()
                {
                    ID = "Workspace: " + (new DirectoryInfo(directoryPath)).Name,
                    ServiceUri = localMapService.UrlMapService
                };
                await dynLayer.InitializeAsync();

                // Create a DynamicLayerInfoCollection to hold the new datasets as "dynamic layers".
                DynamicLayerInfoCollection dynamicLayerInfoCollection = new DynamicLayerInfoCollection();
                dynLayer.DynamicLayerInfos = dynamicLayerInfoCollection;

                // Create a LayerDrawingOptionsCollection to specify the symbology for each layer.
                LayerDrawingOptionCollection layerDrawingOptionsCollection = new LayerDrawingOptionCollection();
                dynLayer.LayerDrawingOptions = layerDrawingOptionsCollection;

                // Iterate over each of the selected files in the workspace.
                int counter = 0;
                foreach (string fileName in fileNames)
                {
                    // Create a new DynamicLayerInfo (to make changes to existing map service layers use the CreateDynamicLayerInfosFromLayerInfos() method.
                    DynamicLayerInfo dynamicLayerInfo = new DynamicLayerInfo { ID = counter, Name = "Dataset: " + fileName };

                    // Create a DataSource object to represent the physical datasource implementation (table or raster) which will become the DataSource 
                    // property of a new LayerDataSource in the map service. Other supported datasource types are JoinDataSource and QueryDataSource.
                    DataSource dataSource = null;

                    // If the workspace type is Raster create a new RasterDataSource.
                    if (workspaceInfo.FactoryType == WorkspaceFactoryType.Raster)
                    {
                        // Create a new RasterDataSource object
                        dataSource = new RasterDataSource
                        {
                            // Match the DataSourceName to the physical filename on disk (including extension).
                            DataSourceName = fileName,

                            // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                            WorkspaceID = workspaceInfo.Id
                        };
                    }
                    else
                    {
                        // Else if the workspace is not Raster create a new TableDataSource
                        dataSource = new TableDataSource
                        {
                            // Match the DataSourceName to the physical filename on disk (excluding extension).
                            DataSourceName = fileName,

                            // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                            WorkspaceID = workspaceInfo.Id
                        };
                    }

                    // Set the Source property of the DynamicLayerInfo object.
                    dynamicLayerInfo.Source = new LayerDataSource { DataSource = dataSource };

                    // Add the new DynamicLayerInfo object to the collection.
                    dynamicLayerInfoCollection.Add(dynamicLayerInfo);

                    // Create a new LayerDrawingOptions object to hold the renderer information.
                    var layerDrawOpt = new LayerDrawingOptions()
                    {
                        // Match up the LayerID to the ID of the layer within the service.
                        LayerID = counter,
                    };

                    // Use the GetDetails method which now supports dynamic data sources to determine the geometry type of the new datasource.
                    var featureLayerInfo = await dynLayer.GetDetailsAsync(dynamicLayerInfo.ID);

                    switch (featureLayerInfo.GeometryType)
                    {
                        case GeometryType.Envelope:
                            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleFillSymbol() { Color = GetRandomColor(), Outline = new SimpleLineSymbol() { Color = GetRandomColor() } } };
                            break;
                        case GeometryType.MultiPoint:
                            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleMarkerSymbol() { Color = GetRandomColor(), Size = 8 } };
                            break;
                        case GeometryType.Point:
                            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleMarkerSymbol() { Color = GetRandomColor(), Size = 8 } };
                            break;
                        case GeometryType.Polygon:
                            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleFillSymbol() { Color = GetRandomColor(), Outline = new SimpleLineSymbol() { Color = GetRandomColor() } } };
                            break;
                        case GeometryType.Polyline:
                            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleLineSymbol() { Color = GetRandomColor() } };
                            break;
                        default:
                            break;
                    }

                    // Set the LayerDrawingOptions property on the local dynamic map service layer (the LayerID property ties this to the DynamicLayerInfo object).
                    layerDrawingOptionsCollection.Add(layerDrawOpt);

                    counter++;
                }

                return dynLayer;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return null;
            }
        }
        private async Task <ArcGISDynamicMapServiceLayer> AddFileDatasetToDynamicMapServiceLayer(WorkspaceFactoryType workspaceType, string directoryPath, List <string> fileNames)
        {
            try
            {
                // Create a new WorkspaceInfo object with a unique ID.
                string        uniqueId      = Guid.NewGuid().ToString();
                WorkspaceInfo workspaceInfo = new WorkspaceInfo(uniqueId, workspaceType, "DATABASE=" + directoryPath);

                // Create and initialize a new LocalMapService instance.
                LocalMapService localMapService = new LocalMapService(_emptyMapPackage)
                {
                    EnableDynamicLayers = true
                };
                localMapService.DynamicWorkspaces.Add(workspaceInfo);
                await localMapService.StartAsync();

                // Create and initialize new ArcGISLocalDynamicMapServiceLayer over the local service.
                var dynLayer = new ArcGISDynamicMapServiceLayer()
                {
                    ID         = "Workspace: " + (new DirectoryInfo(directoryPath)).Name,
                    ServiceUri = localMapService.UrlMapService
                };
                await dynLayer.InitializeAsync();

                // Create a DynamicLayerInfoCollection to hold the new datasets as "dynamic layers".
                DynamicLayerInfoCollection dynamicLayerInfoCollection = new DynamicLayerInfoCollection();
                dynLayer.DynamicLayerInfos = dynamicLayerInfoCollection;

                // Create a LayerDrawingOptionsCollection to specify the symbology for each layer.
                LayerDrawingOptionCollection layerDrawingOptionsCollection = new LayerDrawingOptionCollection();
                dynLayer.LayerDrawingOptions = layerDrawingOptionsCollection;

                // Iterate over each of the selected files in the workspace.
                int counter = 0;
                foreach (string fileName in fileNames)
                {
                    // Create a new DynamicLayerInfo (to make changes to existing map service layers use the CreateDynamicLayerInfosFromLayerInfos() method.
                    DynamicLayerInfo dynamicLayerInfo = new DynamicLayerInfo {
                        ID = counter, Name = "Dataset: " + fileName
                    };

                    // Create a DataSource object to represent the physical datasource implementation (table or raster) which will become the DataSource
                    // property of a new LayerDataSource in the map service. Other supported datasource types are JoinDataSource and QueryDataSource.
                    DataSource dataSource = null;

                    // If the workspace type is Raster create a new RasterDataSource.
                    if (workspaceInfo.FactoryType == WorkspaceFactoryType.Raster)
                    {
                        // Create a new RasterDataSource object
                        dataSource = new RasterDataSource
                        {
                            // Match the DataSourceName to the physical filename on disk (including extension).
                            DataSourceName = fileName,

                            // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                            WorkspaceID = workspaceInfo.Id
                        };
                    }
                    else
                    {
                        // Else if the workspace is not Raster create a new TableDataSource
                        dataSource = new TableDataSource
                        {
                            // Match the DataSourceName to the physical filename on disk (excluding extension).
                            DataSourceName = fileName,

                            // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                            WorkspaceID = workspaceInfo.Id
                        };
                    }

                    // Set the Source property of the DynamicLayerInfo object.
                    dynamicLayerInfo.Source = new LayerDataSource {
                        DataSource = dataSource
                    };

                    // Add the new DynamicLayerInfo object to the collection.
                    dynamicLayerInfoCollection.Add(dynamicLayerInfo);

                    // Create a new LayerDrawingOptions object to hold the renderer information.
                    var layerDrawOpt = new LayerDrawingOptions()
                    {
                        // Match up the LayerID to the ID of the layer within the service.
                        LayerID = counter,
                    };

                    // Use the GetDetails method which now supports dynamic data sources to determine the geometry type of the new datasource.
                    var featureLayerInfo = await dynLayer.GetDetailsAsync(dynamicLayerInfo.ID);

                    switch (featureLayerInfo.GeometryType)
                    {
                    case GeometryType.Envelope:
                        layerDrawOpt.Renderer = new SimpleRenderer()
                        {
                            Symbol = new SimpleFillSymbol()
                            {
                                Color = GetRandomColor(), Outline = new SimpleLineSymbol()
                                {
                                    Color = GetRandomColor()
                                }
                            }
                        };
                        break;

                    case GeometryType.Multipoint:
                        layerDrawOpt.Renderer = new SimpleRenderer()
                        {
                            Symbol = new SimpleMarkerSymbol()
                            {
                                Color = GetRandomColor(), Size = 8
                            }
                        };
                        break;

                    case GeometryType.Point:
                        layerDrawOpt.Renderer = new SimpleRenderer()
                        {
                            Symbol = new SimpleMarkerSymbol()
                            {
                                Color = GetRandomColor(), Size = 8
                            }
                        };
                        break;

                    case GeometryType.Polygon:
                        layerDrawOpt.Renderer = new SimpleRenderer()
                        {
                            Symbol = new SimpleFillSymbol()
                            {
                                Color = GetRandomColor(), Outline = new SimpleLineSymbol()
                                {
                                    Color = GetRandomColor()
                                }
                            }
                        };
                        break;

                    case GeometryType.Polyline:
                        layerDrawOpt.Renderer = new SimpleRenderer()
                        {
                            Symbol = new SimpleLineSymbol()
                            {
                                Color = GetRandomColor()
                            }
                        };
                        break;

                    default:
                        break;
                    }

                    // Set the LayerDrawingOptions property on the local dynamic map service layer (the LayerID property ties this to the DynamicLayerInfo object).
                    layerDrawingOptionsCollection.Add(layerDrawOpt);

                    counter++;
                }

                return(dynLayer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(null);
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            layer = (map.Layers[0] as ArcGISDynamicMapServiceLayer);
            DynamicLayerInfoCollection myDynamicLayerInfos = layer.DynamicLayerInfos;
            if (myDynamicLayerInfos == null)
            {
                myDynamicLayerInfos = layer.CreateDynamicLayerInfosFromLayerInfos();
            }

            #region TableDataSource
            DynamicLayerInfo info = new DynamicLayerInfo()
            {
                ID = 1,
                Source = new LayerDataSource()
                {
                    DataSource = new QueryDataSource()
                    {
                        GeometryType = ESRI.ArcGIS.Client.Tasks.GeometryType.Polyline,
                        OIDFields = new string[] { "OBJECTID" },
                        Query = "SELECT * FROM  SDE.china_road where FNODE_ > 1000",
                        WorkspaceID = "MyDatabaseWorkspaceID"
                    }
                }
            };
            #endregion

            #region QueryDataSource
            DynamicLayerInfo info = new DynamicLayerInfo()
            {
                ID = 1,
                Source = new LayerDataSource()
                {
                    DataSource = new QueryDataSource()
                    {
                        GeometryType = ESRI.ArcGIS.Client.Tasks.GeometryType.Polyline,
                        OIDFields = new string[] { "OBJECTID" },
                        Query = "SELECT * FROM  SDE.china_road where FNODE_ > 1000",
                        WorkspaceID = "MyDatabaseWorkspaceID"
                    }
                }
            };
            #endregion

            #region UserDefine Simple Render Line
            //LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();
            //layerDrawOptions.LayerID = 1;
            //layerDrawOptions.Renderer = new SimpleRenderer()
            //{
            //	Symbol = new SimpleLineSymbol()
            //	{
            //		Color = new SolidColorBrush(Color.FromArgb((int)255, (int)0, (int)0, (int)255)),
            //		Width = 2
            //	}
            //};

            //layer.LayerDrawingOptions = new LayerDrawingOptionsCollection() { layerDrawOptions };
            #endregion

            #region UserDefine Simple Render Poly
            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();
            layerDrawOptions.LayerID = 1;
            layerDrawOptions.Renderer = new SimpleRenderer()
            {
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb((int)255, (int)0, (int)0, (int)255)),
                }
            };

            layer.LayerDrawingOptions =
                    new LayerDrawingOptionsCollection() { layerDrawOptions };
            #endregion

            #region JoinDataSource
            DynamicLayerInfo info = new DynamicLayerInfo()
            {
                ID = 1,
                Source = new LayerDataSource()
                {

                    DataSource = new JoinDataSource()
                    {

                        JoinType = JoinType.LeftInnerJoin,
                        LeftTableSource = new LayerDataSource()
                        {
                            DataSource = new TableDataSource()
                            {
                                DataSourceName = "SDE.china_county",
                                WorkspaceID = "MyDatabaseWorkspaceID"
                            }
                        },
                        LeftTableKey = "OBJECTID",
                        RightTableSource = new LayerDataSource()
                        {
                            DataSource = new TableDataSource()
                            {
                                DataSourceName = "SDE.china_road",
                                WorkspaceID = "MyDatabaseWorkspaceID"
                            }
                        },
                        RightTableKey = "OBJECTID"
                    }
                }
            };
            #endregion

            #region RasterDataSource
            //DynamicLayerInfo info = new DynamicLayerInfo()
            //{
            //	ID = 1,
            //	Source = new LayerDataSource()
            //	{
            //		DataSource = new RasterDataSource()
            //		{
            //			DataSourceName = "rr1",
            //			WorkspaceID = "MyRasterWorkspaceID"
            //		}
            //	}
            //};
            #endregion

            myDynamicLayerInfos.Add(info);
            layer.DynamicLayerInfos = myDynamicLayerInfos;

            layer.VisibleLayers = new int[] { 1 };
            //layer.Refresh();
            //map.ZoomTo(new Envelope(11.8435360079, 49.4443060783, 11.8568721432, 49.4528247773));
            //map.ZoomTo(layer.Layers[1].e)

            #region Generate Render Class Break
            ClassBreaksDefinition classBreaksDefinition = new ClassBreaksDefinition()
            {
                ClassificationField = "FNODE_",
                ClassificationMethod = ClassificationMethod.StandardDeviation,
                BreakCount = 10,
                StandardDeviationInterval = ESRI.ArcGIS.Client.Tasks.StandardDeviationInterval.OneQuarter
            };
            classBreaksDefinition.ColorRamps.Add(new ColorRamp()
            {
                From = Colors.Blue,
                To = Colors.Red,
                Algorithm = Algorithm.HSVAlgorithm
            });

            GenerateRendererParameters rendererParams = new GenerateRendererParameters()
            {
                ClassificationDefinition = classBreaksDefinition,
                Source = info.Source
            };

            generateRendererTask.ExecuteAsync(rendererParams, rendererParams.Where);
            #endregion

            #region Generate Render Unique Value
            UniqueValueDefinition uniqueValueDefinition = new UniqueValueDefinition()
            {
                Fields = new List<string>() { "FNODE_" }
            };
            uniqueValueDefinition.ColorRamps.Add(new ColorRamp()
            {
                From = Colors.Blue,
                To = Colors.Red,
                Algorithm = Algorithm.CIELabAlgorithm
            });

            GenerateRendererParameters rendererParams = new GenerateRendererParameters()
            {
                ClassificationDefinition = uniqueValueDefinition,
                Source = info.Source
            };

            generateRendererTask.ExecuteAsync(rendererParams, rendererParams.Where);
            #endregion
        }
        private void generateRendererTask_ExecuteCompleted(object sender, GenerateRendererResultEventArgs e)
        {
            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();
            layerDrawOptions.LayerID = 1;
            layerDrawOptions.Renderer = e.GenerateRendererResult.Renderer;

            layer.LayerDrawingOptions =
                    new LayerDrawingOptionsCollection() { layerDrawOptions };
            layer.VisibleLayers = new int[] { 1 };
        }
Exemplo n.º 13
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            layer = (map.Layers[0] as ArcGISDynamicMapServiceLayer);
            DynamicLayerInfoCollection myDynamicLayerInfos = layer.DynamicLayerInfos;

            if (myDynamicLayerInfos == null)
            {
                myDynamicLayerInfos = layer.CreateDynamicLayerInfosFromLayerInfos();
            }

            #region TableDataSource
            DynamicLayerInfo info = new DynamicLayerInfo()
            {
                ID     = 1,
                Source = new LayerDataSource()
                {
                    DataSource = new QueryDataSource()
                    {
                        GeometryType = ESRI.ArcGIS.Client.Tasks.GeometryType.Polyline,
                        OIDFields    = new string[] { "OBJECTID" },
                        Query        = "SELECT * FROM  SDE.china_road where FNODE_ > 1000",
                        WorkspaceID  = "MyDatabaseWorkspaceID"
                    }
                }
            };
            #endregion

            #region QueryDataSource
            DynamicLayerInfo info = new DynamicLayerInfo()
            {
                ID     = 1,
                Source = new LayerDataSource()
                {
                    DataSource = new QueryDataSource()
                    {
                        GeometryType = ESRI.ArcGIS.Client.Tasks.GeometryType.Polyline,
                        OIDFields    = new string[] { "OBJECTID" },
                        Query        = "SELECT * FROM  SDE.china_road where FNODE_ > 1000",
                        WorkspaceID  = "MyDatabaseWorkspaceID"
                    }
                }
            };
            #endregion

            #region UserDefine Simple Render Line
            //LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();
            //layerDrawOptions.LayerID = 1;
            //layerDrawOptions.Renderer = new SimpleRenderer()
            //{
            //	Symbol = new SimpleLineSymbol()
            //	{
            //		Color = new SolidColorBrush(Color.FromArgb((int)255, (int)0, (int)0, (int)255)),
            //		Width = 2
            //	}
            //};

            //layer.LayerDrawingOptions = new LayerDrawingOptionsCollection() { layerDrawOptions };
            #endregion

            #region UserDefine Simple Render Poly
            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();
            layerDrawOptions.LayerID  = 1;
            layerDrawOptions.Renderer = new SimpleRenderer()
            {
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb((int)255, (int)0, (int)0, (int)255)),
                }
            };

            layer.LayerDrawingOptions =
                new LayerDrawingOptionsCollection()
            {
                layerDrawOptions
            };
            #endregion

            #region JoinDataSource
            DynamicLayerInfo info = new DynamicLayerInfo()
            {
                ID     = 1,
                Source = new LayerDataSource()
                {
                    DataSource = new JoinDataSource()
                    {
                        JoinType        = JoinType.LeftInnerJoin,
                        LeftTableSource = new LayerDataSource()
                        {
                            DataSource = new TableDataSource()
                            {
                                DataSourceName = "SDE.china_county",
                                WorkspaceID    = "MyDatabaseWorkspaceID"
                            }
                        },
                        LeftTableKey     = "OBJECTID",
                        RightTableSource = new LayerDataSource()
                        {
                            DataSource = new TableDataSource()
                            {
                                DataSourceName = "SDE.china_road",
                                WorkspaceID    = "MyDatabaseWorkspaceID"
                            }
                        },
                        RightTableKey = "OBJECTID"
                    }
                }
            };
            #endregion

            #region RasterDataSource
            //DynamicLayerInfo info = new DynamicLayerInfo()
            //{
            //	ID = 1,
            //	Source = new LayerDataSource()
            //	{
            //		DataSource = new RasterDataSource()
            //		{
            //			DataSourceName = "rr1",
            //			WorkspaceID = "MyRasterWorkspaceID"
            //		}
            //	}
            //};
            #endregion

            myDynamicLayerInfos.Add(info);
            layer.DynamicLayerInfos = myDynamicLayerInfos;


            layer.VisibleLayers = new int[] { 1 };
            //layer.Refresh();
            //map.ZoomTo(new Envelope(11.8435360079, 49.4443060783, 11.8568721432, 49.4528247773));
            //map.ZoomTo(layer.Layers[1].e)

            #region Generate Render Class Break
            ClassBreaksDefinition classBreaksDefinition = new ClassBreaksDefinition()
            {
                ClassificationField       = "FNODE_",
                ClassificationMethod      = ClassificationMethod.StandardDeviation,
                BreakCount                = 10,
                StandardDeviationInterval = ESRI.ArcGIS.Client.Tasks.StandardDeviationInterval.OneQuarter
            };
            classBreaksDefinition.ColorRamps.Add(new ColorRamp()
            {
                From      = Colors.Blue,
                To        = Colors.Red,
                Algorithm = Algorithm.HSVAlgorithm
            });

            GenerateRendererParameters rendererParams = new GenerateRendererParameters()
            {
                ClassificationDefinition = classBreaksDefinition,
                Source = info.Source
            };

            generateRendererTask.ExecuteAsync(rendererParams, rendererParams.Where);
            #endregion

            #region Generate Render Unique Value
            UniqueValueDefinition uniqueValueDefinition = new UniqueValueDefinition()
            {
                Fields = new List <string>()
                {
                    "FNODE_"
                }
            };
            uniqueValueDefinition.ColorRamps.Add(new ColorRamp()
            {
                From      = Colors.Blue,
                To        = Colors.Red,
                Algorithm = Algorithm.CIELabAlgorithm
            });

            GenerateRendererParameters rendererParams = new GenerateRendererParameters()
            {
                ClassificationDefinition = uniqueValueDefinition,
                Source = info.Source
            };

            generateRendererTask.ExecuteAsync(rendererParams, rendererParams.Where);
            #endregion
        }
        private void ApplyUniqueValueClick(object sender, RoutedEventArgs e)
        {
            UniqueValueRenderer newUniqueValueRenderer = new UniqueValueRenderer()
            {
                DefaultSymbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Colors.Gray)
                },
                Field = "SUB_REGION"
            };

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value  = "Pacific",
                Symbol = new SimpleFillSymbol()
                {
                    Fill        = new SolidColorBrush(Colors.Purple),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value  = "W N Cen",
                Symbol = new SimpleFillSymbol()
                {
                    Fill        = new SolidColorBrush(Colors.Green),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value  = "W S Cen",
                Symbol = new SimpleFillSymbol()
                {
                    Fill        = new SolidColorBrush(Colors.Cyan),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value  = "E N Cen",
                Symbol = new SimpleFillSymbol()
                {
                    Fill        = new SolidColorBrush(Colors.Yellow),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value  = "Mtn",
                Symbol = new SimpleFillSymbol()
                {
                    Fill        = new SolidColorBrush(Colors.Blue),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value  = "N Eng",
                Symbol = new SimpleFillSymbol()
                {
                    Fill        = new SolidColorBrush(Colors.Red),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value  = "E S Cen",
                Symbol = new SimpleFillSymbol()
                {
                    Fill        = new SolidColorBrush(Colors.Brown),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value  = "Mid Atl",
                Symbol = new SimpleFillSymbol()
                {
                    Fill        = new SolidColorBrush(Colors.Magenta),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            newUniqueValueRenderer.Infos.Add(new UniqueValueInfo()
            {
                Value  = "S Atl",
                Symbol = new SimpleFillSymbol()
                {
                    Fill        = new SolidColorBrush(Colors.Orange),
                    BorderBrush = new SolidColorBrush(Colors.Transparent)
                }
            });

            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();

            layerDrawOptions.LayerID  = 2;
            layerDrawOptions.Renderer = newUniqueValueRenderer;

            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions =
                new LayerDrawingOptionsCollection()
            {
                layerDrawOptions
            };
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).VisibleLayers = new int[] { 2 };
            // Changing VisibleLayers will refresh the layer, otherwise an explicit call to Refresh is needed.
            //(MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).Refresh();
        }
        private void ApplyRangeValueClick(object sender, RoutedEventArgs e)
        {
            ClassBreaksRenderer newClassBreaksRenderer = new ClassBreaksRenderer();

            newClassBreaksRenderer.Field = "POP00_SQMI";

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MinimumValue = 0,
                MaximumValue = 12,
                Symbol       = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0))
                }
            });

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MaximumValue = 31.3,
                Symbol       = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 100, 255, 100))
                }
            });

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MaximumValue = 59.7,
                Symbol       = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 0, 255, 200))
                }
            });

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MaximumValue = 146.2,
                Symbol       = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255))
                }
            });

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MaximumValue = 57173,
                Symbol       = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255))
                }
            });

            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();

            layerDrawOptions.LayerID  = 3;
            layerDrawOptions.Renderer = newClassBreaksRenderer;

            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions =
                new LayerDrawingOptionsCollection()
            {
                layerDrawOptions
            };
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).VisibleLayers = new int[] { 3 };
            // Changing VisibleLayers will refresh the layer, otherwise an explicit call to Refresh is needed.
            //(MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).Refresh();
        }
Exemplo n.º 16
0
        /// <summary>
        /// Adds a file dataset (Shapefile or raster) to a new local dynamic map service layer.
        /// </summary>
        /// <param name="workspaceType">The workspace type (FileGDB, Raster, SDE, Shapefile) <see cref="http://resources.arcgis.com/en/help/runtime-wpf/apiref/index.html?ESRI.ArcGIS.Client.Local~ESRI.ArcGIS.Client.Local.WorkspaceFactoryType.html"/>.</param>
        /// <param name="directoryPath">A <see cref="System.String"/> representing the directory path.</param>
        /// <param name="fileNames">A <see cref="System.Collections.Generic.List{System.String}"/> representing the name of the file (for raster datasets this must include the extension).</param>
        /// <param name="callback">The Action delegate to call back on once the dynamic layer work is complete.</param>                
        public void AddFileDatasetToDynamicMapServiceLayer(WorkspaceFactoryType workspaceType, string directoryPath, List<string> fileNames, Action<ArcGISLocalDynamicMapServiceLayer> callback)
        {
            try
            {
            // Generate a unique workspace ID (any unique string).
            string uniqueId = Guid.NewGuid().ToString();

            // Create a new WorkspaceInfo object with a unique ID.
            WorkspaceInfo workspaceInfo = new WorkspaceInfo(uniqueId, workspaceType, "DATABASE=" + directoryPath);

            // Create a new LocalMapService instance.
            LocalMapService localMapService = new LocalMapService
            {
                // Set the path property.
                Path = _emptyMpkPath,

                // Enable the dynamic layers capability.
                EnableDynamicLayers = true
            };

            // Register the workspace to be used with this service.
            localMapService.DynamicWorkspaces.Add(workspaceInfo);

            // Asynchronously start the local map service.
            localMapService.StartAsync(x =>
            {
                // Create the local dynamic map service layer.
                ArcGISLocalDynamicMapServiceLayer arcGisLocalDynamicMapServiceLayer = null;

                // Create a new ArcGISLocalDynamicMapServiceLayer passing in the newly started local service.
                arcGisLocalDynamicMapServiceLayer = new ArcGISLocalDynamicMapServiceLayer(localMapService)
                {
                    // Assign the filename as the map layer ID.
                    ID = "Workspace: " + (new DirectoryInfo(directoryPath)).Name,

                    // Enable the dynamic layers capability.
                    EnableDynamicLayers = true,
                };

                // Handle the layer initialized event inline to perform the layer dynamic layer management.
                arcGisLocalDynamicMapServiceLayer.Initialized += (s, e) =>
                {
                    // Create a DynamicLayerInfoCollection to hold the new datasets as "dynamic layers".
                    DynamicLayerInfoCollection dynamicLayerInfoCollection = new DynamicLayerInfoCollection();

                    // Create a LayerDrawingOptionsCollection to specify the symbology for each layer.
                    LayerDrawingOptionsCollection layerDrawingOptionsCollection = new LayerDrawingOptionsCollection();

                    // Iterate over each of the selected files in the workspace.
                    int counter = 0;
                    foreach (string fileName in fileNames)
                    {
                        // Create a new DynamicLayerInfo (to make changes to existing map service layers use the CreateDynamicLayerInfosFromLayerInfos() method.
                        DynamicLayerInfo dynamicLayerInfo = new DynamicLayerInfo
                        {
                            // Assign a layer ID.
                            ID = counter,

                            // Specify a friendly name.
                            Name = "Dataset: " + fileName
                        };

                        // Create a DataSource object to represent the physical datasource implementation (table or raster) which will become the DataSource
                        // property of a new LayerDataSource in the map service. Other supported datasource types are JoinDataSource and QueryDataSource.
                        DataSource dataSource = null;

                        // If the workspace type is Raster create a new RasterDataSource.
                        if (workspaceInfo.FactoryType == WorkspaceFactoryType.Raster)
                        {
                            // Create a new RasterDataSource object
                            dataSource = new RasterDataSource
                            {
                                // Match the DataSourceName to the physical filename on disk (including extension).
                                DataSourceName = fileName,

                                // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                                WorkspaceID = workspaceInfo.Id
                            };
                        }
                        else
                        {
                            // Else if the workspace is not Raster create a new TableDataSource
                            dataSource = new TableDataSource
                            {
                                // Match the DataSourceName to the physical filename on disk (excluding extension).
                                DataSourceName = fileName,

                                // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                                WorkspaceID = workspaceInfo.Id
                            };

                            /*
                                * Apply a renderer for vector layers.
                                * Note: It is always necessary to provide a renderer when the layer being added (represented by a DynamicLayerInfo) is part of a new
                                * DynamicLayerInfoCollection as opposed to using the CreateDynamicLayerInfosFromLayerInfos() method which creates a DynamicLayerInfoCollection
                                * containing the existing layers in the map service.
                            */

                            // Create a new LayerDrawingOptions object to hold the renderer information.
                            var layerDrawOpt = new LayerDrawingOptions()
                            {
                                // Match up the LayerID to the ID of the layer within the service.
                                LayerID = counter,
                            };

                            // We need to determine the geometry type of the new feature class.
                            // To do this, we will submit a request to the ..\MapServer\dynamicLayer?.. endpoint which will return the service level metadata,
                            // allowing us to identify the geometry type and create an appropriate renderer.
                            // Note:- This is a workaround until the next release where GetAllDetails will honor the DynamicLayerInfoCollection.

                            // Create a new WebClient instance to make the request and download the response.
                            WebClient webClient = new WebClient();

                            // Register an asynchronous handler in which to create the renderers and apply the to the dynamic map service layer.
                            webClient.DownloadDataCompleted += (client, downloadDataEventArgs) =>
                            {

                                // Read the JSON response as XML
                                XmlReader reader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(downloadDataEventArgs.Result, new XmlDictionaryReaderQuotas());

                                // Get the root XML element
                                XElement root = XElement.Load(reader);

                                // Query for the "geometryType" element
                                XElement geometryType = root.XPathSelectElement("//geometryType");

                                // Create the render based on the geometry type
                                switch (geometryType.Value)
                                {
                                    case "esriGeometryPoint":
                                        layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleMarkerSymbol() { Color = new SolidColorBrush(GetRandomColor()), Size = 8 } };
                                        break;
                                    case "esriGeometryPolyline":
                                        layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleLineSymbol() { Color = new SolidColorBrush(GetRandomColor()) } };
                                        break;
                                    case "esriGeometryPolygon":
                                        layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleFillSymbol() { Fill = new SolidColorBrush(GetRandomColor()), BorderBrush = new SolidColorBrush(GetRandomColor()) } };
                                        break;
                                }

                                // Set the LayerDrawingOptions property on the local dynamic map service layer (the LayerID property ties this to the DynamicLayerInfo object).
                                layerDrawingOptionsCollection.Add(layerDrawOpt);

                                // Update the layer drawing options property on the dynamic map service layer.
                                arcGisLocalDynamicMapServiceLayer.LayerDrawingOptions = layerDrawingOptionsCollection;

                                // Need to refresh the layer after the renderer(s) have been applied.
                                arcGisLocalDynamicMapServiceLayer.Refresh();

                            };

                            // Make the request for the service metadata
                            // e.g. http://127.0.0.1:<PORT>/arcgis/rest/services/<MPK_NAME>/MapServer/dynamicLayer?layer={"id":0,"source":{"type":"dataLayer","dataSource":{"type":"table","workspaceId":"MyWorkspace","dataSourceName":"MyFeatureClassName"}}}
                            webClient.DownloadDataAsync(new Uri(arcGisLocalDynamicMapServiceLayer.Url
                                + "/dynamicLayer?layer={'id':" + counter.ToString() + ","
                                + "'source':{'type':'dataLayer','dataSource':{"
                                + "'type':'table',"
                                + "'workspaceId':'" + workspaceInfo.Id + "',"
                                + "'dataSourceName':'" + fileName + "'"
                                + "}}}"));
                        }

                        // Set the Source property of the DynamicLayerInfo object.
                        dynamicLayerInfo.Source = new LayerDataSource { DataSource = dataSource };

                        // Add the new DynamicLayerInfo object to the collection.
                        dynamicLayerInfoCollection.Add(dynamicLayerInfo);

                        // Increment the counter which is being used to assign Layer IDs.
                        counter++;
                    }

                    // Update the DynamicLayerInfos property on the dynamic map service layer.
                    arcGisLocalDynamicMapServiceLayer.DynamicLayerInfos = dynamicLayerInfoCollection;

                    // Call the Action delegate.
                    callback(arcGisLocalDynamicMapServiceLayer);
                };

                // Call the Initialize method on the layer to initialize the layer properties.
                arcGisLocalDynamicMapServiceLayer.Initialize();
            });
            }
            catch (Exception ex)
            {
            MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void ApplyRangeValueClick(object sender, RoutedEventArgs e)
        {
            ClassBreaksRenderer newClassBreaksRenderer = new ClassBreaksRenderer();
            newClassBreaksRenderer.Field = "POP00_SQMI";

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MinimumValue = 0,
                MaximumValue = 12,
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0))
                }
            });

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MaximumValue = 31.3,
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 100, 255, 100))
                }
            });

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MaximumValue = 59.7,
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 0, 255, 200))
                }
            });

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MaximumValue = 146.2,
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255))
                }
            });

            newClassBreaksRenderer.Classes.Add(new ClassBreakInfo()
            {
                MaximumValue = 57173,
                Symbol = new SimpleFillSymbol()
                {
                    Fill = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255))
                }
            });

            LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();
            layerDrawOptions.LayerID = 3;
            layerDrawOptions.Renderer = newClassBreaksRenderer;

            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions =
                new LayerDrawingOptionsCollection() { layerDrawOptions };
            (MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).VisibleLayers = new int[] { 3 };
            // Changing VisibleLayers will refresh the layer, otherwise an explicit call to Refresh is needed.
            //(MyMap.Layers["USA"] as ArcGISDynamicMapServiceLayer).Refresh();
        }
Exemplo n.º 18
0
        /// <summary>
        /// Adds a file dataset (Shapefile or raster) to a new local dynamic map service layer.
        /// </summary>
        /// <param name="workspaceType">The workspace type (FileGDB, Raster, SDE, Shapefile) <see cref="http://resources.arcgis.com/en/help/runtime-wpf/apiref/index.html?ESRI.ArcGIS.Client.Local~ESRI.ArcGIS.Client.Local.WorkspaceFactoryType.html"/>.</param>
        /// <param name="directoryPath">A <see cref="System.String"/> representing the directory path.</param>
        /// <param name="fileNames">A <see cref="System.Collections.Generic.List{System.String}"/> representing the name of the file (for raster datasets this must include the extension).</param>
        /// <param name="callback">The Action delegate to call back on once the dynamic layer work is complete.</param>
        public void AddFileDatasetToDynamicMapServiceLayer(WorkspaceFactoryType workspaceType, string directoryPath, List <string> fileNames, Action <ArcGISLocalDynamicMapServiceLayer> callback)
        {
            try
            {
                // Generate a unique workspace ID (any unique string).
                string uniqueId = Guid.NewGuid().ToString();

                // Create a new WorkspaceInfo object with a unique ID.
                WorkspaceInfo workspaceInfo = new WorkspaceInfo(uniqueId, workspaceType, "DATABASE=" + directoryPath);

                // Create a new LocalMapService instance.
                LocalMapService localMapService = new LocalMapService
                {
                    // Set the path property.
                    Path = _emptyMpkPath,

                    // Enable the dynamic layers capability.
                    EnableDynamicLayers = true
                };

                // Register the workspace to be used with this service.
                localMapService.DynamicWorkspaces.Add(workspaceInfo);

                // Asynchronously start the local map service.
                localMapService.StartAsync(x =>
                {
                    // Create the local dynamic map service layer.
                    ArcGISLocalDynamicMapServiceLayer arcGisLocalDynamicMapServiceLayer = null;

                    // Create a new ArcGISLocalDynamicMapServiceLayer passing in the newly started local service.
                    arcGisLocalDynamicMapServiceLayer = new ArcGISLocalDynamicMapServiceLayer(localMapService)
                    {
                        // Assign the filename as the map layer ID.
                        ID = "Workspace: " + (new DirectoryInfo(directoryPath)).Name,

                        // Enable the dynamic layers capability.
                        EnableDynamicLayers = true,
                    };

                    // Handle the layer initialized event inline to perform the layer dynamic layer management.
                    arcGisLocalDynamicMapServiceLayer.Initialized += (s, e) =>
                    {
                        // Create a DynamicLayerInfoCollection to hold the new datasets as "dynamic layers".
                        DynamicLayerInfoCollection dynamicLayerInfoCollection = new DynamicLayerInfoCollection();

                        // Create a LayerDrawingOptionsCollection to specify the symbology for each layer.
                        LayerDrawingOptionsCollection layerDrawingOptionsCollection = new LayerDrawingOptionsCollection();

                        // Iterate over each of the selected files in the workspace.
                        int counter = 0;
                        foreach (string fileName in fileNames)
                        {
                            // Create a new DynamicLayerInfo (to make changes to existing map service layers use the CreateDynamicLayerInfosFromLayerInfos() method.
                            DynamicLayerInfo dynamicLayerInfo = new DynamicLayerInfo
                            {
                                // Assign a layer ID.
                                ID = counter,

                                // Specify a friendly name.
                                Name = "Dataset: " + fileName
                            };

                            // Create a DataSource object to represent the physical datasource implementation (table or raster) which will become the DataSource
                            // property of a new LayerDataSource in the map service. Other supported datasource types are JoinDataSource and QueryDataSource.
                            DataSource dataSource = null;

                            // If the workspace type is Raster create a new RasterDataSource.
                            if (workspaceInfo.FactoryType == WorkspaceFactoryType.Raster)
                            {
                                // Create a new RasterDataSource object
                                dataSource = new RasterDataSource
                                {
                                    // Match the DataSourceName to the physical filename on disk (including extension).
                                    DataSourceName = fileName,

                                    // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                                    WorkspaceID = workspaceInfo.Id
                                };
                            }
                            else
                            {
                                // Else if the workspace is not Raster create a new TableDataSource
                                dataSource = new TableDataSource
                                {
                                    // Match the DataSourceName to the physical filename on disk (excluding extension).
                                    DataSourceName = fileName,

                                    // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                                    WorkspaceID = workspaceInfo.Id
                                };

                                /*
                                 * Apply a renderer for vector layers.
                                 * Note: It is always necessary to provide a renderer when the layer being added (represented by a DynamicLayerInfo) is part of a new
                                 * DynamicLayerInfoCollection as opposed to using the CreateDynamicLayerInfosFromLayerInfos() method which creates a DynamicLayerInfoCollection
                                 * containing the existing layers in the map service. However, the renderer provided does not need to be valid with regard to the actual
                                 * layer and geometry type, it simply needs to be a valid renderer. If the renderer specified here is not appropriate for the geometry type of
                                 * the layer the symbology will fall back to a default SimpleMarkerSymbol, SimpleLineSymbol or SimpleFillSymbol.
                                 */

                                // Create a new LayerDrawingOptions object to hold the renderer information.
                                var layerDrawOpt = new LayerDrawingOptions()
                                {
                                    // Match up the LayerID to the ID of the layer within the service.
                                    LayerID = counter,

                                    // Provide a renderer. In this example it is an empty SimpleMarkerSymbol.
                                    Renderer = new SimpleRenderer()
                                    {
                                        Symbol = new SimpleMarkerSymbol()
                                        {
                                        }
                                    },
                                };

                                // Set the LayerDrawingOptions property on the local dynamic map service layer (the LayerID property ties this to the DynamicLayerInfo object).
                                layerDrawingOptionsCollection.Add(layerDrawOpt);
                            }

                            // Set the Source property of the DynamicLayerInfo object.
                            dynamicLayerInfo.Source = new LayerDataSource {
                                DataSource = dataSource
                            };

                            // Add the new DynamicLayerInfo object to the collection.
                            dynamicLayerInfoCollection.Add(dynamicLayerInfo);

                            // Increment the counter which is being used to assign Layer IDs.
                            counter++;
                        }

                        // Update the DynamicLayerInfos property on the dynamic map service layer.
                        arcGisLocalDynamicMapServiceLayer.DynamicLayerInfos = dynamicLayerInfoCollection;

                        // Update the layer drawing options property on the dynamic map service layer.
                        arcGisLocalDynamicMapServiceLayer.LayerDrawingOptions = layerDrawingOptionsCollection;

                        // Refresh the layer.
                        arcGisLocalDynamicMapServiceLayer.Refresh();

                        // Call the Action delegate.
                        callback(arcGisLocalDynamicMapServiceLayer);
                    };

                    // Call the Initialize method on the layer to initialize the layer properties.
                    arcGisLocalDynamicMapServiceLayer.Initialize();
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// Adds a file dataset (Shapefile or raster) to a new local dynamic map service layer.
        /// </summary>
        /// <param name="workspaceType">The workspace type (FileGDB, Raster, SDE, Shapefile) <see cref="http://resources.arcgis.com/en/help/runtime-wpf/apiref/index.html?ESRI.ArcGIS.Client.Local~ESRI.ArcGIS.Client.Local.WorkspaceFactoryType.html"/>.</param>
        /// <param name="directoryPath">A <see cref="System.String"/> representing the directory path.</param>
        /// <param name="fileNames">A <see cref="System.Collections.Generic.List{System.String}"/> representing the name of the file (for raster datasets this must include the extension).</param>
        /// <param name="callback">The Action delegate to call back on once the dynamic layer work is complete.</param>
        public void AddFileDatasetToDynamicMapServiceLayer(WorkspaceFactoryType workspaceType, string directoryPath, List <string> fileNames, Action <ArcGISLocalDynamicMapServiceLayer> callback)
        {
            try
            {
                // Generate a unique workspace ID (any unique string).
                string uniqueId = Guid.NewGuid().ToString();

                // Create a new WorkspaceInfo object with a unique ID.
                WorkspaceInfo workspaceInfo = new WorkspaceInfo(uniqueId, workspaceType, "DATABASE=" + directoryPath);

                // Create a new LocalMapService instance.
                LocalMapService localMapService = new LocalMapService
                {
                    // Set the path property.
                    Path = _emptyMpkPath,

                    // Enable the dynamic layers capability.
                    EnableDynamicLayers = true
                };

                // Register the workspace to be used with this service.
                localMapService.DynamicWorkspaces.Add(workspaceInfo);

                // Asynchronously start the local map service.
                localMapService.StartAsync(x =>
                {
                    // Create the local dynamic map service layer.
                    ArcGISLocalDynamicMapServiceLayer arcGisLocalDynamicMapServiceLayer = null;

                    // Create a new ArcGISLocalDynamicMapServiceLayer passing in the newly started local service.
                    arcGisLocalDynamicMapServiceLayer = new ArcGISLocalDynamicMapServiceLayer(localMapService)
                    {
                        // Assign the filename as the map layer ID.
                        ID = "Workspace: " + (new DirectoryInfo(directoryPath)).Name,

                        // Enable the dynamic layers capability.
                        EnableDynamicLayers = true,
                    };

                    // Handle the layer initialized event inline to perform the layer dynamic layer management.
                    arcGisLocalDynamicMapServiceLayer.Initialized += (s, e) =>
                    {
                        // Create a DynamicLayerInfoCollection to hold the new datasets as "dynamic layers".
                        DynamicLayerInfoCollection dynamicLayerInfoCollection = new DynamicLayerInfoCollection();

                        // Create a LayerDrawingOptionsCollection to specify the symbology for each layer.
                        LayerDrawingOptionsCollection layerDrawingOptionsCollection = new LayerDrawingOptionsCollection();

                        // Iterate over each of the selected files in the workspace.
                        int counter = 0;
                        foreach (string fileName in fileNames)
                        {
                            // Create a new DynamicLayerInfo (to make changes to existing map service layers use the CreateDynamicLayerInfosFromLayerInfos() method.
                            DynamicLayerInfo dynamicLayerInfo = new DynamicLayerInfo
                            {
                                // Assign a layer ID.
                                ID = counter,

                                // Specify a friendly name.
                                Name = "Dataset: " + fileName
                            };

                            // Create a DataSource object to represent the physical datasource implementation (table or raster) which will become the DataSource
                            // property of a new LayerDataSource in the map service. Other supported datasource types are JoinDataSource and QueryDataSource.
                            DataSource dataSource = null;

                            // If the workspace type is Raster create a new RasterDataSource.
                            if (workspaceInfo.FactoryType == WorkspaceFactoryType.Raster)
                            {
                                // Create a new RasterDataSource object
                                dataSource = new RasterDataSource
                                {
                                    // Match the DataSourceName to the physical filename on disk (including extension).
                                    DataSourceName = fileName,

                                    // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                                    WorkspaceID = workspaceInfo.Id
                                };
                            }
                            else
                            {
                                // Else if the workspace is not Raster create a new TableDataSource
                                dataSource = new TableDataSource
                                {
                                    // Match the DataSourceName to the physical filename on disk (excluding extension).
                                    DataSourceName = fileName,

                                    // Provide the WorkspaceID (the unique workspace identifier created earlier). A LocalMapService may have multiple dynamic workspaces.
                                    WorkspaceID = workspaceInfo.Id
                                };

                                /*
                                 * Apply a renderer for vector layers.
                                 * Note: It is always necessary to provide a renderer when the layer being added (represented by a DynamicLayerInfo) is part of a new
                                 * DynamicLayerInfoCollection as opposed to using the CreateDynamicLayerInfosFromLayerInfos() method which creates a DynamicLayerInfoCollection
                                 * containing the existing layers in the map service.
                                 */

                                // Create a new LayerDrawingOptions object to hold the renderer information.
                                var layerDrawOpt = new LayerDrawingOptions()
                                {
                                    // Match up the LayerID to the ID of the layer within the service.
                                    LayerID = counter,
                                };

                                // We need to determine the geometry type of the new feature class.
                                // To do this, we will submit a request to the ..\MapServer\dynamicLayer?.. endpoint which will return the service level metadata,
                                // allowing us to identify the geometry type and create an appropriate renderer.
                                // Note:- This is a workaround until the next release where GetAllDetails will honor the DynamicLayerInfoCollection.

                                // Create a new WebClient instance to make the request and download the response.
                                WebClient webClient = new WebClient();

                                // Register an asynchronous handler in which to create the renderers and apply the to the dynamic map service layer.
                                webClient.DownloadDataCompleted += (client, downloadDataEventArgs) =>
                                {
                                    // Read the JSON response as XML
                                    XmlReader reader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(downloadDataEventArgs.Result, new XmlDictionaryReaderQuotas());

                                    // Get the root XML element
                                    XElement root = XElement.Load(reader);

                                    // Query for the "geometryType" element
                                    XElement geometryType = root.XPathSelectElement("//geometryType");

                                    // Create the render based on the geometry type
                                    switch (geometryType.Value)
                                    {
                                    case "esriGeometryPoint":
                                        layerDrawOpt.Renderer = new SimpleRenderer()
                                        {
                                            Symbol = new SimpleMarkerSymbol()
                                            {
                                                Color = new SolidColorBrush(GetRandomColor()), Size = 8
                                            }
                                        };
                                        break;

                                    case "esriGeometryPolyline":
                                        layerDrawOpt.Renderer = new SimpleRenderer()
                                        {
                                            Symbol = new SimpleLineSymbol()
                                            {
                                                Color = new SolidColorBrush(GetRandomColor())
                                            }
                                        };
                                        break;

                                    case "esriGeometryPolygon":
                                        layerDrawOpt.Renderer = new SimpleRenderer()
                                        {
                                            Symbol = new SimpleFillSymbol()
                                            {
                                                Fill = new SolidColorBrush(GetRandomColor()), BorderBrush = new SolidColorBrush(GetRandomColor())
                                            }
                                        };
                                        break;
                                    }

                                    // Set the LayerDrawingOptions property on the local dynamic map service layer (the LayerID property ties this to the DynamicLayerInfo object).
                                    layerDrawingOptionsCollection.Add(layerDrawOpt);

                                    // Update the layer drawing options property on the dynamic map service layer.
                                    arcGisLocalDynamicMapServiceLayer.LayerDrawingOptions = layerDrawingOptionsCollection;

                                    // Need to refresh the layer after the renderer(s) have been applied.
                                    arcGisLocalDynamicMapServiceLayer.Refresh();
                                };

                                // Make the request for the service metadata
                                // e.g. http://127.0.0.1:<PORT>/arcgis/rest/services/<MPK_NAME>/MapServer/dynamicLayer?layer={"id":0,"source":{"type":"dataLayer","dataSource":{"type":"table","workspaceId":"MyWorkspace","dataSourceName":"MyFeatureClassName"}}}
                                webClient.DownloadDataAsync(new Uri(arcGisLocalDynamicMapServiceLayer.Url
                                                                    + "/dynamicLayer?layer={'id':" + counter.ToString() + ","
                                                                    + "'source':{'type':'dataLayer','dataSource':{"
                                                                    + "'type':'table',"
                                                                    + "'workspaceId':'" + workspaceInfo.Id + "',"
                                                                    + "'dataSourceName':'" + fileName + "'"
                                                                    + "}}}"));
                            }

                            // Set the Source property of the DynamicLayerInfo object.
                            dynamicLayerInfo.Source = new LayerDataSource {
                                DataSource = dataSource
                            };

                            // Add the new DynamicLayerInfo object to the collection.
                            dynamicLayerInfoCollection.Add(dynamicLayerInfo);

                            // Increment the counter which is being used to assign Layer IDs.
                            counter++;
                        }

                        // Update the DynamicLayerInfos property on the dynamic map service layer.
                        arcGisLocalDynamicMapServiceLayer.DynamicLayerInfos = dynamicLayerInfoCollection;

                        // Call the Action delegate.
                        callback(arcGisLocalDynamicMapServiceLayer);
                    };

                    // Call the Initialize method on the layer to initialize the layer properties.
                    arcGisLocalDynamicMapServiceLayer.Initialize();
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }