public static List <Resource> GetResources(Catalog catalog, Filter filter, string serverUri, string proxyUrl, object lockObj) { List <Resource> resources = new List <Resource>(); if (catalog.Services != null && catalog.Services.Count > 0) { foreach (Service service in catalog.Services) { string serviceName = service.Name; // remove folder prefix int pos = serviceName.IndexOf("/", StringComparison.Ordinal); if (pos > -1) { serviceName = serviceName.Substring(pos + 1); } string serviceUrl = string.Format("{0}/{1}/{2}", serverUri, serviceName, service.Type); // Create display name string displayName = serviceName.Replace("_", " "); ResourceType type = ResourceType.Undefined; if (Enum.TryParse <ResourceType>(service.Type, true, out type)) { //Ignore Map Service Layers if Feature Access has been enabled for the service //(Map Service Layers will be added by the FeatureServer node expansion) if (type == ResourceType.MapServer && MapService.MapServiceFeatureAccessEnabled(service, catalog, serverUri, filter)) { continue; } IService ser = ServiceFactory.CreateService(type, serverUri, proxyUrl); if (ser != null && ser.IsFilteredIn(filter)) { if (lockObj != null) { lock (lockObj) { resources.Add(new Resource() { Url = serviceUrl, DisplayName = displayName, ResourceType = type, ProxyUrl = proxyUrl }); } } else { resources.Add(new Resource() { Url = serviceUrl, DisplayName = displayName, ResourceType = type, ProxyUrl = proxyUrl }); } } } } } return(resources); }
public void GetBaseMapServiceMetaData(object userToken) { if (string.IsNullOrEmpty(baseMapServiceUrl) || !Uri.IsWellFormedUriString(baseMapServiceUrl, UriKind.Absolute)) throw new InvalidOperationException(Resources.Strings.ExceptionMustSpecifyAbsoluteUriForBaseMapServiceUrl); MapService mapService = new MapService(baseMapServiceUrl, null) { ProxyUrl = this.ProxyUrl }; mapService.ServiceDetailsDownloadFailed += (o, e) => { if (GetBaseMapServiceMetaDataFailed != null) GetBaseMapServiceMetaDataFailed(this, e); }; mapService.ServiceDetailsDownloadCompleted += (o, e) => { MapService service = o as MapService; if (service != null) { MapServiceInfo mapServiceInfo = service.ServiceInfo; if (mapServiceInfo != null) { // Set spatial reference SpatialReference spatialRef = mapServiceInfo.SpatialReference; MapUnit mapUnit = MapUnit.Meters; // Set map unit string mapUnits = mapServiceInfo.Units; if (!string.IsNullOrEmpty(mapUnits)) { mapUnits = mapUnits.Replace("esri", string.Empty); // remove esri prefix from map units if (Enum.IsDefined(typeof(MapUnit), mapUnits)) { mapUnit = (MapUnit)Enum.Parse(typeof(MapUnit), mapUnits, true); } } if (GetBaseMapServiceMetaDataCompleted != null) GetBaseMapServiceMetaDataCompleted(this, new GetMapServiceMetaDataCompletedEventArgs() { MapUnit = mapUnit, SpatialReference = spatialRef, UserState = e.UserState, FullExtent = mapServiceInfo.FullExtent, InitialExtent = mapServiceInfo.InitialExtent, IsCached = mapServiceInfo.TileInfo != null && mapServiceInfo.SingleFusedMapCache }); } else { if (GetBaseMapServiceMetaDataFailed != null) GetBaseMapServiceMetaDataFailed(this, new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetriveMapServiceInformation), e.UserState)); } } else { if (GetBaseMapServiceMetaDataFailed != null) GetBaseMapServiceMetaDataFailed(this, new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetriveMapServiceInformation), e.UserState)); } }; mapService.GetServiceDetails(userToken); }
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())); } }
public void GetBaseMapServiceMetaData(object userToken) { if (string.IsNullOrEmpty(baseMapServiceUrl) || !Uri.IsWellFormedUriString(baseMapServiceUrl, UriKind.Absolute)) { throw new InvalidOperationException(Resources.Strings.ExceptionMustSpecifyAbsoluteUriForBaseMapServiceUrl); } MapService mapService = new MapService(baseMapServiceUrl, null) { ProxyUrl = this.ProxyUrl }; mapService.ServiceDetailsDownloadFailed += (o, e) => { if (GetBaseMapServiceMetaDataFailed != null) { GetBaseMapServiceMetaDataFailed(this, e); } }; mapService.ServiceDetailsDownloadCompleted += (o, e) => { MapService service = o as MapService; if (service != null) { MapServiceInfo mapServiceInfo = service.ServiceInfo; if (mapServiceInfo != null) { // Set spatial reference SpatialReference spatialRef = mapServiceInfo.SpatialReference; MapUnit mapUnit = MapUnit.Meters; // Set map unit string mapUnits = mapServiceInfo.Units; if (!string.IsNullOrEmpty(mapUnits)) { mapUnits = mapUnits.Replace("esri", string.Empty); // remove esri prefix from map units if (Enum.IsDefined(typeof(MapUnit), mapUnits)) { mapUnit = (MapUnit)Enum.Parse(typeof(MapUnit), mapUnits, true); } } if (GetBaseMapServiceMetaDataCompleted != null) { GetBaseMapServiceMetaDataCompleted(this, new GetMapServiceMetaDataCompletedEventArgs() { MapUnit = mapUnit, SpatialReference = spatialRef, UserState = e.UserState, FullExtent = mapServiceInfo.FullExtent, InitialExtent = mapServiceInfo.InitialExtent, IsCached = mapServiceInfo.TileInfo != null && mapServiceInfo.SingleFusedMapCache }); } } else { if (GetBaseMapServiceMetaDataFailed != null) { GetBaseMapServiceMetaDataFailed(this, new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetriveMapServiceInformation), e.UserState)); } } } else { if (GetBaseMapServiceMetaDataFailed != null) { GetBaseMapServiceMetaDataFailed(this, new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionUnableToRetriveMapServiceInformation), e.UserState)); } } }; mapService.GetServiceDetails(userToken); }