Ejemplo n.º 1
0
 private void getFieldsInLayer(Resource parentResource, object userState)
 {
     layer = new Layer(parentResource.Url, parentResource.ProxyUrl);
     layer.GetLayerDetailsFailed += (o, e) =>
     {
         OnGetChildResourcesFailed(e);
     };
     layer.GetLayerDetailsCompleted += (o, e) =>
     {
         OnGetChildResourcesCompleted(new GetChildResourcesCompletedEventArgs()
         {
             ChildResources = e.ChildResources, UserState = e.UserState
         });
     };
     layer.GetLayerDetails(userState);
 }
Ejemplo n.º 2
0
        private void processResult(ArcGISWebClient.DownloadStringCompletedEventArgs e) 
        {
            if (e.Cancelled)
                return;

            if (e.Error != null)
            {
                OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(e.Error, e.UserState));
                return;
            }

            if (string.IsNullOrEmpty(e.Result))
            {
                OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionEmptyResponse), e.UserState));
                return;
            }

            LayerDetails layerDetails;
            try
            {
                string json = e.Result;
                Exception exception = Utils.CheckJsonForException(json);
                if (exception != null)
                {
                    OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(exception, e.UserState));
                    return;
                }
                byte[] bytes = Encoding.Unicode.GetBytes(json);
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes))
                {
                    DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(LayerDetails));
                    layerDetails = dataContractJsonSerializer.ReadObject(memoryStream) as LayerDetails;
                    memoryStream.Close();
                }

                if (layerDetails == null)
                {
                    OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToDeserializeResponse), e.UserState));
                    return;
                }
            }
            catch (Exception ex)
            {
                OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(ex, e.UserState));
                return;
            }

            List<Resource> childResources = new List<Resource>();
            if (layerDetails.SubLayers != null)
            {
                int totalSubLayerCount = layerDetails.SubLayers.Count;
                int subLayerCount = 0;                
                string parentMapServerUrl = Uri.Substring(0, Uri.IndexOf("MapServer", StringComparison.OrdinalIgnoreCase)+9);
                foreach (SubLayer subLayer in layerDetails.SubLayers)
                {       
                    // In order to determine whether a sub layer is a group layer or not, we need to make more requests
                    string subLayerUrl = string.Format("{0}/{1}", parentMapServerUrl, subLayer.ID);
                    Layer lyr = new Layer(subLayerUrl, ProxyUrl);
                    lyr.GetLayerDetailsFailed += (o, args) =>{

                        // Remove layer
                        childResources.Remove(args.UserState as Resource);

                        subLayerCount++;
                        if(subLayerCount >= totalSubLayerCount)
                            OnGetGroupLayerDetailsCompleted(new GetLayerDetailsCompletedEventArgs() { ChildResources = childResources, LayerDetails = layerDetails, UserState = e.UserState });
                    };
                    lyr.GetLayerDetailsCompleted += (o,args) =>{
                        subLayerCount++;
                        if (args.LayerDetails == null)
                        {
                            childResources.Remove(args.UserState as Resource);
                            return;
                        }

                        Resource childResource = args.UserState as Resource;
                        childResource.ResourceType = args.LayerDetails.Type == "Group Layer" ? ResourceType.GroupLayer : ResourceType.Layer;
                        childResource.DisplayName = args.LayerDetails.Name;
                        childResource.Url = string.Format("{0}/{1}", parentMapServerUrl, args.LayerDetails.ID);
                        childResource.ProxyUrl = ProxyUrl;
                        childResource.Tag = args.LayerDetails.ID;

                        if(subLayerCount >= totalSubLayerCount)
                            OnGetGroupLayerDetailsCompleted(new GetLayerDetailsCompletedEventArgs() { ChildResources = childResources, LayerDetails = layerDetails, UserState = e.UserState });
                    };

                    // Add layer before validating to preserve catalog order.  Layer will be removed if validation
                    // fails.
                    Resource child = new Resource();
                    childResources.Add(child);

                    lyr.GetLayerDetails(child);
                }
            }
            else
            {
                OnGetGroupLayerDetailsCompleted(new GetLayerDetailsCompletedEventArgs() { ChildResources = childResources, LayerDetails = layerDetails, UserState = e.UserState });
            }
        }
