Class CoordinateXY

  1 /*
  2  * Copyright (c) 2018 Vivid Solutions
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12 package org.locationtech.jts.geom;
 13  
 14 /**
 15  * Coordinate subclass supporting XY ordinates.
 16  * <p>
 17  * This data object is suitable for use with coordinate sequences with <tt>dimension</tt> = 2.
 18  * <p>
 19  * The {@link Coordinate#z} field is visible, but intended to be ignored.
 20  *
 21  * @since 1.16
 22  */
 23 public class CoordinateXY extends Coordinate {
 24   private static final long serialVersionUID = 3532307803472313082L;
 25  
 26   /** Standard ordinate index value for X */
 27   public static final int X = 0;
 28  
 29   /** Standard ordinate index value for Y */
 30   public static final int Y = 1;
 31  
 32   /** CoordinateXY does not support Z values. */
 33   public static final int Z = -1;
 34  
 35   /** CoordinateXY does not support M measures. */
 36   public static final int M = -1;
 37  
 38   /** Default constructor */
 39   public CoordinateXY() {
 40     super();
 41   }
 42  
 43   /**
 44    * Constructs a CoordinateXY instance with the given ordinates.
 45    * 
 46    * @param x the X ordinate
 47    * @param y the Y ordinate
 48    */
 49   public CoordinateXY(double x, double y) {
 50     super(x, y, Coordinate.NULL_ORDINATE);
 51   }
 52  
 53   /**
 54    * Constructs a CoordinateXY instance with the x and y ordinates of the given Coordinate.
 55    * 
 56    * @param coord the Coordinate providing the ordinates
 57    */
 58   public CoordinateXY(Coordinate coord) {
 59     super(coord.x,coord.y);
 60   }
 61  
 62   /**
 63    * Constructs a CoordinateXY instance with the x and y ordinates of the given CoordinateXY.
 64    * 
 65    * @param coord the CoordinateXY providing the ordinates
 66    */
 67   public CoordinateXY(CoordinateXY coord) {
 68     super(coord.x,coord.y);  
 69   }
 70  
 71   /**
 72    * Creates a copy of this CoordinateXY.
 73    * 
 74    * @return a copy of this CoordinateXY
 75    */
 76   public CoordinateXY copy() {
 77     return new CoordinateXY(this);
 78   }
 79     
 80   /** The z-ordinate is not supported */
 81   @Override
 82   public double getZ() {
 83       return NULL_ORDINATE;
 84   }
 85  
 86   /** The z-ordinate is not supported */
 87   @Override
 88   public void setZ(double z) {
 89       throw new IllegalArgumentException("CoordinateXY dimension 2 does not support z-ordinate");
 90   }  
 91   
 92   @Override
 93   public void setCoordinate(Coordinate other)
 94   {
 95     x = other.x;
 96     y = other.y;
 97     z = other.getZ();
 98   }
 99   
100   @Override
101   public double getOrdinate(int ordinateIndex) {
102       switch (ordinateIndex) {
103       case X: return x;
104       case Y: return y;
105       }
106       throw new IllegalArgumentException("Invalid ordinate index: " + ordinateIndex);
107   }
108   
109   @Override
110   public void setOrdinate(int ordinateIndex, double value) {
111       switch (ordinateIndex) {
112       case X:
113         x = value;
114         break;
115       case Y:
116         y = value;
117         break;
118       default:
119         throw new IllegalArgumentException("Invalid ordinate index: " + ordinateIndex);
120     }
121   }
122   
123   public String toString() {
124     return "(" + x + ", " + y + ")";
125   }
126 }
127