/// <summary> /// Gets FeatureInfo as text/plain /// </summary> /// <param name="strBBOX">string representation of a boundingbox</param> /// <returns>Plain text string with featureinfo results</returns> public static string CreateFeatureInfoPlain(SharpMap.Map map, string[] requestedLayers, Single x, Single y, int featureCount, string cqlFilter) { string vstr = "GetFeatureInfo results: \n"; foreach (string requestLayer in requestedLayers) { bool found = false; foreach (ILayer mapLayer in map.Layers) { if (String.Equals(mapLayer.LayerName, requestLayer, StringComparison.InvariantCultureIgnoreCase)) { found = true; if (!(mapLayer is ICanQueryLayer)) continue; ICanQueryLayer queryLayer = mapLayer as ICanQueryLayer; if (queryLayer.IsQueryEnabled) { Single queryBoxMinX = x - (_pixelSensitivity); Single queryBoxMinY = y - (_pixelSensitivity); Single queryBoxMaxX = x + (_pixelSensitivity); Single queryBoxMaxY = y + (_pixelSensitivity); SharpMap.Geometries.Point minXY = map.ImageToWorld(new System.Drawing.PointF(queryBoxMinX, queryBoxMinY)); SharpMap.Geometries.Point maxXY = map.ImageToWorld(new System.Drawing.PointF(queryBoxMaxX, queryBoxMaxY)); BoundingBox queryBox = new BoundingBox(minXY, maxXY); SharpMap.Data.FeatureDataSet fds = new SharpMap.Data.FeatureDataSet(); queryLayer.ExecuteIntersectionQuery(queryBox, fds); if (_intersectDelegate != null) { fds.Tables[0] = _intersectDelegate(fds.Tables[0], queryBox); } if (fds.Tables.Count == 0) { vstr = vstr + "\nSearch returned no results on layer: " + requestLayer; } else { if (fds.Tables[0].Rows.Count == 0) { vstr = vstr + "\nSearch returned no results on layer: " + requestLayer + " "; } else { //filter the rows with the CQLFilter if one is provided if (cqlFilter != null) { for (int i = fds.Tables[0].Rows.Count - 1; i >= 0; i--) { if (!CQLFilter((SharpMap.Data.FeatureDataRow)fds.Tables[0].Rows[i], cqlFilter)) { fds.Tables[0].Rows.RemoveAt(i); } } } //if featurecount < fds...count, select smallest bbox, because most likely to be clicked vstr = vstr + "\n Layer: '" + requestLayer + "'\n Featureinfo:\n"; int[] keys = new int[fds.Tables[0].Rows.Count]; double[] area = new double[fds.Tables[0].Rows.Count]; for (int l = 0; l < fds.Tables[0].Rows.Count; l++) { SharpMap.Data.FeatureDataRow fdr = fds.Tables[0].Rows[l] as SharpMap.Data.FeatureDataRow; area[l] = fdr.Geometry.GetBoundingBox().GetArea(); keys[l] = l; } Array.Sort(area, keys); if (fds.Tables[0].Rows.Count < featureCount) { featureCount = fds.Tables[0].Rows.Count; } for (int k = 0; k < featureCount; k++) { for (int j = 0; j < fds.Tables[0].Rows[keys[k]].ItemArray.Length; j++) { vstr = vstr + " '" + fds.Tables[0].Rows[keys[k]].ItemArray[j].ToString() + "'"; } if ((k + 1) < featureCount) vstr = vstr + ",\n"; } } } } } } if (found == false) { WmsException.ThrowWmsException(WmsException.WmsExceptionCode.LayerNotDefined, "Unknown layer '" + requestLayer + "'"); } } return vstr; }
/// <summary> /// Gets FeatureInfo as GeoJSON /// </summary> /// <param name="strBBOX">string representation of a boundingbox</param> /// <returns>GeoJSON string with featureinfo results</returns> public static string CreateFeatureInfoGeoJSON(SharpMap.Map map, string[] requestedLayers, Single x, Single y, int featureCount, string cqlFilter) { List<SharpMap.Converters.GeoJSON.GeoJSON> items = new List<SharpMap.Converters.GeoJSON.GeoJSON>(); foreach (string requestLayer in requestedLayers) { bool found = false; foreach (ILayer mapLayer in map.Layers) { if (String.Equals(mapLayer.LayerName, requestLayer, StringComparison.InvariantCultureIgnoreCase)) { found = true; if (!(mapLayer is ICanQueryLayer)) continue; ICanQueryLayer queryLayer = mapLayer as ICanQueryLayer; if (queryLayer.IsQueryEnabled) { Single queryBoxMinX = x - (_pixelSensitivity); Single queryBoxMinY = y - (_pixelSensitivity); Single queryBoxMaxX = x + (_pixelSensitivity); Single queryBoxMaxY = y + (_pixelSensitivity); SharpMap.Geometries.Point minXY = map.ImageToWorld(new System.Drawing.PointF(queryBoxMinX, queryBoxMinY)); SharpMap.Geometries.Point maxXY = map.ImageToWorld(new System.Drawing.PointF(queryBoxMaxX, queryBoxMaxY)); BoundingBox queryBox = new BoundingBox(minXY, maxXY); SharpMap.Data.FeatureDataSet fds = new SharpMap.Data.FeatureDataSet(); queryLayer.ExecuteIntersectionQuery(queryBox, fds); // if (_intersectDelegate != null) { fds.Tables[0] = _intersectDelegate(fds.Tables[0], queryBox); } //filter the rows with the CQLFilter if one is provided if (cqlFilter != null) { for (int i = fds.Tables[0].Rows.Count-1; i >=0 ; i--) { if (!CQLFilter((SharpMap.Data.FeatureDataRow)fds.Tables[0].Rows[i], cqlFilter)) { fds.Tables[0].Rows.RemoveAt(i); } } } IEnumerable<SharpMap.Converters.GeoJSON.GeoJSON> data = SharpMap.Converters.GeoJSON.GeoJSONHelper.GetData(fds); #if DotSpatialProjections throw new NotImplementedException(); #else // Reproject geometries if needed IMathTransform transform = null; if (queryLayer is VectorLayer) { ICoordinateTransformation transformation = (queryLayer as VectorLayer).CoordinateTransformation; transform = transformation == null ? null : transformation.MathTransform; } if (transform != null) { data = data.Select(d => { Geometry converted = GeometryTransform.TransformGeometry(d.Geometry, transform); d.SetGeometry(converted); return d; }); } #endif items.AddRange(data); } } } if (found == false) { WmsException.ThrowWmsException(WmsException.WmsExceptionCode.LayerNotDefined, "Unknown layer '" + requestLayer + "'"); } } StringWriter writer = new StringWriter(); SharpMap.Converters.GeoJSON.GeoJSONWriter.Write(items, writer); return writer.ToString(); }
/// <summary> /// /// </summary> /// <param name="map"></param> /// <param name="context"></param> private static void GetFeatureInfo(SharpMap.Map map, System.Web.HttpContext context) { //Check for required parameters if (context.Request.Params["LAYERS"] == null) { WmsException.ThrowWmsException("Required parameter LAYERS not specified"); return; } if (context.Request.Params["QUERY_LAYERS"] == null) { WmsException.ThrowWmsException("Required parameter QUERY_LAYERS not specified"); return; } if (context.Request.Params["STYLES"] == null) { WmsException.ThrowWmsException("Required parameter STYLES not specified"); return; } if (context.Request.Params["SRS"] == null) { WmsException.ThrowWmsException("Required parameter CRS not specified"); return; } /*else if (context.Request.Params["SRS"] != "EPSG:" + map.Layers[0].SRID.ToString()) { WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidCRS, "CRS not supported"); return; }*/ if (context.Request.Params["BBOX"] == null) { WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue, "Required parameter BBOX not specified"); return; } if (context.Request.Params["WIDTH"] == null) { WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue, "Required parameter WIDTH not specified"); return; } if (context.Request.Params["HEIGHT"] == null) { WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidDimensionValue, "Required parameter HEIGHT not specified"); return; } if (context.Request.Params["INFO_FORMAT"] == null) { WmsException.ThrowWmsException("Required parameter INFO_FORMAT not specified"); return; } if (context.Request.Params["FEATURE_COUNT"] == null) { WmsException.ThrowWmsException("Required parameter FEATURE_COUNT not specified"); return; } if (context.Request.Params["X"] == null) { WmsException.ThrowWmsException("Required parameter X not specified"); return; } if (context.Request.Params["Y"] == null) { WmsException.ThrowWmsException("Required parameter Y not specified"); return; } //Set layers on/off if (context.Request.Params["LAYERS"] != "") //If LAYERS is empty, use all layers { string[] layers = context.Request.Params["LAYERS"].Split(new char[] { ',' }); foreach (SharpMap.Layers.ILayer layer in map.Layers) layer.Enabled = false; bool layerNotFound = true; string layerNotFoundName = ""; foreach (string layer in layers) { SharpMap.Layers.ILayer lay = map.Layers.Find(delegate(SharpMap.Layers.ILayer findlay) { return findlay.LayerName == layer; }); if (lay == null) { WmsException.ThrowWmsException(WmsException.WmsExceptionCode.LayerNotDefined, "Unknown layer '" + layer + "'"); return; } else { if (lay is SharpMap.Layers.VectorLayer) { lay.Enabled = true; layerNotFound = false; } else { lay.Enabled = false; layerNotFoundName = layer; } } } if (layerNotFound) WmsException.ThrowWmsException(WmsException.WmsExceptionCode.LayerNotQueryable, "No se puede consultar la capa '" + layerNotFoundName + "'"); } else { foreach (SharpMap.Layers.ILayer lay in map.Layers) { if (lay is SharpMap.Layers.VectorLayer) lay.Enabled = true; else lay.Enabled = false; } } SharpMap.Geometries.BoundingBox bbox = ParseBBOX(context.Request.Params["bbox"]); if (bbox == null) { WmsException.ThrowWmsException("Invalid parameter BBOX"); return; } int w = 0; int h = 0; if (!int.TryParse(context.Request.Params["WIDTH"], out w)) WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidFormat, "Invalid WIDTH"); if (!int.TryParse(context.Request.Params["HEIGHT"], out h)) WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidFormat, "Invalid HEIGHT"); map.Size = new System.Drawing.Size(w, h); map.ZoomToBox(bbox); float x = 0; float y = 0; if (!float.TryParse(context.Request.Params["X"], System.Globalization.NumberStyles.Float, SharpMap.Map.numberFormat_EnUS, out x)) WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidPoint, "Invalid x coordinate"); if (!float.TryParse(context.Request.Params["Y"], System.Globalization.NumberStyles.Float, SharpMap.Map.numberFormat_EnUS, out y)) WmsException.ThrowWmsException(WmsException.WmsExceptionCode.InvalidPoint, "Invalid y coordinate"); System.Drawing.PointF ptf = new System.Drawing.PointF(x, y); SharpMap.Geometries.Point pt = map.ImageToWorld(ptf); // Para cada layer activo realizo la consulta: SharpMap.Data.FeatureDataRow fdrow = null; foreach (SharpMap.Layers.ILayer layer in map.Layers) { if (layer.Enabled) { SharpMap.Layers.VectorLayer vlayer = layer as SharpMap.Layers.VectorLayer; vlayer.DataSource.Open(); double distance = 0.0; fdrow = vlayer.FindGeoNearPoint(pt, 0.1, ref distance); vlayer.DataSource.Close(); } } string response = ""; if (OnProcessWMSGetFeatureInfoResponse != null) { response = OnProcessWMSGetFeatureInfoResponse(fdrow, context.Request.Params["INFO_FORMAT"], context.Request.Params["LAYERS"]); } context.Response.Clear(); context.Response.ContentType = context.Request.Params["INFO_FORMAT"]; context.Response.Write(response); context.Response.End(); }