Ejemplo n.º 3
0
        public override void CreateLayerAsync(Resource resource, SpatialReference mapSpatialReference, object userState)
        {
            if (resource == null)
            {
                return;
            }

            ESRI.ArcGIS.Client.Layer layer = null;

            switch (resource.ResourceType)
            {
            case ResourceType.MapServer:
            case ResourceType.FeatureServer:
            {
                string url = resource.Url;
                if (resource.ResourceType == ResourceType.FeatureServer)
                {
                    int i = url.LastIndexOf(string.Format("/{0}", ResourceType.FeatureServer.ToString()));
                    if (i >= 0)
                    {
                        url = url.Remove(i);
                        url = string.Format("{0}/{1}", url, ResourceType.MapServer.ToString());
                    }
                }

                _service = ServiceFactory.CreateService(ResourceType.MapServer, url, resource.ProxyUrl);
                if (_service != null)
                {
                    _service.ServiceDetailsDownloadFailed += (o, e) =>
                    {
                        OnCreateLayerFailed(e);
                    };
                    _service.ServiceDetailsDownloadCompleted += (o, e) =>
                    {
                        MapService mapService = o as MapService;
                        if (mapService.ServiceInfo == null)
                        {
                            OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetrieveMapServiceDetails), e.UserState));
                            return;
                        }

                        if (mapService.ServiceInfo.TileInfo != null && mapService.ServiceInfo.SingleFusedMapCache)
                        {
                            #region Create tiled layer
                            if (mapSpatialReference != null && !mapSpatialReference.Equals(mapService.ServiceInfo.SpatialReference))
                            {
                                OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionCachedMapServiceSpatialReferenceDoesNotMatch), e.UserState));
                                return;
                            }
                            layer = new ArcGISTiledMapServiceLayer()
                            {
                                Url      = url,
                                ProxyURL = getCleanProxyUrl(mapService.ProxyUrl),
                            };

                            if (mapService.ServiceInfo.TileInfo.LODs != null)
                            {
                                double maxResolution = 0;
                                double minResolution = 0;
                                foreach (LODInfo lod in mapService.ServiceInfo.TileInfo.LODs)
                                {
                                    if (lod.Resolution > maxResolution)
                                    {
                                        maxResolution = lod.Resolution;
                                    }
                                    if (minResolution <= 0 || minResolution > lod.Resolution)
                                    {
                                        minResolution = lod.Resolution;
                                    }
                                }
                                if (maxResolution > 0)
                                {
                                    layer.MaximumResolution = maxResolution * 4;
                                }
                                if (minResolution > 0)
                                {
                                    layer.MinimumResolution = minResolution / 4;
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            #region create dynamic layer
                            layer = new ArcGISDynamicMapServiceLayer()
                            {
                                Url      = url,
                                ProxyURL = getCleanProxyUrl(mapService.ProxyUrl),
                            };
                            #endregion
                        }

                        //Set layer's attached properties
                        if (layer != null)
                        {
                            layer.SetValue(MapApplication.LayerNameProperty, resource.DisplayName);
                            layer.SetValue(Core.LayerExtensions.DisplayUrlProperty, url);
                            if (!string.IsNullOrEmpty(resource.ProxyUrl))
                            {
                                Core.LayerExtensions.SetUsesProxy(layer, true);
                            }
                            layer.ID = Guid.NewGuid().ToString("N");
                        }

                        OnCreateLayerCompleted(new CreateLayerCompletedEventArgs()
                            {
                                Layer = layer, UserState = e.UserState
                            });
                    };

                    _service.GetServiceDetails(null);
                }
            } break;

            case ResourceType.ImageServer:
            {
                layer = new ArcGISImageServiceLayer()
                {
                    Url      = resource.Url,
                    ProxyURL = getCleanProxyUrl(resource.ProxyUrl),
                    ID       = Guid.NewGuid().ToString("N"),
                };

                //Set layer's attached properties
                layer.SetValue(MapApplication.LayerNameProperty, resource.DisplayName);
                layer.SetValue(Core.LayerExtensions.DisplayUrlProperty, resource.Url);
                if (!string.IsNullOrEmpty(resource.ProxyUrl))
                {
                    Core.LayerExtensions.SetUsesProxy(layer, true);
                }

                // Need to declare handler separate from lambda expression to avoid erroneous
                // "use of unassigned variable" build error
                EventHandler <EventArgs> initialized = null;

                // Need to populate the layer's metadata to handle initialization of image format
                // and band IDs
                initialized = (o, e) =>
                {
                    layer.Initialized -= initialized;
                    ArcGISImageServiceLayer imageLayer = (ArcGISImageServiceLayer)layer;
                    if (imageLayer.Version < 10)
                    {
                        // Pre v10, band IDs must be specified explicitly.  But no more than
                        // 3 can be used.  Just take up to the first 3 by default.
                        List <int> bandIDs = new List <int>();
                        for (int i = 0; i < imageLayer.BandCount; i++)
                        {
                            bandIDs.Add(i);
                            if (i == 2)
                            {
                                break;
                            }
                        }

                        imageLayer.BandIds = bandIDs.ToArray();

                        // Use png format to support transparency
                        imageLayer.ImageFormat = ArcGISImageServiceLayer.ImageServiceImageFormat.PNG8;
                    }
                    else
                    {
                        // At v10 and later, band IDs do not need to be specified, and jpg/png
                        // format introduces some intelligence about which format is best
                        imageLayer.ImageFormat =
                            ArcGISImageServiceLayer.ImageServiceImageFormat.JPGPNG;
                    }

                    OnCreateLayerCompleted(new CreateLayerCompletedEventArgs()
                        {
                            Layer = layer, UserState = userState
                        });
                };

                EventHandler <EventArgs> initFailed = null;
                initFailed = (o, e) =>
                {
                    layer.InitializationFailed -= initFailed;
                    OnCreateLayerCompleted(new CreateLayerCompletedEventArgs()
                        {
                            Layer = layer, UserState = userState
                        });
                };

                layer.Initialized          += initialized;
                layer.InitializationFailed += initFailed;
                layer.Initialize();
            }
            break;

            case ResourceType.Layer:
            case ResourceType.EditableLayer:
            {
                featureLayer = new Layer(resource.Url, resource.ProxyUrl);
                featureLayer.GetLayerDetailsFailed += (o, e) =>
                {
                    OnCreateLayerFailed(e);
                };
                featureLayer.GetLayerDetailsCompleted += (o, e) =>
                {
                    if (e.LayerDetails == null)
                    {
                        OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetrieveLayerDetails), e.UserState));
                        return;
                    }
                    if (Utility.RasterLayer.Equals(e.LayerDetails.Type))
                    {
                        OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionRasterLayersNotSupported), e.UserState));
                        return;
                    }
                    else if (Utility.ImageServerLayer.Equals(e.LayerDetails.Type))
                    {
                        OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionImageServerLayersNotSupported), e.UserState));
                        return;
                    }

                    if (Utility.CapabilitiesSupportsQuery(e.LayerDetails.Capabilities) == false)
                    {
                        OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionLayerDoesNotSupportQuery), e.UserState));
                        return;
                    }

                    GeometryType GeometryType = GeometryType.Unknown;
                    switch (e.LayerDetails.GeometryType)
                    {
                    case "esriGeometryPoint":
                        GeometryType = GeometryType.Point;
                        break;

                    case "esriGeometryMultipoint":
                        GeometryType = GeometryType.MultiPoint;
                        break;

                    case "esriGeometryPolyline":
                        GeometryType = GeometryType.Polyline;
                        break;

                    case "esriGeometryPolygon":
                        GeometryType = GeometryType.Polygon;
                        break;
                    }
                    FeatureLayer newFeatureLayer = new FeatureLayer()
                    {
                        Url      = featureLayer.Uri,
                        ProxyUrl = getCleanProxyUrl(featureLayer.ProxyUrl),
                        ID       = Guid.NewGuid().ToString("N"),
                        Mode     = FeatureLayer.QueryMode.OnDemand,
                        Renderer = new ESRI.ArcGIS.Mapping.Core.Symbols.HiddenRenderer()
                    };
                    newFeatureLayer.SetValue(MapApplication.LayerNameProperty, resource.DisplayName);
                    newFeatureLayer.SetValue(Core.LayerExtensions.DisplayUrlProperty, resource.Url);
                    newFeatureLayer.SetValue(Core.LayerExtensions.GeometryTypeProperty, GeometryType);
                    if (!string.IsNullOrEmpty(resource.ProxyUrl))
                    {
                        Core.LayerExtensions.SetUsesProxy(newFeatureLayer, true);
                    }
                    if (e.LayerDetails.Fields != null)
                    {
                        Collection <ESRI.ArcGIS.Mapping.Core.FieldInfo> fields = FieldInfosFromFields(e.LayerDetails.Fields);
                        newFeatureLayer.SetValue(Core.LayerExtensions.FieldsProperty, fields);
                        Core.LayerExtensions.SetDisplayField(newFeatureLayer, e.LayerDetails.DisplayField);
                    }
                    newFeatureLayer.OutFields.Add("*");         // Get all fields at configuration time
                    OnCreateLayerCompleted(new CreateLayerCompletedEventArgs()
                        {
                            Layer = newFeatureLayer, UserState = e.UserState, GeometryType = GeometryType
                        });
                };
                featureLayer.GetLayerDetails(userState);
            } break;

            default:
                throw new Exception(string.Format(Resources.Strings.ExceptionCannotCreateLayerForResourceType, resource.ResourceType.ToString()));
            }
        }
 private void getFieldsInLayer(Resource parentResource, object userState)
 {
     layer = new Layer(parentResource.Url, parentResource.ProxyUrl);
     layer.GetLayerDetailsFailed += (o, e) =>
     {
         OnGetChildResourcesFailed(e);
     };
     layer.GetLayerDetailsCompleted += (o, e) =>
     {
         OnGetChildResourcesCompleted(new GetChildResourcesCompletedEventArgs() { ChildResources = e.ChildResources, UserState = e.UserState });
     };
     layer.GetLayerDetails(userState);
 }
        public override void CreateLayerAsync(Resource resource, SpatialReference mapSpatialReference, object userState)
        {
            if(resource == null)
                return;

            ESRI.ArcGIS.Client.Layer layer = null;

            switch (resource.ResourceType)
            {
                case ResourceType.MapServer:
                case ResourceType.FeatureServer:
                    {
                        string url = resource.Url;
                        if (resource.ResourceType == ResourceType.FeatureServer)
                        {
                            int i = url.LastIndexOf(string.Format("/{0}", ResourceType.FeatureServer.ToString()));
                            if (i >= 0)
                            {
                                url = url.Remove(i);
                                url = string.Format("{0}/{1}", url, ResourceType.MapServer.ToString());
                            }
                        }

                        _service = ServiceFactory.CreateService(ResourceType.MapServer, url, resource.ProxyUrl);
                        if (_service != null)
                        {
                            _service.ServiceDetailsDownloadFailed += (o, e) =>
                            {
                                OnCreateLayerFailed(e);
                            };
                            _service.ServiceDetailsDownloadCompleted += (o, e) =>
                            {
                                MapService mapService = o as MapService;
                                if (mapService.ServiceInfo == null)
                                {
                                    OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetrieveMapServiceDetails), e.UserState));
                                    return;
                                }

                                if (mapService.ServiceInfo.TileInfo != null && mapService.ServiceInfo.SingleFusedMapCache)
                                {
                                    #region Create tiled layer
                                    if (mapSpatialReference != null && !mapSpatialReference.Equals(mapService.ServiceInfo.SpatialReference))
                                    {
                                        OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionCachedMapServiceSpatialReferenceDoesNotMatch), e.UserState));
                                        return;
                                    }
                                    layer = new ArcGISTiledMapServiceLayer()
                                    {
                                        Url = url,
                                        ProxyURL = getCleanProxyUrl(mapService.ProxyUrl),
                                    };

                                    if (mapService.ServiceInfo.TileInfo.LODs != null)
                                    {
                                        double maxResolution = 0;
                                        double minResolution = 0;
                                        foreach (LODInfo lod in mapService.ServiceInfo.TileInfo.LODs)
                                        {
                                            if (lod.Resolution > maxResolution)
                                                maxResolution = lod.Resolution;
                                            if (minResolution <= 0 || minResolution > lod.Resolution)
                                                minResolution = lod.Resolution;
                                        }
                                        if (maxResolution > 0 )
                                            layer.MaximumResolution = maxResolution * 4;
                                        if (minResolution > 0)
                                            layer.MinimumResolution = minResolution / 4;
                                    }
                                    #endregion
                                }
                                else
                                {
                                    #region create dynamic layer
                                    layer = new ArcGISDynamicMapServiceLayer()
                                    {
                                        Url = url,
                                        ProxyURL = getCleanProxyUrl(mapService.ProxyUrl),
                                    };
                                    #endregion
                                }

                                //Set layer's attached properties
                                if (layer != null)
                                {
                                    layer.SetValue(MapApplication.LayerNameProperty, resource.DisplayName);
                                    layer.SetValue(Core.LayerExtensions.DisplayUrlProperty, url);
                                    if (!string.IsNullOrEmpty(resource.ProxyUrl))
                                        Core.LayerExtensions.SetUsesProxy(layer, true);
                                    layer.ID = Guid.NewGuid().ToString("N");
                                }

                                OnCreateLayerCompleted(new CreateLayerCompletedEventArgs() { Layer = layer, UserState = e.UserState });

                            };

                            _service.GetServiceDetails(null);
                        }

                    } break;

                case ResourceType.ImageServer:
                    {
                        layer = new ArcGISImageServiceLayer()
                        {
                            Url = resource.Url,
                            ProxyURL = getCleanProxyUrl(resource.ProxyUrl),
                            ID = Guid.NewGuid().ToString("N"),
                        };

                        //Set layer's attached properties
                        layer.SetValue(MapApplication.LayerNameProperty, resource.DisplayName);
                        layer.SetValue(Core.LayerExtensions.DisplayUrlProperty, resource.Url);
                        if (!string.IsNullOrEmpty(resource.ProxyUrl))
                            Core.LayerExtensions.SetUsesProxy(layer, true);

                        // Need to declare handler separate from lambda expression to avoid erroneous
                        // "use of unassigned variable" build error
                        EventHandler<EventArgs> initialized = null;

                        // Need to populate the layer's metadata to handle initialization of image format
                        // and band IDs
                        initialized = (o, e) =>
                        {
                            layer.Initialized -= initialized;
                            ArcGISImageServiceLayer imageLayer = (ArcGISImageServiceLayer)layer;
                            if (imageLayer.Version < 10)
                            {
                                // Pre v10, band IDs must be specified explicitly.  But no more than 
                                // 3 can be used.  Just take up to the first 3 by default.
                                List<int> bandIDs = new List<int>();
                                for (int i = 0; i < imageLayer.BandCount; i++)
                                {
                                    bandIDs.Add(i);
                                    if (i == 2)
                                        break;
                                }

                                imageLayer.BandIds = bandIDs.ToArray();

                                // Use png format to support transparency
                                imageLayer.ImageFormat = ArcGISImageServiceLayer.ImageServiceImageFormat.PNG8;
                            }
                            else
                            {
                                // At v10 and later, band IDs do not need to be specified, and jpg/png
                                // format introduces some intelligence about which format is best
                                imageLayer.ImageFormat = 
                                    ArcGISImageServiceLayer.ImageServiceImageFormat.JPGPNG;
                            }

                            OnCreateLayerCompleted(new CreateLayerCompletedEventArgs() { Layer = layer, UserState = userState });
                        };

                        EventHandler<EventArgs> initFailed = null;
                        initFailed = (o, e) =>
                        {
                            layer.InitializationFailed -= initFailed;
                            OnCreateLayerCompleted(new CreateLayerCompletedEventArgs() { Layer = layer, UserState = userState });
                        };

                        layer.Initialized += initialized;
                        layer.InitializationFailed += initFailed;
                        layer.Initialize();
                    }
                    break;
                case ResourceType.Layer:
                case ResourceType.EditableLayer:
                    {
                        featureLayer = new Layer(resource.Url, resource.ProxyUrl);
                        featureLayer.GetLayerDetailsFailed += (o, e) =>
                        {
                            OnCreateLayerFailed(e);
                        };
                        featureLayer.GetLayerDetailsCompleted += (o, e) =>
                        {
                            if (e.LayerDetails == null)
                            {
                                OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetrieveLayerDetails), e.UserState));
                                return;
                            }
                            if (Utility.RasterLayer.Equals(e.LayerDetails.Type))
                            {
                                OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionRasterLayersNotSupported), e.UserState));
                                return;
                            }
                            else if (Utility.ImageServerLayer.Equals(e.LayerDetails.Type))
                            {
                                OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionImageServerLayersNotSupported), e.UserState));
                                return;
                            }

                            if (Utility.CapabilitiesSupportsQuery(e.LayerDetails.Capabilities) == false)
                            {
                                OnCreateLayerFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionLayerDoesNotSupportQuery), e.UserState));
                                return;
                            }

                            GeometryType GeometryType = GeometryType.Unknown;
                            switch (e.LayerDetails.GeometryType)
                            {
                                case "esriGeometryPoint":
                                    GeometryType = GeometryType.Point;
                                    break;
                                case "esriGeometryMultipoint":
                                    GeometryType = GeometryType.MultiPoint;
                                    break;
                                case "esriGeometryPolyline":
                                    GeometryType = GeometryType.Polyline;
                                    break;
                                case "esriGeometryPolygon":
                                    GeometryType = GeometryType.Polygon;
                                    break;
                            }
                            FeatureLayer newFeatureLayer = new FeatureLayer()
                            {
                                Url = featureLayer.Uri,
                                ProxyUrl = getCleanProxyUrl(featureLayer.ProxyUrl),
                                ID = Guid.NewGuid().ToString("N"),
                                Mode = FeatureLayer.QueryMode.OnDemand,
                                Renderer = new ESRI.ArcGIS.Mapping.Core.Symbols.HiddenRenderer()
                            };
                            newFeatureLayer.SetValue(MapApplication.LayerNameProperty, resource.DisplayName);
                            newFeatureLayer.SetValue(Core.LayerExtensions.DisplayUrlProperty, resource.Url);
                            newFeatureLayer.SetValue(Core.LayerExtensions.GeometryTypeProperty, GeometryType);
                            if (!string.IsNullOrEmpty(resource.ProxyUrl))
                                Core.LayerExtensions.SetUsesProxy(newFeatureLayer, true);
                            if (e.LayerDetails.Fields != null)
                            {
                                Collection<ESRI.ArcGIS.Mapping.Core.FieldInfo> fields = FieldInfosFromFields(e.LayerDetails.Fields);
                                newFeatureLayer.SetValue(Core.LayerExtensions.FieldsProperty, fields);
                                Core.LayerExtensions.SetDisplayField(newFeatureLayer, e.LayerDetails.DisplayField);
                            }
                            newFeatureLayer.OutFields.Add("*"); // Get all fields at configuration time
                            OnCreateLayerCompleted(new CreateLayerCompletedEventArgs() { Layer = newFeatureLayer, UserState = e.UserState, GeometryType = GeometryType });
                        };
                        featureLayer.GetLayerDetails(userState);
                    } break;

                default:
                    throw new Exception(string.Format(Resources.Strings.ExceptionCannotCreateLayerForResourceType, resource.ResourceType.ToString()));
            }
            
        }
