Exemplo n.º 1
0
  /**
   * @param envelope
   * @param width
   * @param height
   * @return
   * @throws ThinklabResourceNotFoundException
   */
  private BufferedImage getWMSImage(Envelope envelope, int width, int height)
      throws ThinklabResourceNotFoundException {

    BufferedImage ret = null;
    initializeWms();

    String sig = envelope.toString() + "," + width + "," + height;

    if (_cache.containsKey(sig)) return ImageUtil.clone(_cache.get(sig));

    if (_wms != null) {

      GetMapRequest request = _wms.createGetMapRequest();
      request.setFormat("image/png");
      request.setDimensions("" + width, "" + height);
      request.setTransparent(true);

      // FIXME this assumes the envelope is in lat/lon
      request.setSRS("EPSG:4326");

      String bbox =
          (float) envelope.getMinX()
              + ","
              + (float) envelope.getMinY()
              + ","
              + (float) envelope.getMaxX()
              + ","
              + (float) envelope.getMaxY();

      request.setBBox(bbox);

      for (Layer layer : getWMSLayers()) {
        request.addLayer(layer);
      }

      GetMapResponse response = null;
      try {

        System.out.println(request.getFinalURL());

        response = (GetMapResponse) _wms.issueRequest(request);
        ret = ImageIO.read(response.getInputStream());
      } catch (Exception e) {
        Geospace.get().logger().warn("cannot get WFS imagery: " + e.getLocalizedMessage());
        return null;
      }

      /*
       * FIXME this obviously must have a limit
       */
      if (ret != null) _cache.put(sig, ImageUtil.clone(ret));
    }

    return ret;
  }
Exemplo n.º 2
0
 private Layer getLayer(WebMapServer server, String layerName) {
   for (Layer layer : server.getCapabilities().getLayerList()) {
     if (layerName.equals(layer.getName())) {
       return layer;
     }
   }
   throw new IllegalArgumentException("Could not find layer " + layerName);
 }
Exemplo n.º 3
0
  private Collection<Layer> getWMSLayers() {

    String zp = Geospace.get().getProperties().getProperty(WMS_LAYER_PROPERTY + "." + _wms_index);
    ArrayList<Layer> layers = new ArrayList<Layer>();
    for (Layer l : WMSUtils.getNamedLayers(_wms.getCapabilities())) {
      if (zp == null || (zp != null && zp.contains(l.getName()))) {
        layers.add(l);
      }
    }
    return layers;
  }
