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; } }
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(); } }
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(); } }