Exemplo n.º 1
0
 private void addLayersListeners() {
   MapContent generalContext = ((MapTilesCache) cache).getMapDisplayContext();
   MapContent lightweightContext = ((MapTilesCache) lightweightCache).getMapDisplayContext();
   generalChangesReflector = new LayersChangesReflector(cache);
   lightChangesReflector = new LayersChangesReflector(lightweightCache);
   screenChangesReflector = new ScreenChangesReflector();
   for (Layer layer : generalContext.layers()) {
     layer.addMapLayerListener(generalChangesReflector);
   }
   for (Layer layer : lightweightContext.layers()) {
     layer.addMapLayerListener(lightChangesReflector);
   }
   if (screenContext != null) {
     for (Layer layer : screenContext.layers()) {
       layer.addMapLayerListener(screenChangesReflector);
     }
   }
 }
Exemplo n.º 2
0
 private void removeLayersListeners() {
   MapContent generalContext = ((MapTilesCache) cache).getMapDisplayContext();
   MapContent lightweightContext = ((MapTilesCache) lightweightCache).getMapDisplayContext();
   if (generalChangesReflector != null) {
     for (Layer layer : generalContext.layers()) {
       layer.removeMapLayerListener(generalChangesReflector);
     }
   }
   if (lightChangesReflector != null) {
     for (Layer layer : lightweightContext.layers()) {
       layer.removeMapLayerListener(lightChangesReflector);
     }
   }
   if (screenContext != null && screenChangesReflector != null) {
     for (Layer layer : screenContext.layers()) {
       layer.removeMapLayerListener(screenChangesReflector);
     }
   }
 }
  public void selectFeatures(int x, int y) {
    int pixelDelta = 2;
    LatLon clickUL = Main.map.mapView.getLatLon(x - pixelDelta, y - pixelDelta);
    LatLon clickLR = Main.map.mapView.getLatLon(x + pixelDelta, y + pixelDelta);

    Envelope envelope =
        new Envelope(
            Math.min(clickUL.lon(), clickLR.lon()),
            Math.max(clickUL.lon(), clickLR.lon()),
            Math.min(clickUL.lat(), clickLR.lat()),
            Math.max(clickUL.lat(), clickLR.lat()));

    ReferencedEnvelope mapArea = new ReferencedEnvelope(envelope, crsOSMI);

    Intersects filter = ff.intersects(ff.property("msGeometry"), ff.literal(mapArea));
    //
    // Select features in all layers
    //
    content.layers().clear();

    // Iterate through features and build a list that intersects the above
    // envelope
    for (int idx = 0; idx < arrFeatures.size(); ++idx) {
      OSMIFeatureTracker tracker = arrFeatures.get(idx);
      FeatureCollection<SimpleFeatureType, SimpleFeature> features = tracker.getFeatures();

      SimpleFeatureSource tempfs = DataUtilities.source(features);
      FeatureCollection<SimpleFeatureType, SimpleFeature> selectedFeatures;

      try {
        selectedFeatures = tempfs.getFeatures(filter);
        Set<FeatureId> IDs = new HashSet<>();

        try (FeatureIterator<SimpleFeature> iter = selectedFeatures.features()) {

          Main.info("Selected features " + selectedFeatures.size());

          while (iter.hasNext()) {
            SimpleFeature feature = iter.next();
            IDs.add(feature.getIdentifier());
          }
        }

        geometryType = selectGeomType.get(idx + layerOffset);
        Style style = createDefaultStyle(idx + layerOffset, IDs);
        content.addLayer(new FeatureLayer(features, style));
      } catch (IOException e) {
        Main.error(e);
      }
    }

    bIsChanged = true;
  }
Exemplo n.º 4
0
  /** Called when a map layer has been removed */
  public void layerRemoved(MapLayerListEvent event) {
    Layer layer = event.getElement();
    if (layerTable != null) {
      layerTable.onRemoveLayer(layer);
    }
    redrawBaseImage = true;

    if (content.layers().size() == 0) {
      clearFields();
    } else {
      setFullExtent();
    }
    if (!isDisposed()) redraw();
  }
  /**
   * @param wfsClient
   * @throws NoSuchAuthorityCodeException
   * @throws FactoryException
   * @throws IOException
   * @throws ParseException
   * @throws NoSuchElementException
   * @throws IndexOutOfBoundsException
   */
  public void loadFeatures(GeoFabrikWFSClient wfsClient)
      throws NoSuchAuthorityCodeException, FactoryException, IOException, IndexOutOfBoundsException,
          NoSuchElementException, ParseException {
    String typeNames[] = wfsClient.getTypeNames();

    content.layers().clear();
    selectGeomType.clear();
    selectGeomType.add(GeomType.POINT);

    ProgressMonitor monitor = new ProgressMonitor(Main.map.mapView, "Loading features", "", 0, 100);

    for (int idx = 1; idx < typeNames.length; ++idx) {
      String typeName = typeNames[idx];
      Set<FeatureId> selectedFeatures = new HashSet<>();

      monitor.setProgress(100 / typeNames.length * idx);
      FeatureCollection<SimpleFeatureType, SimpleFeature> features =
          wfsClient.getFeatures(typeName, monitor);
      setGeometry(selectGeomType, typeName);

      Main.info("Osm Inspector Features size: " + features.size());

      OSMIFeatureTracker tracker = arrFeatures.get(idx - layerOffset);
      tracker.mergeFeatures(features);

      FeatureIterator<SimpleFeature> it = tracker.getFeatures().features();

      while (it.hasNext()) {
        BugInfo theInfo = new BugInfo(it.next(), osmiBugInfo.size());
        if (!osmiBugInfo.keySet().contains(theInfo)) {
          osmiBugInfo.put(theInfo, theInfo.bugId);
        }
      }

      Style style = createDefaultStyle(idx, selectedFeatures);
      content.addLayer(new FeatureLayer(tracker.getFeatures(), style));
    }

    osmiIndex.append(osmiBugInfo);

    monitor.setProgress(100);
    monitor.close();
    bIsChanged = true;
    // dialog.updateDialog(this);
    dialog.refreshModel();
    // dialog.updateNextPrevAction(this);

    this.updateView();
  }
