Buffer

Download Sample Viewer

Description

This application shows how to perform a buffer around a point or a polyline. The buffer operation used is the static method GeometryEngine.buffer().

Code snippet


    // buffer a polyline
    bufferedArea = GeometryEngine.buffer(
        polyLine,
        jMap.getSpatialReference(),
        BUFFER_DISTANCE,
        jMap.getSpatialReference().getUnit());
  

Sample Code

/* Copyright 2014 Esri

All rights reserved under the copyright laws of the United States
and applicable international laws, treaties, and conventions.

You may freely redistribute and use this sample code, with or
without modification, provided you include the original copyright
notice and use restrictions.

See the use restrictions.*/
package com.esri.client.samples.geometry;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Line;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polyline;
import com.esri.core.geometry.Segment;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.Style;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.GraphicsLayer;
import com.esri.map.JMap;
import com.esri.map.LayerList;
import com.esri.map.MapOverlay;

/**
 * This application computes and draws a buffer around a point or a polyline.
 * <ul>
 * <li>Right-click to draw a buffer around a point.</li>
 * <li>Create a polyline by clicking on the map - single-left-click to extend; double-click to end. 
 * Then, right-click to compute and draw a buffer on the drawn polyline.</li>
 * </ul>
 * To get the buffer, this application uses the static method GeometryEngine.buffer().
 */
public class BufferApp {

  private JMap map;
  private GraphicsLayer graphicsLayer;
  // buffer distance: will be in the map's units, meters in this case
  private static final double BUFFER_DISTANCE = 10000; // 10 km
  // symbology
  final static SimpleLineSymbol SYM_LINE   = new SimpleLineSymbol(Color.RED, 2.0f);
  final static SimpleMarkerSymbol SYM_POINT =
      new SimpleMarkerSymbol(new Color(200, 0, 0, 200), 8, Style.CIRCLE);
  final static SimpleFillSymbol SYM_BUFFER =
      new SimpleFillSymbol(new Color(0, 0, 255, 80), SYM_LINE);

  // ------------------------------------------------------------------------
  // Constructors
  // ------------------------------------------------------------------------
  public BufferApp() {
  }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Map overlay to handle mouse-click events.
   */
  class UserCommandOverlay extends MapOverlay {

    private static final long serialVersionUID = 1L;
    // map
    JMap jMap;
    // layer to which graphics will be added to
    GraphicsLayer gLayer;

    Polyline polyLine = new Polyline();
    Point    prevPoint;
    boolean  startOver = false;
    Geometry bufferedArea = null;

    /**
     * Constructor
     * @param jMap JMap to which this overlay belongs.
     * @param graphicsLayer
     */
    UserCommandOverlay(JMap jMap, GraphicsLayer graphicsLayer) {
      this.jMap = jMap;
      this.gLayer = graphicsLayer;
    }

