Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Java

Project point

Download Sample Viewer

Description

This application shows how to project a point from decimal degrees (latitude and longitude values) to the spatial reference of the map.

Code snippet


    // project our lat-lon point (in WGS84) to the map's spatial reference
    Point pointProj = (Point) GeometryEngine.project(
        point, wgs84, jMap.getSpatialReference());
  

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.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.Graphic;
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;

/**
 * This application shows how to project a point from decimal degrees (latitude and
 * longitude values) to the spatial reference of the map.  The spatial reference of the
 * map (<code>JMap</code>) is set when the first layer is added to the map, in this
 * case the tiled map service layer (<code>ArcGISTiledMapServiceLayer</code>).  If a
 * tiled layer is used, it should be added to the map first.
 * <p>
 * The projection operation is performed using the static method
 * <code>GeometryEngine.project</code>, which will project any <code>Geometry</code> object
 * from one spatial reference to another.
 */
public class ProjectPointApp {

  // symbology
  final static SimpleMarkerSymbol SYM_POINT =
      new SimpleMarkerSymbol(Color.RED, 16, Style.CIRCLE);

  // resources
  private final String BASE_LAYER_URL =
      "http://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer";

  // define the spatial reference of our latitude-longitude points, WGS84
  private static SpatialReference wgs84 = SpatialReference.create(4326);

  // components
  private JMap map;
  private GraphicsLayer graphicsLayer;
  private JTextField longitude;
  private JTextField latitude;
  private JComponent contentPane;

  // sizing constants
  private int PANEL_WIDTH = 260;

  // ------------------------------------------------------------------------
  // Constructor
  // ------------------------------------------------------------------------
  public ProjectPointApp() {
  }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  private void projectPoint(Point point) {

    // project the point to the map's spatial reference
    Point pointProj = (Point) GeometryEngine.project(point, wgs84, map.getSpatialReference());

    // show point
    Graphic pointGraphic = new Graphic(pointProj, SYM_POINT);
    graphicsLayer.addGraphic(pointGraphic);

    // zoom to an envelope around the point
    map.setExtent(new Envelope(pointProj, 1000000, 1000000));

    return;
  }

  private Point getPoint() {
    double latCoord = Double.parseDouble(latitude.getText());
    if (latCoord > 90 || latCoord < -90) {
      JOptionPane.showMessageDialog(contentPane,
          "Please enter a latitude between -90 and 90 decimal degrees.", "", JOptionPane.WARNING_MESSAGE);
    }
    double longCoord = Double.parseDouble(longitude.getText());
    if (longCoord > 180 || longCoord < -180) {
      JOptionPane.showMessageDialog(contentPane,
          "Please enter a longitude between -180 and 180 decimal degrees.", "", JOptionPane.WARNING_MESSAGE);
    }

    // Point constructor is (X, Y), i.e. (longitude, latitude) in our case
    return new Point(longCoord, latCoord);
  }

  // ------------------------------------------------------------------------
  // 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
          ProjectPointApp projectPointApp = new ProjectPointApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = projectPointApp.createWindow();
          appWindow.add(projectPointApp.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
    contentPane = createContentPane();

    // map
    map = createMap();

    // user panel
    JPanel panel = createUserPanel();

    contentPane.add(panel);
    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Project Point Application");
    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.setBounds(100, 100, 1000, 700);
    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 - focus on U.S extent by default
    // -----------------------------------------------------------------------------------------
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(BASE_LAYER_URL);
    jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));

    LayerList layers = jMap.getLayers();
    layers.add(baseLayer);

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

    return jMap;
  }

  /**
   * Creates a description for this application.
   * @return description
   */
  private JTextArea createDescription() {
    JTextArea description = new JTextArea(
        "This application shows how to project a point from decimal " +
            "degrees (latitude and longitude) to the map's spatial reference, " +
            "set by the base layer used.  Enter coordinates below and press " +
        "the button to show the projected point on the map.");
    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,10));
    return description;
  }

  /**
   * Creates the panel to display description and capture user input
   */
  private JPanel createUserPanel() {

    // description
    JTextArea description = createDescription();

    // label for coordinates input
    JLabel lblLatitude = new JLabel("Latitude: ");
    lblLatitude.setForeground(Color.BLACK);
    lblLatitude.setFont(new Font("Verdana", Font.BOLD, 12));
    lblLatitude.setMaximumSize(new Dimension(120, 20));
    JLabel lblLongitude = new JLabel("Longitude: ");
    lblLongitude.setForeground(Color.BLACK);
    lblLongitude.setFont(new Font("Verdana", Font.BOLD, 12));
    lblLongitude.setMaximumSize(new Dimension(120, 20));

    // text fields to input coordinates
    // default lat/long values are set to St-Paul/Minneapolis region
    latitude = new JTextField("44.9", 5);
    latitude.setMaximumSize(new Dimension(20, 20));
    longitude = new JTextField("-93.3", 5);
    longitude.setMaximumSize(new Dimension(20, 20));

    // button panel
    final JButton btnProject = new JButton("Project and show point");
    btnProject.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Point point = getPoint();
        projectPoint(point);
      }
    });

    // group the above UI items into a panel
    JPanel inputPanel = new JPanel();
    inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
    inputPanel.setSize(PANEL_WIDTH, 40);
    inputPanel.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
    inputPanel.setBackground(Color.WHITE);
    inputPanel.add(lblLatitude);
    inputPanel.add(latitude);
    inputPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    inputPanel.add(lblLongitude);
    inputPanel.add(longitude);

    final JPanel userPanel = new JPanel();
    userPanel.setSize(PANEL_WIDTH, 70);
    userPanel.setLayout(new BorderLayout());
    userPanel.add(inputPanel, BorderLayout.CENTER);
    userPanel.add(btnProject, BorderLayout.SOUTH);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout(0, 0));
    panel.setLocation(10, 10);
    panel.setSize(PANEL_WIDTH, 180);
    panel.setBackground(new Color(0, 0, 0, 0));
    panel.setBorder(new LineBorder(new Color(0, 0, 0, 60), 5, false));

    // group description and input in a panel
    panel.add(description, BorderLayout.CENTER);
    panel.add(userPanel, BorderLayout.SOUTH);

    return panel;
  }
}
Feedback on this topic?