Exemplo n.º 6
0
  public static void legendAction() {
    try {
      LegendColorTable.countTable = 0;
      Layer layer = content.layers().get(content.layers().size() - 1);
      SimpleFeatureSource featureSource = (SimpleFeatureSource) layer.getFeatureSource();
      String fieldName = getFieldForColour(featureSource);

      if (fieldName != null && fieldName.length() > 0) {

        Style style = createStyle(featureSource, fieldName);

        content.removeLayer(layer);
        Layer newLayer = new FeatureLayer(featureSource, style);
        content.addLayer(newLayer);

      } else throw new NullPointerException("Error occured during making legend");

    } catch (ArrayIndexOutOfBoundsException ex) {
      JOptionPane.showMessageDialog(null, "No layers are added !");
    } catch (Exception ex) {
      ex.printStackTrace();
      // JOptionPane.showMessageDialog(null, ex.getMessage());
    }
  }
Exemplo n.º 7
0
  /**
   * Gets the full extent of map context's layers. The only reason this method is defined is to
   * avoid having try-catch blocks all through other methods.
   */
  private void setFullExtent() {
    if (content != null && content.layers().size() > 0) {
      try {

        fullExtent = content.getMaxBounds();

        /*
         * Guard agains degenerate envelopes (e.g. empty
         * map layer or single point feature)
         */
        if (fullExtent == null) {
          // set arbitrary bounds centred on 0,0
          fullExtent = worldEnvelope(); // new ReferencedEnvelope(-1, 1, -1, 1,
          // context.getCoordinateReferenceSystem());

        }
        // else {
        // double w = fullExtent.getWidth();
        // double h = fullExtent.getHeight();
        // double x = fullExtent.getMinimum(0);
        // double y = fullExtent.getMinimum(1);
        //
        // double xmin = x;
        // double xmax = x + w;
        // if (w <= 0.0) {
        // xmin = x - 1.0;
        // xmax = x + 1.0;
        // }
        //
        // double ymin = y;
        // double ymax = y + h;
        // if (h <= 0.0) {
        // ymin = y - 1.0;
        // ymax = y + 1.0;
        // }
        //
        // fullExtent = new ReferencedEnvelope(xmin, xmax, ymin, ymax,
        // context.getCoordinateReferenceSystem());
        // }

      } catch (Exception ex) {
        throw new IllegalStateException(ex);
      }
    } else {
      fullExtent = null;
    }
  }
Exemplo n.º 8
0
  /**
   * Called when a new map layer has been added. Sets the layer as selected (for queries) and, if
   * the layer table is being used, adds the new layer to the table.
   */
  public void layerAdded(MapLayerListEvent event) {
    Layer layer = event.getElement();
    if (layerTable != null) {
      layerTable.onAddLayer(layer);
    }
    layer.setSelected(true);
    redrawBaseImage = true;

    boolean atFullExtent = equalsFullExtent(getDisplayArea());
    boolean firstLayer = content.layers().size() == 1;
    if (firstLayer || atFullExtent) {
      reset();
      if (firstLayer) {
        setCrs(layer.getBounds().getCoordinateReferenceSystem());
        return;
      }
    }
    if (!isDisposed()) redraw();
  }
Exemplo n.º 9
0
  /**
   * Set the map context for this map pane to display
   *
   * @param content the map context
   */
  public void setMapContent(MapContent content) {
    if (this.content != content) {

      if (this.content != null) {
        this.content.removeMapLayerListListener(this);
        // for( MapLayer layer : this.context.getLayers() ) {
        // if (layer instanceof ComponentListener) {
        // removeComponentListener((ComponentListener) layer);
        // }
        // }
      }

      this.content = content;

      if (content != null) {
        this.content.addMapLayerListListener(this);
        this.content.addMapBoundsListener(this);

        // set all layers as selected by default for the info tool
        for (Layer layer : content.layers()) {
          layer.setSelected(true);
          // if (layer instanceof ComponentListener) {
          // addComponentListener((ComponentListener) layer);
          // }
        }

        setFullExtent();
      }

      if (renderer != null) {
        renderer.setMapContent(this.content);
      }

      MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.NEW_CONTEXT);
      publishEvent(ev);
    }
  }
