public static void ChangeSymbolOnDefaultRenderer(this GraphicsLayer layer, string symbolJson)
        {
            if (layer == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(symbolJson))
            {
                Symbol symbol = null;
                try
                {
                    symbol = SymbolJsonHelper.SymbolFromJson(symbolJson);
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogError(ex);
                }
                if (symbol != null)
                {
                    layer.ChangeRenderer(symbol);
                }
            }
        }
示例#2
0
        private void PerformPostLayerInitializationActions(Layer layer, bool initializationSuccess)
        {
            GraphicsLayer gLayer = layer as GraphicsLayer;

            if (gLayer != null)
            {
                GeometryType           geometryType = Core.LayerExtensions.GetGeometryType(gLayer);
                Collection <FieldInfo> layerFields  = Core.LayerExtensions.GetFields(gLayer);
                FeatureLayer           featureLayer = layer as FeatureLayer;
                if (layerFields.Count == 0 &&
                    featureLayer != null && featureLayer.LayerInfo != null && featureLayer.LayerInfo.Fields != null)
                {
                    foreach (ESRI.ArcGIS.Client.Field field in featureLayer.LayerInfo.Fields)
                    {
                        if (FieldHelper.IsFieldFilteredOut(field.Type))
                        {
                            continue;
                        }
                        ESRI.ArcGIS.Mapping.Core.FieldInfo fieldInfo = ESRI.ArcGIS.Mapping.Core.FieldInfo.FieldInfoFromField(featureLayer, field);
                        layerFields.Add(fieldInfo);
                    }
                }
                if (gLayer.Graphics != null)
                {
                    #region Get geometry type, start getting symbology
                    if (geometryType == GeometryType.Unknown && gLayer.Graphics.Count > 0)
                    {
                        geometryType = LayerUtils.GetGeometryTypeFromGraphic(gLayer.Graphics.ElementAtOrDefault(0));
                        Core.LayerExtensions.SetGeometryType(gLayer, geometryType);

                        if ((gLayer.Renderer == null || gLayer.Renderer is HiddenRenderer) && !Symbology.DefaultSymbols.ContainsKey(geometryType))
                        {
                            if (geometryType == GeometryType.Unknown)
                            {
                                gLayer.SetValue(ESRI.ArcGIS.Client.Extensibility.LayerExtensions.ErrorMessageProperty, "Layer has unspecified geometry type.");
                            }
                            else
                            {
                                Core.LayerExtensions.SetRunLayerPostInitializationActions(gLayer, true);
                                SymbolConfigProvider.GetDefaultLinearGradientBrush(gLayer, ColorRampType.ClassBreaks);
                            }
                            return;
                        }
                    }
                    #endregion

                    #region Project graphics if necessary
                    if (graphicsRequireReprojection(gLayer.Graphics))
                    {
                        GeometryServiceOperationHelper helper = new GeometryServiceOperationHelper(
                            new ConfigurationStoreHelper().GetGeometryServiceUrl(ConfigurationStore)
                            );
                        helper.ProjectGraphicsCompleted += (sender, args) =>
                        {
                            GraphicsLayer targetLayer = args.UserState as GraphicsLayer;
                            if (targetLayer != null)
                            {
                                targetLayer.Graphics.Clear();
                                foreach (Graphic graphic in args.Graphics)
                                {
                                    targetLayer.Graphics.Add(graphic);
                                }
                            }
                        };
                        helper.ProjectGraphics(gLayer.Graphics, Map.SpatialReference, gLayer);
                    }
                    #endregion

                    #region Get field information
                    if (layerFields.Count == 0) // fields not determined yet
                    {
                        determineFieldsFromGraphic(layerFields, gLayer.Graphics.ElementAtOrDefault(0));
                    }
                    #endregion
                }

                #region Get renderer from feature layer's layer info, if necessary
                if (gLayer.Renderer == null || gLayer.Renderer is HiddenRenderer)
                {
                    FeatureLayer lay = gLayer as FeatureLayer;
                    if (lay != null && lay.LayerInfo != null && lay.LayerInfo.Renderer != null)
                    {
                        lay.Renderer = lay.LayerInfo.Renderer;
                    }
                }
                #endregion

                #region Change PictureMarkerSymbol to ImageFillSymbol
                if (gLayer.Renderer != null && (geometryType == GeometryType.Point || geometryType == GeometryType.MultiPoint))
                {
                    SimpleRenderer sr = gLayer.Renderer as SimpleRenderer;
                    ESRI.ArcGIS.Client.FeatureService.Symbols.PictureMarkerSymbol pms = null;
                    if (sr != null)
                    {
                        pms = sr.Symbol as ESRI.ArcGIS.Client.FeatureService.Symbols.PictureMarkerSymbol;
                        if (pms != null)
                        {
                            sr.Symbol = SymbolJsonHelper.ToImageFillSymbol(pms);
                        }
                    }
                    else
                    {
                        ClassBreaksRenderer cbr = gLayer.Renderer as ClassBreaksRenderer;
                        if (cbr != null)
                        {
                            foreach (ClassBreakInfo info in cbr.Classes)
                            {
                                pms = info.Symbol as ESRI.ArcGIS.Client.FeatureService.Symbols.PictureMarkerSymbol;
                                if (pms != null)
                                {
                                    info.Symbol = SymbolJsonHelper.ToImageFillSymbol(pms);
                                }
                            }
                        }
                        else
                        {
                            UniqueValueRenderer uvr = gLayer.Renderer as UniqueValueRenderer;
                            if (uvr != null)
                            {
                                foreach (UniqueValueInfo info in uvr.Infos)
                                {
                                    pms = info.Symbol as ESRI.ArcGIS.Client.FeatureService.Symbols.PictureMarkerSymbol;
                                    if (pms != null)
                                    {
                                        info.Symbol = SymbolJsonHelper.ToImageFillSymbol(pms);
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion

                if (gLayer.Renderer == null || gLayer.Renderer is HiddenRenderer)
                {
                    ApplyDefaultRenderer(gLayer, geometryType);
                }

                ApplyDefaultGradientBrush(gLayer);
            }
            else if ((layer is ArcGISDynamicMapServiceLayer || layer is ArcGISTiledMapServiceLayer) &&
                     !((bool)layer.GetValue(ESRI.ArcGIS.Client.WebMap.Document.IsBaseMapProperty)))
            {
                //get layer infos - used later for figuring out domain/subtypes, etc
                if ((layer.GetValue(ESRI.ArcGIS.Mapping.Core.LayerExtensions.LayerInfosProperty) as Collection <LayerInformation>)
                    == null)
                {
                    getLayerInfos(layer);
                }
            }
            bool doSelect = false;
            layer.SetValue(ESRI.ArcGIS.Client.Extensibility.LayerExtensions.InitialUpdateCompletedProperty, true);
            if (!initializationSuccess)
            {
                layer.SetValue(ESRI.ArcGIS.Client.Extensibility.LayerExtensions.InitialUpdateFailedProperty, true);
            }
            else
            {
                bool hasId = !string.IsNullOrEmpty(layer.ID) || !string.IsNullOrEmpty(layer.GetValue(ESRI.ArcGIS.Client.Extensibility.MapApplication.LayerNameProperty) as  string);
                // Certain layers get added when the map draw mode is changed (An empty graphics layer is added)
                // We don't want to auto-select this layer
                if (hasId || !(layer is GraphicsLayer))
                {
                    doSelect = true;
                }
            }

            if (doSelect)
            {
                SetSelectedLayer(new LayerEventArgs()
                {
                    Layer = layer
                });
            }

            SubscribeToLayerInitializationEvents(layer, false);
        }