Class CoordinateXYZM

  1 /*
  2  * Copyright (c) 2016 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 XYZM ordinates.
 16  * <p>
 17  * This data object is suitable for use with coordinate sequences with <tt>dimension</tt> = 4 and <tt>measures</tt> = 1.
 18  *
 19  * @since 1.16
 20  */
 21 public class CoordinateXYZM extends Coordinate {
 22   private static final long serialVersionUID = -8763329985881823442L;
 23  
 24   /** Default constructor */
 25   public CoordinateXYZM() {
 26     super();
 27     this.m = 0.0;
 28   }
 29  
 30   /**
 31    * Constructs a CoordinateXYZM instance with the given ordinates and measure.
 32    * 
 33    * @param x the X ordinate
 34    * @param y the Y ordinate
 35    * @param z the Z ordinate
 36    * @param m the M measure value
 37    */
 38   public CoordinateXYZM(double x, double y, double z, double m) {
 39     super(x, y, z);
 40     this.m = m;
 41   }
 42  
 43   /**
 44    * Constructs a CoordinateXYZM instance with the ordinates of the given Coordinate.
 45    * 
 46    * @param coord the coordinate providing the ordinates
 47    */
 48   public CoordinateXYZM(Coordinate coord) {
 49     super(coord);
 50     m = getM();
 51   }
 52   
 53   /**
 54    * Constructs a CoordinateXYZM instance with the ordinates of the given CoordinateXYZM.
 55    * 
 56    * @param coord the coordinate providing the ordinates
 57    */
 58   public CoordinateXYZM(CoordinateXYZM coord) {
 59     super(coord);
 60     m = coord.m;
 61   }
 62  
 63   /**
 64    * Creates a copy of this CoordinateXYZM.
 65    * 
 66    * @return a copy of this CoordinateXYZM
 67    */
 68   public CoordinateXYZM copy() {
 69     return new CoordinateXYZM(this);
 70   }
 71  
 72   /** The m-measure. */
 73   private double m;
 74  
 75   /** The m-measure, if available. */
 76   public double getM() {
 77     return m;
 78   }
 79  
 80   public void setM(double m) {
 81     this.m = m;
 82   }
 83  
 84   public double getOrdinate(int ordinateIndex)
 85   {
 86     switch (ordinateIndex) {
 87     case X: return x;
 88     case Y: return y;
 89     case Z: return getZ(); // sure to delegate to subclass rather than offer direct field access
 90     case M: return getM(); // sure to delegate to subclass rather than offer direct field access
 91     }
 92     throw new IllegalArgumentException("Invalid ordinate index: " + ordinateIndex);
 93   }
 94   
 95   @Override
 96   public void setCoordinate(Coordinate other)
 97   {
 98     x = other.x;
 99     y = other.y;
100     z = other.getZ();
101     m = other.getM();
102   }
103   
104   @Override
105   public void setOrdinate(int ordinateIndex, double value) {
106       switch (ordinateIndex) {
107       case X:
108         x = value;
109         break;
110       case Y:
111         y = value;
112         break;
113       case Z:
114         z = value;
115         break;
116       case M:
117         m = value;
118         break;
119       default:
120         throw new IllegalArgumentException("Invalid ordinate index: " + ordinateIndex);
121     }
122   }
123   
124   public String toString() {
125     return "(" + x + ", " + y + ", " + getZ() + " m="+getM()+")";
126   }
127 }
128