Exemplo n.º 10
0
  @SuppressWarnings("deprecation")
  public void handleEvent(Event event) {

    curPaintArea = getVisibleRect();

    // System.out.println("event: " + event.type);
    if (event.type == SWT.MouseDown) {
      startX = event.x;
      startY = event.y;
      // start mouse activity
      mouseDown = true;
    } else if (event.type == SWT.MouseUp) {
      endX = event.x;
      endY = event.y;

      boolean mouseWasMoved = startX != endX || startY != endY;
      if (toolCanMove && mouseWasMoved) {
        // if the tool is able to move draw the moved image
        afterImageMove();
      }
      // stop mouse activity
      mouseDown = false;
      isDragging = false;
    } else if (event.type == SWT.Paint) {
      // System.out.println("PAINT CALLED (DOESN'T MEAN I'M DRAWING)");

      if (acceptRepaintRequests) {
        gc = event.gc;

        // System.out.println(toolCanDraw + "/" + toolCanMove + "/" + isDragging + "/" +
        // redrawBaseImage);

        /*
         * if the mouse is dragging and the current tool can
         * move the map we just draw what we already have
         * on white background. At the end of the moving
         * we will take care of adding the missing pieces.
         */
        if (toolCanMove && isDragging) {
          // System.out.println("toolCanMove && isDragging");
          if (gc != null && !gc.isDisposed() && swtImage != null) {
            /*
             * double buffer necessary, since the SWT.NO_BACKGROUND
             * needed by the canvas to properly draw background, doesn't
             * clean the parts outside the bounds of the moving panned image,
             * giving a spilling image effect.
             */
            Image tmpImage = new Image(getDisplay(), curPaintArea.width, curPaintArea.height);
            GC tmpGc = new GC(tmpImage);
            tmpGc.setBackground(white);
            tmpGc.fillRectangle(0, 0, curPaintArea.width, curPaintArea.height);
            tmpGc.drawImage(swtImage, imageOrigin.x, imageOrigin.y);
            gc.drawImage(tmpImage, 0, 0);
            tmpImage.dispose();
          }
          return;
        }

        /*
         * if the mouse is dragging and the current tool can
         * draw a boundingbox while dragging, we draw the box
         * keeping the current drawn image
         */
        if (toolCanDraw && toolManager.getCursorTool().isDrawing() && isDragging) {
          // System.out.println("draw box: " + startX + "/" + startY + "/" + endX +
          // "/" + endY);
          if (swtImage != null) {
            drawFinalImage(swtImage);
          }
          gc.setXORMode(true);

          org.eclipse.swt.graphics.Color fC = gc.getForeground();
          gc.setLineStyle(cursorToolLineStyle);
          gc.setLineWidth(cursorToolLineWidth);
          gc.setForeground(cursorToolColor);
          gc.drawRectangle(startX, startY, endX - startX, endY - startY);

          gc.setForeground(fC);
          gc.setXORMode(false);
          return;
        }

        if (!toolCanDraw && !toolCanMove && isDragging) {
          return;
        }

        if (curPaintArea == null || content == null || renderer == null) {
          return;
        }

        if (content.layers().size() == 0) {
          // if no layers available, return only if there are also no overlays

          gc.setForeground(yellow);
          gc.fillRectangle(0, 0, curPaintArea.width + 1, curPaintArea.height + 1);
          if (overlayImage == null) return;
        }

        final ReferencedEnvelope mapAOI = content.getViewport().getBounds();
        if (mapAOI == null) {
          return;
        }

        if (redrawBaseImage) {
          MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STARTED);
          publishEvent(ev);

          baseImage =
              new BufferedImage(
                  curPaintArea.width + 1, curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);
          Graphics2D g2d = baseImage.createGraphics();
          g2d.fillRect(0, 0, curPaintArea.width + 1, curPaintArea.height + 1);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

          // renderer.setContext(context);
          java.awt.Rectangle awtRectangle = Utils.toAwtRectangle(curPaintArea);
          renderer.paint(g2d, awtRectangle, mapAOI, getWorldToScreenTransform());
          // swtImage.dispose();

          if (swtImage != null && !swtImage.isDisposed()) {
            swtImage.dispose();
            swtImage = null;
          }
          // System.out.println("READRAWBASEIMAGE");
          swtImage =
              new Image(
                  getDisplay(),
                  awtToSwt(baseImage, curPaintArea.width + 1, curPaintArea.height + 1));
        }

        if (swtImage != null) {
          drawFinalImage(swtImage);
        }

        MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STOPPED);
        publishEvent(ev);
        clearLabelCache = true;
        onRenderingCompleted();
        redrawBaseImage = false;
      }
    }
  }