Ejemplo n.º 1
0
        private List <ITableClass> FindTableClass(IServiceMap map, string id, out string filterQuery)
        {
            filterQuery = String.Empty;
            if (map == null)
            {
                return(null);
            }

            List <ITableClass> classes = new List <ITableClass>();

            foreach (ILayer element in MapServerHelper.FindMapLayers(map, _useTOC, id))
            {
                if (element.Class is ITableClass)
                {
                    classes.Add(element.Class as ITableClass);
                }

                if (element is IFeatureLayer)
                {
                    if (((IFeatureLayer)element).FilterQuery != null)
                    {
                        string fquery = ((IFeatureLayer)element).FilterQuery.WhereClause;
                        if (filterQuery == String.Empty)
                        {
                            filterQuery = fquery;
                        }
                        else if (filterQuery != fquery && fquery.Trim() != String.Empty)
                        {
                            filterQuery += " AND " + fquery;
                        }
                    }
                }
            }
            return(classes);
        }
Ejemplo n.º 2
0
        public void Request(IServiceRequestContext context)
        {
            if (context == null || context.ServiceRequest == null)
            {
                return;
            }

            if (_mapServer == null)
            {
                return;
            }

            MiscParameterDescriptor parameters = new MiscParameterDescriptor();

            if (!parameters.ParseParameters(context.ServiceRequest.Request.Split('&')))
            {
                _mapServer.Log("Invalid Parameters", loggingMethod.error, context.ServiceRequest.Request);
                return;
            }

            using (IServiceMap map = context.ServiceMap) // _mapServer[context];
            {
                if (map == null)
                {
                    _mapServer.Log("Invalid Map", loggingMethod.error, context.ServiceRequest.Request);
                    return;
                }

                QueryFilter filter = parameters.BBOX != null ? new SpatialFilter() : new QueryFilter();
                filter.SubFields = "*";
                if (parameters.BBOX != null)
                {
                    ((SpatialFilter)filter).Geometry = parameters.BBOX;
                }
                ISpatialReference sRef = SpatialReference.FromID("epsg:4326");
                filter.FeatureSpatialReference = sRef;
                if (filter is SpatialFilter)
                {
                    ((SpatialFilter)filter).FilterSpatialReference = sRef;
                }

                // Get Layers
                List <ILayer> queryLayers = new List <ILayer>();
                foreach (string l in parameters.LAYERS)
                {
                    if (l == String.Empty || l[0] != 'c')
                    {
                        continue;
                    }

                    MapServerHelper.Layers layers = MapServerHelper.FindMapLayers(map, _useTOC, l.Substring(1, l.Length - 1));
                    if (layers == null)
                    {
                        continue;
                    }

                    foreach (ILayer layer in layers)
                    {
                        queryLayers.Add(layer);
                    }
                }

                StringBuilder sb = new StringBuilder();
                foreach (ILayer layer in queryLayers)
                {
                    if (layer is IFeatureLayer && ((IFeatureLayer)layer).FeatureClass != null)
                    {
                        using (IFeatureCursor cursor = ((IFeatureLayer)layer).FeatureClass.Search(filter) as IFeatureCursor)
                        {
                            Stream stream = gView.Framework.OGC.KML.KML.ToKml(cursor, 1000);
                            stream.Position = 0;
                            byte[] bytes = new byte[(int)stream.Length];
                            stream.Read(bytes, 0, bytes.Length);
                            sb.Append(Encoding.UTF8.GetString(bytes));
                        }
                    }
                }

                context.ServiceRequest.Response = sb.ToString();
            }
        }
        private void PerformQueryTilesRequest(IServiceRequestContext context, XmlNode rType)
        {
            if (context == null || context.ServiceRequest == null)
            {
                _mapServer.Log("PerformQueryTilesRequest", loggingMethod.error, "no context or servicerequest");
                return;
            }

            ServiceRequest serviceRequest = context.ServiceRequest;

            _mapServer.Log("Service:" + serviceRequest.Service, loggingMethod.request, "TileService Request: QueryTiles");

            try
            {
                int level = 0;
                if (rType.Attributes["level"] != null)
                {
                    level = int.Parse(rType.Attributes["level"].Value);
                }

                IServiceMap map = _mapServer[context];
                if (map == null)
                {
                    serviceRequest.Response = CreateException("Service not found");
                    return;
                }

                #region QueryGeometry
                IGeometry queryGeometry = null;

                #region Envelope
                XmlNode envelopeNode = rType.SelectSingleNode("Envelope[@minx and @miny and @maxx and @maxy]");
                if (envelopeNode != null)
                {
                    Envelope queryEnvelope = new Envelope(
                        double.Parse(envelopeNode.Attributes["minx"].Value, _nhi),
                        double.Parse(envelopeNode.Attributes["miny"].Value, _nhi),
                        double.Parse(envelopeNode.Attributes["maxx"].Value, _nhi),
                        double.Parse(envelopeNode.Attributes["maxy"].Value, _nhi));
                    queryGeometry = queryEnvelope;
                }
                #endregion

                #region Polygon
                XmlNode polygonNode = rType.SelectSingleNode("Polygon");
                if (polygonNode != null)
                {
                    Polygon polygon = new Polygon();
                    foreach (XmlNode ringNode in polygonNode.SelectNodes("Ring"))
                    {
                        Ring ring = new Ring();
                        foreach (XmlNode pointNode in ringNode.SelectNodes("Point[@x and @y]"))
                        {
                            ring.AddPoint(new Point(double.Parse(pointNode.Attributes["x"].Value, _nhi),
                                                    double.Parse(pointNode.Attributes["y"].Value, _nhi)));
                        }
                        if (ring.PointCount > 2)
                        {
                            polygon.AddRing(ring);
                        }
                    }
                    if (polygon.RingCount == 0)
                    {
                        serviceRequest.Response = CreateException("Invalid Polygon definition node");
                        return;
                    }
                    queryGeometry = polygon;
                }
                #endregion

                if (queryGeometry == null)
                {
                    serviceRequest.Response = CreateException("No geometry (Envelope,Polygon) definition node");
                    return;
                }
                #endregion

                #region Layer/Featureclass
                XmlNode layerNode = rType.SelectSingleNode("Layer[@id]");
                if (layerNode == null)
                {
                    serviceRequest.Response = CreateException("No layer definition node");
                    return;
                }
                string id = layerNode.Attributes["id"].Value;
                MapServerHelper.Layers layers = MapServerHelper.FindMapLayers(map, true, id);
                if (layers.Count != 1)
                {
                    serviceRequest.Response = CreateException("Can't find layer with id='" + id + "'");
                    return;
                }
                IFeatureClass fc = layers[0].Class as IFeatureClass;
                if (fc == null || fc.FindField("GRID_LEVEL") == null ||
                    fc.FindField("GRID_ROW") == null ||
                    fc.FindField("GRID_COLUMN") == null ||
                    fc.FindField("FILE") == null)
                {
                    serviceRequest.Response = CreateException("Featureclass is not a tilegrid");
                    return;
                }
                #endregion

                #region Query
                SpatialFilter filter = new SpatialFilter();
                filter.AddField(fc.IDFieldName);
                filter.AddField(fc.ShapeFieldName);
                filter.AddField("GRID_LEVEL");
                filter.AddField("GRID_ROW");
                filter.AddField("GRID_COLUMN");
                filter.AddField("FILE");
                filter.Geometry    = queryGeometry;
                filter.WhereClause = "GRID_LEVEL=" + level;

                StringBuilder sb = new StringBuilder();
                sb.Append("<TileRequest><Tiles>");
                using (IFeatureCursor cursor = fc.GetFeatures(filter))
                {
                    IFeature feature;
                    while ((feature = cursor.NextFeature) != null)
                    {
                        sb.Append("<Tile");

                        #region Envelope
                        IEnvelope env = feature.Shape.Envelope;
                        sb.Append(" id='" + feature.OID + "'");
                        sb.Append(" minx='" + env.minx.ToString(_nhi) + "'");
                        sb.Append(" miny='" + env.miny.ToString(_nhi) + "'");
                        sb.Append(" maxx='" + env.maxx.ToString(_nhi) + "'");
                        sb.Append(" maxy='" + env.maxy.ToString(_nhi) + "'");
                        #endregion

                        #region File
                        FileInfo fi = new FileInfo(feature["FILE"].ToString());
                        sb.Append(" path='/" + feature["GRID_LEVEL"].ToString() + "/" + feature["GRID_ROW"].ToString() + "/" + feature["GRID_COLUMN"].ToString() + fi.Extension + "'");
                        #endregion
                        sb.Append(" />");
                    }
                }
                sb.Append("</Tiles></TileRequest>");
                #endregion

                serviceRequest.Response = sb.ToString();
            }
            catch (Exception ex)
            {
                _mapServer.Log("Service:" + serviceRequest.Service, loggingMethod.error, ex.Message + "\r\n" + ex.StackTrace);
                serviceRequest.Response = CreateException(ex.Message);
                return;
            }
        }