Exemplo n.º 4
0
  public ImageDescriptor createWMSGylph(Layer target) {
    if (target.isType(WebMapServer.class)) return null;
    try {
      WebMapServer wms = target.getResource(WebMapServer.class, null);
      org.geotools.data.ows.Layer layer =
          target.getResource(org.geotools.data.ows.Layer.class, null);

      if (wms.getCapabilities().getRequest().getGetLegendGraphic() != null) {

        GetLegendGraphicRequest request = wms.createGetLegendGraphicRequest();
        request.setLayer(layer.getName());

        String desiredFormat = null;
        List formats = wms.getCapabilities().getRequest().getGetLegendGraphic().getFormats();
        if (formats.contains("image/png")) { // $NON-NLS-1$
          desiredFormat = "image/png"; // $NON-NLS-1$
        }
        if (desiredFormat == null && formats.contains("image/gif")) { // $NON-NLS-1$
          desiredFormat = "image/gif"; // $NON-NLS-1$
        }
        if (desiredFormat == null) {
          return null;
        }
        request.setFormat(desiredFormat);

        return ImageDescriptor.createFromURL(request.getFinalURL());
      }
    } catch (Exception e) {
      // darn
    }
    return null;
    /*
     * BufferedImage image = createBufferedImage( target, 16, 16); Graphics2D g2 = (Graphics2D)
     * image.getGraphics(); g2.setColor(Color.GREEN); g2.fillRect(1, 1, 14, 14);
     * g2.setColor(Color.BLACK); g2.drawRect(0, 0, 15, 15); return createImageDescriptor(image);
     */
  }
 @SuppressWarnings("unchecked")
 private void setImageFormat(WebMapServer wms, GetMapRequest request) {
   List formats = wms.getCapabilities().getRequest().getGetMap().getFormats();
   String str;
   if (getPreferencesStore().getBoolean(PreferenceConstants.P_USE_DEFAULT_ORDER)) {
     str = getPreferencesStore().getDefaultString(PreferenceConstants.P_IMAGE_TYPE_ORDER);
   } else {
     str = getPreferencesStore().getString(PreferenceConstants.P_IMAGE_TYPE_ORDER);
   }
   String[] preferredFormats = str.split(","); // $NON-NLS-1$
   // Select one of the available formats from the WMS server
   // the order of preferred formats is set in the preferences
   for (String format : preferredFormats) {
     if (formats.contains(format)) {
       request.setProperty(GetMapRequest.FORMAT, format);
       request.setTransparent(formatSupportsTransparency(format));
       break;
     }
   }
 }
  public static void main(String[] args) {
    URL url = null;
    try {
      url =
          new URL(
              // "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?VERSION=1.1.0&REQUEST=GetCapabilities");
              "http://sigel.aneel.gov.br/arcgis/services/SIGEL/Geracao/MapServer/WMSServer?request=getcapabilities");
      // "http://www2.sipam.gov.br/geoserver/wms?service=WMS&request=GetCapabilities");
    } catch (MalformedURLException e) {
      // will not happen
    }

    WebMapServer wms = null;
    try {
      wms = new WebMapServer(url);

      WMSCapabilities capabilities = wms.getCapabilities();

      String serverName = capabilities.getService().getName();
      String serverTitle = capabilities.getService().getTitle();
      System.out.println(
          "Capabilities retrieved from server: " + serverName + " (" + serverTitle + ")");

      System.out.println(capabilities.getService().getOnlineResource());

      // gets the top most layer, which will contain all the others
      Layer rootLayer = capabilities.getLayer();

      System.out.println(rootLayer.getName());

      System.out.println("===============");

      Layer[] layers2 = WMSUtils.getNamedLayers(capabilities);
      List<Layer> layers = capabilities.getLayerList();
      CRSEnvelope env = null;
      int i = 0;
      for (Layer layer : layers2) {
        i++;
        System.out.println("Layer: (" + i + ")" + layer.getName());
        System.out.println("       " + layer.getTitle());
        System.out.println("       " + layer.getChildren().length);
        System.out.println("       " + layer.getBoundingBoxes());
        env = layer.getLatLonBoundingBox();
        System.out.println("       " + env.getLowerCorner() + " x " + env.getUpperCorner());

        List<String> formats = wms.getCapabilities().getRequest().getGetMap().getFormats();

        System.out.println("formats: " + formats);
      }
      System.out.println("===============");

      GetMapRequest request = wms.createGetMapRequest();

      request.setFormat("image/png");
      request.setDimensions(
          "583", "420"); // sets the dimensions of the image to be returned from the server
      request.setTransparent(true);
      request.setSRS("EPSG:4326");
      request.setBBox(env);
      request.addLayer(layers.get(layers.size() - 1));

      System.out.println(request.getFinalURL());

      GetMapResponse response = (GetMapResponse) wms.issueRequest(request);

      ImageIO.write(
          ImageIO.read(response.getInputStream()),
          "png",
          new File("/Users/otos/development/projeto/demoiselle-spatial/tmp/file.png"));

      System.out.println(request.getFinalURL());

    } catch (IOException e) {
      // There was an error communicating with the server
      // For example, the server is down
    } catch (ServiceException e) {
      // The server returned a ServiceException (unusual in this case)
    } catch (SAXException e) {
      // Unable to parse the response from the server
      // For example, the capabilities it returned was not valid
    }
  }
  public synchronized void render(
      Graphics2D destination, ReferencedEnvelope bounds, IProgressMonitor monitor)
      throws RenderException {

    int endLayerStatus = ILayer.DONE;
    try {
      if (bounds == null || bounds.isNull()) {
        bounds = getContext().getImageBounds();
      }

      if (monitor.isCanceled()) return;

      getContext().setStatus(ILayer.WAIT);

      WebMapServer wms = getWMS();

      GetMapRequest request = wms.createGetMapRequest();

      // put in default exception format we understand as a client
      // (if suppoted by the server)
      WMSCapabilities capabilities = wms.getCapabilities();
      if (capabilities
          .getRequest()
          .getGetMap()
          .getFormats()
          .contains(GetMapRequest.EXCEPTION_XML)) {
        request.setExceptions(GetMapRequest.EXCEPTION_XML);
      }
      setImageFormat(wms, request);

      if (monitor.isCanceled()) return;

      double currScale = getContext().getViewportModel().getScaleDenominator();
      List<ILayer> layers = getLayers();
      for (int i = layers.size() - 1; i >= 0; i--) {
        ILayer ilayer = layers.get(i);
        Layer layer;
        double minScale = 0;
        double maxScale = Double.MAX_VALUE;
        layer = ilayer.getResource(org.geotools.data.ows.Layer.class, null);
        // check if there are min/max scale rules
        StyleBlackboard sb = (StyleBlackboard) ilayer.getStyleBlackboard();
        Style style = (Style) sb.lookup(Style.class);
        if (style != null) {
          Rule rule = style.getFeatureTypeStyles()[0].getRules()[0];
          minScale = rule.getMinScaleDenominator();
          maxScale = rule.getMaxScaleDenominator();
        }

        if (currScale >= minScale && currScale <= maxScale) {
          // check for a wms style
          StyleImpl wmsStyle =
              (StyleImpl) ilayer.getStyleBlackboard().get(WMSStyleContent.WMSSTYLE);
          if (wmsStyle != null) {
            request.addLayer(layer, wmsStyle);
          } else {
            request.addLayer(layer);
          }
        }
      }

      if (monitor.isCanceled()) return;

      List<Layer> wmsLayers = getWMSLayers();
      if (wmsLayers == null || wmsLayers.isEmpty()) {
        endLayerStatus = ILayer.WARNING;
        return;
      }

      // figure out request CRS
      String requestCRScode = findRequestCRS(wmsLayers, getViewportCRS(), getContext().getMap());
      // TODO: make findRequestCRS more efficient (we are running CRS.decode at *least* twice)
      CoordinateReferenceSystem requestCRS = CRS.decode(requestCRScode);

      // figure out viewport
      //            ReferencedEnvelope viewport;
      //            Envelope viewportBBox = getViewportBBox();
      //            CoordinateReferenceSystem viewportCRS = getViewportCRS();
      //            if (viewportBBox == null) {
      //                // change viewport to world
      //                viewportBBox = new Envelope(-180, 180, -90, 90);
      //                if (!DefaultGeographicCRS.WGS84.equals(viewportCRS)) { // reproject
      //                    viewport = new ReferencedEnvelope(viewportBBox,
      // DefaultGeographicCRS.WGS84);
      //                    viewportBBox = viewport.transform(viewportCRS, true);
      //                }
      //            }

      ReferencedEnvelope requestBBox = null;
      Envelope backprojectedBBox = null; // request bbox projected to the viewport crs
      //            viewport = new ReferencedEnvelope(viewportBBox, viewportCRS);
      //            requestBBox = calculateRequestBBox(wmsLayers, viewport, requestCRS);

      requestBBox = calculateRequestBBox(wmsLayers, bounds, requestCRS, capabilities.getVersion());

      // check that a request is needed (not out of a bounds, invalid, etc)
      if (requestBBox == NILL_BOX) {
        endLayerStatus = ILayer.WARNING;
        return;
      }
      assert requestBBox.getCoordinateReferenceSystem().equals(requestCRS);

      if (requestBBox.getCoordinateReferenceSystem().equals(getViewportCRS())) {
        backprojectedBBox = (Envelope) requestBBox;
      } else {
        backprojectedBBox = (Envelope) requestBBox.transform(getViewportCRS(), true);
      }

      if (WMSPlugin.isDebugging(Trace.RENDER)) {
        WMSPlugin.trace("Viewport CRS: " + getViewportCRS().getName()); // $NON-NLS-1$
        WMSPlugin.trace("Request CRS: " + requestCRS.getName()); // $NON-NLS-1$
        WMSPlugin.trace("Context Image bounds: " + getContext().getImageBounds()); // $NON-NLS-1$
        WMSPlugin.trace("Request BBox  bounds: " + requestBBox); // $NON-NLS-1$
        WMSPlugin.trace("Backprojected request bounds: " + backprojectedBBox); // $NON-NLS-1$
      }

      Service wmsService = capabilities.getService();
      Dimension maxDimensions = new Dimension(wmsService.getMaxWidth(), wmsService.getMaxHeight());
      //            Dimension imageDimensions =
      // calculateImageDimensions(getContext().getMapDisplay()
      //                    .getDisplaySize(), maxDimensions, getViewportBBox(), backprojectedBBox);
      Dimension imageDimensions =
          calculateImageDimensions(
              getContext().getImageSize(), maxDimensions, bounds, backprojectedBBox);
      if (imageDimensions.height < 1 || imageDimensions.width < 1) {
        endLayerStatus = ILayer.WARNING;
        return;
      }
      request.setDimensions(
          imageDimensions.width + "", imageDimensions.height + ""); // $NON-NLS-1$ //$NON-NLS-2$
      // epsg could be under identifiers or authority.
      Set<ReferenceIdentifier> identifiers = requestCRS.getIdentifiers();
      String srs = identifiers.isEmpty() ? EPSG_4326 : identifiers.iterator().next().toString();
      request.setSRS(srs); // EPSG_4326
      request.setBBox(requestBBox);
      // request.setBBox(requestBBox.getMinX() + "," + requestBBox.getMinY()+ "," +
      // requestBBox.getMaxX()+ "," + requestBBox.getMaxY());

      if (monitor.isCanceled()) return;

      setFilter(wms, request);

      // request.setProperty("DACS_ACS", null);
      BufferedImage image = readImage(wms, request, monitor);

      if (monitor.isCanceled()) return;

      if (image == null) {
        Exception e = new RuntimeException(Messages.BasicWMSRenderer2_unable_to_decode_image);
        throw wrapException(e);
      }

      // backprojectedBBox or viewportBBox
      renderGridCoverage(destination, backprojectedBBox, imageDimensions, requestBBox, image);

    } catch (Exception e) {
      if (e instanceof RenderException) throw (RenderException) e;
      throw new RenderException(e);
    } finally {
      getContext().setStatus(endLayerStatus);
      if (endLayerStatus == ILayer.DONE) {
        // clear the status message (rendering was successful)
        getContext().setStatusMessage(null);
      }
    }
  }
  private static void writeLayer(ILayer layer, BufferedWriter out) throws IOException {
    Layer wmsLayer = layer.getResource(Layer.class, null);
    WebMapServer wms = layer.getResource(WebMapServer.class, null);
    WMSCapabilities caps = wms.getCapabilities();
    String version = caps.getVersion();

    String title = wms.getCapabilities().getService().getTitle();
    int hidden = layer.isVisible() ? 1 : 0;
    int info = layer.isApplicable("info") ? 1 : 0; // $NON-NLS-1$
    String get = caps.getRequest().getGetCapabilities().getGet().toExternalForm();
    System.out.println(get);
    if (get.endsWith("&")) get = get.substring(0, get.length() - 1); // $NON-NLS-1$
    append(
        4,
        out,
        "<Layer hidden=\""
            + hidden
            + "\" queryable=\""
            + info
            + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    append(
        6,
        out,
        "<Server service=\"OGC:WMS\" title=\""
            + title
            + "\" version=\""
            + version
            + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    append(
        8,
        out,
        "<OnlineResource method=\"GET\" xlink:href=\""
            + get
            + "\" xlink:type=\"simple\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
    append(6, out, "</Server>"); // $NON-NLS-1$
    append(6, out, "<Name>" + wmsLayer.getName() + "</Name>"); // $NON-NLS-1$ //$NON-NLS-2$
    append(6, out, "<Title>" + wmsLayer.getTitle() + "</Title>"); // $NON-NLS-1$ //$NON-NLS-2$
    if (!Double.isNaN(wmsLayer.getScaleHintMin()))
      append(
          6,
          out,
          "<sld:MinScaleDenominator>"
              + wmsLayer.getScaleHintMin()
              + "</sld:MinScaleDenominator>"); //$NON-NLS-1$ //$NON-NLS-2$
    if (!Double.isNaN(wmsLayer.getScaleHintMax()))
      append(
          6,
          out,
          "<sld:MaxScaleDenominator>"
              + wmsLayer.getScaleHintMax()
              + "</sld:MaxScaleDenominator>"); //$NON-NLS-1$ //$NON-NLS-2$
    for (String srs : (Set<String>) wmsLayer.getSrs()) {
      append(6, out, "<SRS>" + srs + "</SRS>"); // $NON-NLS-1$ //$NON-NLS-2$
    }
    append(6, out, "<FormatList>"); // $NON-NLS-1$

    boolean first = true; // TODO: look up preferences?
    for (String format : caps.getRequest().getGetMap().getFormats()) {
      if (first) {
        append(
            8, out, "<Format current=\"1\">" + format + "</Format>"); // $NON-NLS-1$ //$NON-NLS-2$
        first = false;
      }
      append(8, out, "<Format>" + format + "</Format>"); // $NON-NLS-1$ //$NON-NLS-2$
    }
    append(6, out, "</FormatList>"); // $NON-NLS-1$

    first = true; // TODO: look up on styleblackboard?
    append(6, out, "<StyleList>"); // $NON-NLS-1$
    Object styles = wmsLayer.getStyles();
    List list;
    if (styles instanceof String) list = Collections.singletonList(styles);
    else if (styles instanceof List) list = (List) styles;
    else list = Collections.emptyList();
    for (Iterator<Object> iter = list.iterator(); iter.hasNext(); ) {
      Object next = iter.next();
      if (next instanceof String) {
        String style = (String) next;
        first = writeStyle(style, style, first, out);
      } else if (next instanceof StyleImpl) {
        StyleImpl style = (StyleImpl) next;
        writeStyle(style.getName(), style.getTitle().toString(), first, out);
      }
    }
    append(6, out, "</StyleList>"); // $NON-NLS-1$
    append(4, out, "</Layer>"); // $NON-NLS-1$
  }
Exemplo n.º 9
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  @RequestMapping(value = "/{wsName}/{name}", method = RequestMethod.POST)
  public @ResponseBody JSONObj create(
      @PathVariable String wsName,
      @PathVariable String name,
      @RequestBody JSONObj obj,
      HttpServletRequest req)
      throws IOException {
    Catalog cat = geoServer.getCatalog();
    CatalogFactory factory = cat.getFactory();

    WorkspaceInfo workspace = findWorkspace(wsName);
    StoreInfo store = null;

    JSONObj params = obj.object("connection");
    if (params == null) {
      throw new IllegalArgumentException("connection parameters required");
    }
    if (params.has("raster")) {
      String url = params.str("raster");
      CoverageStoreInfo info = factory.createCoverageStore();
      info.setWorkspace(workspace);
      info.setType(name);

      // connect and defaults
      info.setURL(url);
      info.setType(obj.str("type"));
      try {
        GridCoverageReader reader = info.getGridCoverageReader(null, null);
        Format format = reader.getFormat();
        info.setDescription(format.getDescription());
        info.setEnabled(true);
      } catch (IOException e) {
        info.setError(e);
        info.setEnabled(false);
      }
      store = info;
    } else if (params.has("url")
        && params.str("url").toLowerCase().contains("Service=WMS")
        && params.str("url").startsWith("http")) {
      WMSStoreInfo info = factory.createWebMapServer();
      info.setWorkspace(workspace);
      info.setType(name);

      // connect and defaults
      info.setCapabilitiesURL(params.str("url"));
      try {
        WebMapServer service = info.getWebMapServer(new NullProgressListener());
        info.setDescription(service.getInfo().getDescription());
        info.setEnabled(true);
      } catch (Throwable e) {
        info.setError(e);
        info.setEnabled(false);
      }
      store = info;
    } else {
      HashMap map = new HashMap(params.raw());
      Map resolved = ResourcePool.getParams(map, cat.getResourceLoader());
      DataAccess dataStore = DataAccessFinder.getDataStore(resolved);
      if (dataStore == null) {
        throw new IllegalArgumentException(
            "Connection parameters incomplete (does not match an available data store, coverage store or wms store).");
      }
      DataStoreInfo info = factory.createDataStore();
      info.setWorkspace(workspace);
      info.setType(name);
      info.getConnectionParameters().putAll(map);
      try {
        info.setDescription(dataStore.getInfo().getDescription());
        info.setEnabled(true);
      } catch (Throwable e) {
        info.setError(e);
        info.setEnabled(false);
      }
      store = info;
    }
    boolean refresh = define(store, obj);
    if (refresh) {
      LOG.log(
          Level.FINE, "Inconsistent: default connection used for store creation required refresh");
    }
    cat.add(store);

    return storeDetails(new JSONObj(), store, req);
  }