Esempio n. 1
0
    /// <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();
    }