示例#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;
  }
  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
    }
  }