Esempio n. 1
0
  /**
   * Loads the feature collection based on the current styling and the scale denominator. If no
   * feature is going to be returned a null feature collection will be returned instead
   *
   * @param featureSource
   * @param layer
   * @param mapContent
   * @param wms
   * @param scaleDenominator
   * @return
   * @throws Exception
   */
  public static SimpleFeatureCollection loadFeatureCollection(
      SimpleFeatureSource featureSource,
      Layer layer,
      WMSMapContent mapContent,
      WMS wms,
      double scaleDenominator)
      throws Exception {
    SimpleFeatureType schema = featureSource.getSchema();

    Envelope envelope = mapContent.getRenderingArea();
    ReferencedEnvelope aoi =
        new ReferencedEnvelope(envelope, mapContent.getCoordinateReferenceSystem());
    CoordinateReferenceSystem sourceCrs = schema.getCoordinateReferenceSystem();

    boolean reprojectBBox =
        (sourceCrs != null)
            && !CRS.equalsIgnoreMetadata(aoi.getCoordinateReferenceSystem(), sourceCrs);
    if (reprojectBBox) {
      aoi = aoi.transform(sourceCrs, true);
    }

    Filter filter = createBBoxFilter(schema, aoi);

    // now build the query using only the attributes and the bounding
    // box needed
    Query q = new Query(schema.getTypeName());
    q.setFilter(filter);

    // now, if a definition query has been established for this layer,
    // be sure to respect it by combining it with the bounding box one.
    Query definitionQuery = layer.getQuery();

    if (definitionQuery != Query.ALL) {
      if (q == Query.ALL) {
        q = (Query) definitionQuery;
      } else {
        q = (Query) DataUtilities.mixQueries(definitionQuery, q, "KMLEncoder");
      }
    }

    // handle startIndex requested by client query
    q.setStartIndex(definitionQuery.getStartIndex());

    // check the regionating strategy
    RegionatingStrategy regionatingStrategy = null;
    String stratname = (String) mapContent.getRequest().getFormatOptions().get("regionateBy");
    if (("auto").equals(stratname)) {
      Catalog catalog = wms.getGeoServer().getCatalog();
      Name name = layer.getFeatureSource().getName();
      stratname =
          catalog
              .getFeatureTypeByName(name)
              .getMetadata()
              .get("kml.regionateStrategy", String.class);
      if (stratname == null || "".equals(stratname)) {
        stratname = "best_guess";
        LOGGER.log(
            Level.FINE,
            "No default regionating strategy has been configured in "
                + name
                + "; using automatic best-guess strategy.");
      }
    }

    if (stratname != null) {
      regionatingStrategy = findStrategyByName(stratname);

      // if a strategy was specified but we did not find it, let the user
      // know
      if (regionatingStrategy == null)
        throw new ServiceException("Unknown regionating strategy " + stratname);
    }

    // try to load less features by leveraging regionating strategy and the
    // SLD
    Filter regionatingFilter = Filter.INCLUDE;

    if (regionatingStrategy != null)
      regionatingFilter = regionatingStrategy.getFilter(mapContent, layer);

    Filter ruleFilter =
        summarizeRuleFilters(
            getLayerRules(featureSource.getSchema(), layer.getStyle()), scaleDenominator);
    Filter finalFilter = joinFilters(q.getFilter(), ruleFilter, regionatingFilter);
    if (finalFilter == Filter.EXCLUDE) {
      // if we don't have any feature to return
      return null;
    }
    q.setFilter(finalFilter);

    // make sure we output in 4326 since that's what KML mandates
    CoordinateReferenceSystem wgs84;
    try {
      wgs84 = CRS.decode("EPSG:4326");
    } catch (Exception e) {
      throw new RuntimeException(
          "Cannot decode EPSG:4326, the CRS subsystem must be badly broken...");
    }
    if (sourceCrs != null && !CRS.equalsIgnoreMetadata(wgs84, sourceCrs)) {
      return new ReprojectFeatureResults(featureSource.getFeatures(q), wgs84);
    }

    return featureSource.getFeatures(q);
  }
Esempio n. 2
0
 /**
  * Encodes the url of a GetMap request from a map context + map layer.
  *
  * <p>If the <tt>Layer</tt> argument is <code>null</code>, the request is made including all
  * layers in the <tt>mapContexT</tt>.
  *
  * @param mapContent The map context.
  * @param layer The Map layer, may be <code>null</code>
  * @param layerIndex The index of the layer in the request.
  * @param kvp Additional or overidding kvp parameters, may be <code>null</code>
  * @param tile Flag controlling wether the request should be made against tile cache
  * @param geoserver
  * @return The full url for a getMap request.
  * @deprecated use {@link WMSRequests#getGetMapUrl(WMSMapContent, Layer, int, Envelope, String[])}
  */
 public static String getMapUrl(
     WMSMapContent mapContent, Layer layer, int layerIndex, boolean tile, GeoServer geoserver) {
   return getMapUrl(
       mapContent, layer, layerIndex, mapContent.getRenderingArea(), null, tile, geoserver);
 }