    /**
     * Handle mouse-clicks.
     * On left-click - draws either a polyline or a point.
     * On right-click - computes and draws the buffer of the polyline or point.
     */
    @Override
    public void onMouseClicked(MouseEvent event) {

      super.onMouseClicked(event);

      if (startOver) {
        gLayer.removeAll();
        startOver = false;
      }

      // point clicked
      Point currPoint = jMap.toMapPoint(event.getX(), event.getY());

      // on right-click, compute the buffer
      if (event.getButton() == MouseEvent.BUTTON3) {
        if (polyLine.getSegmentCount() > 0) {
          // polyline
          bufferedArea = GeometryEngine.buffer(
              polyLine,
              jMap.getSpatialReference(),
              BUFFER_DISTANCE,
              jMap.getSpatialReference().getUnit());
        } else if (prevPoint != null) {
          // line
          bufferedArea = GeometryEngine.buffer(
              prevPoint,
              jMap.getSpatialReference(),
              BUFFER_DISTANCE,
              jMap.getSpatialReference().getUnit());
        } else {
          // point
          bufferedArea = GeometryEngine.buffer(
              currPoint,
              jMap.getSpatialReference(),
              BUFFER_DISTANCE,
              jMap.getSpatialReference().getUnit());
          Graphic currPointGraphic = new Graphic(currPoint, BufferApp.SYM_POINT);
          gLayer.addGraphic(currPointGraphic);
        }

        // add the buffered area to the graphics layer
        Graphic bufferedGraphic = new Graphic(bufferedArea, BufferApp.SYM_BUFFER);
        gLayer.addGraphic(bufferedGraphic);

        // start new on next click
        startOver = true;
        prevPoint = null;
        polyLine.setEmpty();

        return;
      }

      Graphic currPointGraphic = new Graphic(currPoint, BufferApp.SYM_POINT);
      gLayer.addGraphic(currPointGraphic);

      // on left-clicks, extend the current polyline
      if (prevPoint != null) {
        Line line = new Line();
        line.setStart(prevPoint);
        line.setEnd(currPoint);

        Segment segment = new Line();
        segment.setStart(prevPoint);
        segment.setEnd(currPoint);
        polyLine.addSegment(segment, false);

        Graphic lineGraphic = new Graphic(polyLine, BufferApp.SYM_LINE);
        gLayer.addGraphic(lineGraphic);
      }

      prevPoint = currPoint;
    }
  }
  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------
  /**
   * Starting point of this application.
   * @param args arguments to this application.
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          BufferApp bufferApp = new BufferApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = bufferApp.createWindow();
          appWindow.add(bufferApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          // on any error, display the stack trace.
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() throws Exception {
    // application content
    JLayeredPane contentPane = createContentPane();

    // description
    JPanel description = createDescription();
    contentPane.add(description);

    // map
    map = createMap();
    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame();
    window.setBounds(100, 100, 1000, 700);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BorderLayout(0, 0));
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        map.dispose();
      }
    });
    return window;
  }

  /**
   * Creates a content pane.
   * @return a content pane.
   */
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }

  /**
   * Creates a map.
   * @return a map.
   */
  private JMap createMap() throws Exception {
    final JMap jMap = new JMap();
    // -----------------------------------------------------------------------------------------
    // Base Layer - set initial map extent to USA
    // -----------------------------------------------------------------------------------------
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(
        "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
    jMap.setExtent(new Envelope(-8304000, 4920280, -7703576, 5320810));
    LayerList layers = jMap.getLayers();
    layers.add(baseLayer);

    // -----------------------------------------------------------------------------------------
    // Graphics Layer - to add lines
    // -----------------------------------------------------------------------------------------
    graphicsLayer = new GraphicsLayer();
    layers.add(graphicsLayer);

    jMap.addMapOverlay(new UserCommandOverlay(jMap, graphicsLayer));

    return jMap;
  }

  /**
   * Creates a description for this application.
   * @return description
   */
  private JPanel createDescription() {
    JPanel descriptionContainer = new JPanel();
    descriptionContainer.setLayout(new BoxLayout(descriptionContainer, 0));
    descriptionContainer.setLocation(10,10);
    descriptionContainer.setSize(330, 110);
    JTextArea description = new JTextArea(
        "This application computes and draws a buffer around a point or a polyline.\n" +
            "- Point: right-click to draw a buffer around a point\n" +
            "- Polyline: create a polyline by clicking on the map with single-left-clicks; " +
        "right-click to draw a buffer around the polyline.");
    description.setFont(new Font("Verdana", Font.PLAIN, 11));
    description.setForeground(Color.WHITE);
    description.setEditable(false);
    description.setLineWrap(true);
    description.setWrapStyleWord(true);
    description.setBackground(new Color(0, 0, 0, 180));
    description.setBorder(BorderFactory.createEmptyBorder(5,10,5,5));
    descriptionContainer.add(description);
    descriptionContainer.setBackground(new Color(0, 0, 0, 0));
    descriptionContainer.setBorder(new LineBorder(Color.BLACK, 3, false));
    return descriptionContainer;
  }
}
Feedback on this topic?