Ejemplo n.º 4
0
        private void ServiceMap_BeforeRenderLayers(Framework.Carto.IServiceMap sender, List <Framework.Data.ILayer> layers)
        {
            if (String.IsNullOrWhiteSpace(_exportMap?.Layers) || !_exportMap.Layers.Contains(":"))
            {
                return;
            }

            string option = _exportMap.Layers.Substring(0, _exportMap.Layers.IndexOf(":")).ToLower();

            int[] layerIds = _exportMap.Layers.Substring(_exportMap.Layers.IndexOf(":") + 1)
                             .Split(',').Select(l => int.Parse(l)).ToArray();

            foreach (var layer in layers)
            {
                switch (option)
                {
                case "show":
                    layer.Visible = layerIds.Contains(layer.ID);
                    break;

                case "hide":
                    layer.Visible = !layerIds.Contains(layer.ID);
                    break;

                case "include":
                    if (layerIds.Contains(layer.ID))
                    {
                        layer.Visible = true;
                    }
                    break;

                case "exclude":
                    if (layerIds.Contains(layer.ID))
                    {
                        layer.Visible = false;
                    }
                    break;
                }
            }

            if (!String.IsNullOrWhiteSpace(_exportMap.DynamicLayers))
            {
                var jsonDynamicLayers = JsonConvert.DeserializeObject <JsonDynamicLayer[]>(_exportMap.DynamicLayers);
                foreach (var jsonDynamicLayer in jsonDynamicLayers)
                {
                    if (jsonDynamicLayer.Source != null)
                    {
                        var featureLayers = MapServerHelper.FindMapLayers(sender, _useTOC, jsonDynamicLayer.Source.MapLayerId.ToString());

                        foreach (var featureLayer in featureLayers)
                        {
                            if (!(featureLayer.Class is IFeatureClass))
                            {
                                continue;
                            }

                            IFeatureClass fc       = (IFeatureClass)featureLayer.Class;
                            var           dynLayer = LayerFactory.Create(featureLayer.Class, featureLayer) as IFeatureLayer;
                            if (dynLayer != null)
                            {
                                if (jsonDynamicLayer.DrawingInfo.Renderer != null)
                                {
                                    if (jsonDynamicLayer.DrawingInfo.Renderer.Type.ToLower() == "simple")
                                    {
                                        var renderer = new SimpleRenderer();
                                        if (fc.GeometryType == geometryType.Point)
                                        {
                                            var jsonRenderer = JsonConvert.DeserializeObject <SimpleMarkerSymbol>(jsonDynamicLayer.DrawingInfo.Renderer.Symbol.ToString());

                                            if (jsonRenderer.Style == "esriSMSCircle")
                                            {
                                                var symbol = new gView.Framework.Symbology.SimplePointSymbol();
                                                symbol.SymbolSmothingMode = Framework.Symbology.SymbolSmoothing.AntiAlias;
                                                symbol.FillColor          = ToColor(jsonRenderer.Color);
                                                symbol.Size = jsonRenderer.Size;
                                                if (jsonRenderer.Outline != null)
                                                {
                                                    symbol.OutlineColor = ToColor(jsonRenderer.Outline.Color);
                                                    symbol.OutlineWidth = jsonRenderer.Outline.Width;
                                                }
                                                renderer.Symbol = symbol;
                                            }
                                            else
                                            {
                                                throw new Exception("Unsupported MarkerSymbolStyle: " + jsonRenderer.Style);
                                            }
                                        }
                                        else if (fc.GeometryType == geometryType.Polyline)
                                        {
                                            var jsonRenderer = JsonConvert.DeserializeObject <SimpleLineSymbol>(jsonDynamicLayer.DrawingInfo.Renderer.Symbol.ToString());

                                            var symbol = new gView.Framework.Symbology.SimpleLineSymbol();
                                            symbol.SymbolSmothingMode = Framework.Symbology.SymbolSmoothing.AntiAlias;
                                            symbol.Color    = ToColor(jsonRenderer.Color);
                                            symbol.Width    = jsonRenderer.Width;
                                            renderer.Symbol = symbol;
                                        }
                                        else if (fc.GeometryType == geometryType.Polygon)
                                        {
                                            var jsonRenderer = JsonConvert.DeserializeObject <SimpleFillSymbol>(jsonDynamicLayer.DrawingInfo.Renderer.Symbol.ToString());

                                            var symbol = new gView.Framework.Symbology.SimpleFillSymbol();
                                            symbol.SymbolSmothingMode = Framework.Symbology.SymbolSmoothing.AntiAlias;
                                            symbol.FillColor          = ToColor(jsonRenderer.Color);

                                            if (jsonRenderer.Outline != null)
                                            {
                                                symbol.OutlineColor = ToColor(jsonRenderer.Outline.Color);
                                                symbol.OutlineWidth = jsonRenderer.Outline.Width;
                                            }
                                            renderer.Symbol = symbol;
                                        }
                                        else
                                        {
                                            throw new ArgumentException("Unsupported dynamic layer geometry: " + fc.GeometryType.ToString());
                                        }

                                        dynLayer.FeatureRenderer = renderer;
                                    }
                                    else
                                    {
                                        throw new ArgumentException("Unknwon renderer type: " + jsonDynamicLayer.DrawingInfo.Renderer.Type);
                                    }
                                }
                                dynLayer.FilterQuery = new QueryFilter()
                                {
                                    SubFields   = "*",
                                    WhereClause = jsonDynamicLayer.DefinitionExpression
                                };
                                dynLayer.Visible = true;
                                layers.Add(dynLayer);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public void Request(IServiceRequestContext context)
        {
            if (context == null || context.ServiceRequest == null)
            {
                return;
            }

            if (_mapServer == null)
            {
                return;
            }

            MiscParameterDescriptor parameters = new MiscParameterDescriptor();

            if (!parameters.ParseParameters(context.ServiceRequest.Request.Split('&')))
            {
                _mapServer.Log("Invalid Parameters", loggingMethod.error, context.ServiceRequest.Request);
                return;
            }

            using (IServiceMap map = context.ServiceMap) // _mapServer[context];
            {
                if (map == null)
                {
                    _mapServer.Log("Invalid Map", loggingMethod.error, context.ServiceRequest.Request);
                    return;
                }

                QueryFilter filter = parameters.BBOX != null ? new SpatialFilter() : new QueryFilter();
                filter.SubFields = "*";
                if (parameters.BBOX != null)
                {
                    ((SpatialFilter)filter).Geometry = parameters.BBOX;
                }
                if (!String.IsNullOrEmpty(parameters.SRS))
                {
                    ISpatialReference sRef = SpatialReference.FromID(parameters.SRS);
                    filter.FeatureSpatialReference = sRef;
                    if (filter is SpatialFilter)
                    {
                        ((SpatialFilter)filter).FilterSpatialReference = sRef;
                    }
                }
                // Get Layers
                List <ILayer> queryLayers = new List <ILayer>();
                foreach (string l in parameters.LAYERS)
                {
                    if (l == String.Empty || l[0] != 'c')
                    {
                        continue;
                    }

                    MapServerHelper.Layers layers = MapServerHelper.FindMapLayers(map, _useTOC, l.Substring(1, l.Length - 1));
                    if (layers == null)
                    {
                        continue;
                    }

                    foreach (ILayer layer in layers)
                    {
                        queryLayers.Add(layer);
                    }
                }

                StringBuilder sb = new StringBuilder();
                sb.Append("{'type':'FeatureCollection','features':[");
                foreach (ILayer layer in queryLayers)
                {
                    if (layer is IFeatureLayer && ((IFeatureLayer)layer).FeatureClass != null)
                    {
                        using (IFeatureCursor cursor = ((IFeatureLayer)layer).FeatureClass.Search(filter) as IFeatureCursor)
                        {
                            string json = gView.Framework.OGC.GeoJson.GeoJson.ToGeoJsonFeatures(cursor, parameters.MaxFeatures);
                            sb.Append(json);
                        }
                    }
                }
                sb.Append("]}");

                context.ServiceRequest.Response = sb.ToString();
            }
        }