Class CoordinateXYM

  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 XYM ordinates.
 16  * <p>
 17  * This data object is suitable for use with coordinate sequences with <tt>dimension</tt> = 3 and <tt>measures</tt> = 1.
 18  * <p>
 19  * The {@link Coordinate#z} field is visible, but intended to be ignored.
 20  * 
 21  * @since 1.16
 22  */
 23 public class CoordinateXYM extends Coordinate {
 24   private static final long serialVersionUID = 2842127537691165613L;
 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   /** CoordinateXYM does not support Z values. */
 33   public static final int Z = -1;
 34  
 35   /**
 36    * Standard ordinate index value for M in XYM sequences.
 37    *
 38    * <p>This constant assumes XYM coordinate sequence definition.  Check this assumption using
 39    * {@link #getDimension()} and {@link #getMeasures()} before use.
 40    */
 41   public static final int M = 2;
 42  
 43   /** Default constructor */
 44   public CoordinateXYM() {
 45     super();
 46     this.m = 0.0;
 47   }
 48  
 49   /**
 50    * Constructs a CoordinateXYM instance with the given ordinates and measure.
 51    * 
 52    * @param x the X ordinate
 53    * @param y the Y ordinate
 54    * @param m the M measure value
 55    */
 56   public CoordinateXYM(double x, double y, double m) {
 57     super(x, y, Coordinate.NULL_ORDINATE);
 58     this.m = m;
 59   }
 60  
 61   /**
 62    * Constructs a CoordinateXYM instance with the x and y ordinates of the given Coordinate.
 63    * 
 64    * @param coord the coordinate providing the ordinates
 65    */
 66   public CoordinateXYM(Coordinate coord) {
 67     super(coord.x,coord.y);
 68     m = getM();
 69   }
 70  
 71   /**
 72    * Constructs a CoordinateXY instance with the x and y ordinates of the given CoordinateXYM.
 73    * 
 74    * @param coord the coordinate providing the ordinates
 75    */
 76   public CoordinateXYM(CoordinateXYM coord) {
 77     super(coord.x,coord.y);
 78     m = coord.m;
 79   }
 80   
 81   /**
 82    * Creates a copy of this CoordinateXYM.
 83    * 
 84    * @return a copy of this CoordinateXYM
 85    */
 86   public CoordinateXYM copy() {
 87     return new CoordinateXYM(this);
 88   }
 89     
 90   /** The m-measure. */
 91   protected double m;
 92  
 93   /** The m-measure, if available. */
 94   public double getM() {
 95     return m;
 96   }
 97  
 98   public void setM(double m) {
 99     this.m = m;
100   }
101   
102   /** The z-ordinate is not supported */
103   @Override
104   public double getZ() {
105       return NULL_ORDINATE;
106   }
107  
108   /** The z-ordinate is not supported */
109   @Override
110   public void setZ(double z) {
111       throw new IllegalArgumentException("CoordinateXY dimension 2 does not support z-ordinate");
112   }
113   
114   @Override
115   public void setCoordinate(Coordinate other)
116   {
117     x = other.x;
118     y = other.y;
119     z = other.getZ();
120     m = other.getM();
121   }
122   
123   @Override
124   public double getOrdinate(int ordinateIndex) {
125       switch (ordinateIndex) {
126       case X: return x;
127       case Y: return y;
128       case M: return m;
129       }
130       throw new IllegalArgumentException("Invalid ordinate index: " + ordinateIndex);
131   }
132   
133   @Override
134   public void setOrdinate(int ordinateIndex, double value) {
135       switch (ordinateIndex) {
136       case X:
137         x = value;
138         break;
139       case Y:
140         y = value;
141         break;
142       case M:
143         m = value;
144         break;
145       default:
146         throw new IllegalArgumentException("Invalid ordinate index: " + ordinateIndex);
147     }
148   }
149   
150   public String toString() {
151     return "(" + x + ", " + y + " m=" + getM() + ")";
152   }
153 }
154