Ejemplo n.º 6
0
        private void processResult(ArcGISWebClient.DownloadStringCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                return;
            }

            if (e.Error != null)
            {
                OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(e.Error, e.UserState));
                return;
            }

            if (string.IsNullOrEmpty(e.Result))
            {
                OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionEmptyResponse), e.UserState));
                return;
            }

            LayerDetails layerDetails;

            try
            {
                string    json      = e.Result;
                Exception exception = Utils.CheckJsonForException(json);
                if (exception != null)
                {
                    OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(exception, e.UserState));
                    return;
                }
                byte[] bytes = Encoding.Unicode.GetBytes(json);
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes))
                {
                    DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(LayerDetails));
                    layerDetails = dataContractJsonSerializer.ReadObject(memoryStream) as LayerDetails;
                    memoryStream.Close();
                }

                if (layerDetails == null)
                {
                    OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToDeserializeResponse), e.UserState));
                    return;
                }
            }
            catch (Exception ex)
            {
                OnGetGroupLayerDetailsFailed(new ExceptionEventArgs(ex, e.UserState));
                return;
            }

            List <Resource> childResources = new List <Resource>();

            if (layerDetails.SubLayers != null)
            {
                int    totalSubLayerCount = layerDetails.SubLayers.Count;
                int    subLayerCount      = 0;
                string parentMapServerUrl = Uri.Substring(0, Uri.IndexOf("MapServer", StringComparison.OrdinalIgnoreCase) + 9);
                foreach (SubLayer subLayer in layerDetails.SubLayers)
                {
                    // In order to determine whether a sub layer is a group layer or not, we need to make more requests
                    string subLayerUrl = string.Format("{0}/{1}", parentMapServerUrl, subLayer.ID);
                    Layer  lyr         = new Layer(subLayerUrl, ProxyUrl);
                    lyr.GetLayerDetailsFailed += (o, args) => {
                        // Remove layer
                        childResources.Remove(args.UserState as Resource);

                        subLayerCount++;
                        if (subLayerCount >= totalSubLayerCount)
                        {
                            OnGetGroupLayerDetailsCompleted(new GetLayerDetailsCompletedEventArgs()
                            {
                                ChildResources = childResources, LayerDetails = layerDetails, UserState = e.UserState
                            });
                        }
                    };
                    lyr.GetLayerDetailsCompleted += (o, args) => {
                        subLayerCount++;
                        if (args.LayerDetails == null)
                        {
                            childResources.Remove(args.UserState as Resource);
                            return;
                        }

                        Resource childResource = args.UserState as Resource;
                        childResource.ResourceType = args.LayerDetails.Type == "Group Layer" ? ResourceType.GroupLayer : ResourceType.Layer;
                        childResource.DisplayName  = args.LayerDetails.Name;
                        childResource.Url          = string.Format("{0}/{1}", parentMapServerUrl, args.LayerDetails.ID);
                        childResource.ProxyUrl     = ProxyUrl;
                        childResource.Tag          = args.LayerDetails.ID;

                        if (subLayerCount >= totalSubLayerCount)
                        {
                            OnGetGroupLayerDetailsCompleted(new GetLayerDetailsCompletedEventArgs()
                            {
                                ChildResources = childResources, LayerDetails = layerDetails, UserState = e.UserState
                            });
                        }
                    };

                    // Add layer before validating to preserve catalog order.  Layer will be removed if validation
                    // fails.
                    Resource child = new Resource();
                    childResources.Add(child);

                    lyr.GetLayerDetails(child);
                }
            }
            else
            {
                OnGetGroupLayerDetailsCompleted(new GetLayerDetailsCompletedEventArgs()
                {
                    ChildResources = childResources, LayerDetails = layerDetails, UserState = e.UserState
                });
            }
        }