Class PackedCoordinateSequenceFactory

  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.impl;
 13  
 14 import java.io.Serializable;
 15  
 16 import org.locationtech.jts.geom.Coordinate;
 17 import org.locationtech.jts.geom.CoordinateSequence;
 18 import org.locationtech.jts.geom.CoordinateSequenceFactory;
 19 import org.locationtech.jts.geom.Coordinates;
 20  
 21 /**
 22  * Builds packed array coordinate sequences. 
 23  * The array data type can be either
 24  * <code>double</code> or <code>float</code>, 
 25  * and defaults to <code>double</code>.
 26  */
 27 public class PackedCoordinateSequenceFactory implements
 28     CoordinateSequenceFactory, Serializable
 29 {
 30   private static final long serialVersionUID = -3558264771905224525L;
 31   
 32   /**
 33    * Type code for arrays of type <code>double</code>.
 34    */
 35   public static final int DOUBLE = 0;
 36   
 37   /**
 38    * Type code for arrays of type <code>float</code>.
 39    */
 40   public static final int FLOAT = 1;
 41  
 42   /**
 43    * A factory using array type {@link #DOUBLE}
 44    */
 45   public static final PackedCoordinateSequenceFactory DOUBLE_FACTORY =
 46       new PackedCoordinateSequenceFactory(DOUBLE);
 47   
 48   /**
 49    * A factory using array type {@link #FLOAT}
 50    */
 51   public static final PackedCoordinateSequenceFactory FLOAT_FACTORY =
 52       new PackedCoordinateSequenceFactory(FLOAT);
 53  
 54   private static final int DEFAULT_MEASURES = 0;
 55  
 56   private static final int DEFAULT_DIMENSION = 3;
 57  
 58   private int type = DOUBLE;
 59  
 60   /**
 61    * Creates a new PackedCoordinateSequenceFactory
 62    * of type DOUBLE.
 63    */
 64   public PackedCoordinateSequenceFactory(){
 65     this(DOUBLE);
 66   }
 67  
 68   /**
 69    * Creates a new PackedCoordinateSequenceFactory
 70    * of the given type.
 71    * Acceptable type values are
 72    * {@linkplain PackedCoordinateSequenceFactory#FLOAT}or
 73    * {@linkplain PackedCoordinateSequenceFactory#DOUBLE}
 74    */
 75   public PackedCoordinateSequenceFactory(int type){
 76     this.type = type;
 77   }
 78  
 79   /**
 80    * Gets the type of packed coordinate sequence this factory builds, either
 81    * {@linkplain PackedCoordinateSequenceFactory#FLOAT} or
 82    * {@linkplain PackedCoordinateSequenceFactory#DOUBLE}
 83    * 
 84    * @return the type of packed array built
 85    */
 86   public int getType() {
 87     return type;
 88   }
 89  
 90   /**
 91    * @see CoordinateSequenceFactory#create(Coordinate[])
 92    */
 93   public CoordinateSequence create(Coordinate[] coordinates) {
 94     int dimension = DEFAULT_DIMENSION;
 95     int measures = DEFAULT_MEASURES;
 96     if (coordinates != null && coordinates.length > 0 && coordinates[0] != null) {
 97       Coordinate first = coordinates[0];
 98       dimension = Coordinates.dimension(first);
 99       measures = Coordinates.measures(first);
100     }
101     if (type == DOUBLE) {
102       return new PackedCoordinateSequence.Double(coordinates, dimension, measures);
103     } else {
104       return new PackedCoordinateSequence.Float(coordinates,  dimension, measures);
105     }
106   }
107  
108   /**
109    * @see CoordinateSequenceFactory#create(CoordinateSequence)
110    */
111   public CoordinateSequence create(CoordinateSequence coordSeq) {
112     int dimension = coordSeq.getDimension();
113     int measures = coordSeq.getMeasures();
114     if (type == DOUBLE) {
115       return new PackedCoordinateSequence.Double(coordSeq.toCoordinateArray(), dimension, measures);
116     } else {
117       return new PackedCoordinateSequence.Float(coordSeq.toCoordinateArray(), dimension, measures);
118     }
119   }
120  
121   /**
122    * Creates a packed coordinate sequence of type {@link #DOUBLE}
123    * from the provided array
124    * using the given coordinate dimension and a measure count of 0. 
125    * 
126    * @param packedCoordinates the array containing coordinate values
127    * @param dimension the coordinate dimension
128    * @return a packed coordinate sequence of type {@link #DOUBLE}
129    */
130   public CoordinateSequence create(double[] packedCoordinates, int dimension) {
131     return create( packedCoordinates, dimension, DEFAULT_MEASURES );
132   }
133   
134   /**
135    * Creates a packed coordinate sequence of type {@link #DOUBLE}
136    * from the provided array
137    * using the given coordinate dimension and measure count. 
138    * 
139    * @param packedCoordinates the array containing coordinate values
140    * @param dimension the coordinate dimension
141    * @param measures the coordinate measure count
142    * @return a packed coordinate sequence of type {@link #DOUBLE}
143    */
144   public CoordinateSequence create(double[] packedCoordinates, int dimension, int measures) {
145     if (type == DOUBLE) {
146       return new PackedCoordinateSequence.Double(packedCoordinates, dimension, measures);
147     } else {
148       return new PackedCoordinateSequence.Float(packedCoordinates, dimension, measures);
149     }
150   }
151   /**
152    * Creates a packed coordinate sequence of type {@link #FLOAT}
153    * from the provided array. 
154    * 
155    * @param packedCoordinates the array containing coordinate values
156    * @param dimension the coordinate dimension
157    * @return a packed coordinate sequence of type {@link #FLOAT}
158    */
159   public CoordinateSequence create(float[] packedCoordinates, int dimension) {
160     return create( packedCoordinates, dimension, DEFAULT_MEASURES );
161   }
162   
163   /**
164    * Creates a packed coordinate sequence of type {@link #FLOAT}
165    * from the provided array. 
166    * 
167    * @param packedCoordinates the array containing coordinate values
168    * @param dimension the coordinate dimension
169    * @param measures the coordinate measure count
170    * @return a packed coordinate sequence of type {@link #FLOAT}
171    */
172   public CoordinateSequence create(float[] packedCoordinates, int dimension, int measures) {
173     if (type == DOUBLE) {
174       return new PackedCoordinateSequence.Double(packedCoordinates, dimension, measures);
175     } else {
176       return new PackedCoordinateSequence.Float(packedCoordinates, dimension, measures);
177     }
178   }
179  
180   /**
181    * @see org.locationtech.jts.geom.CoordinateSequenceFactory#create(int, int)
182    */
183   public CoordinateSequence create(int size, int dimension) {
184     if (type == DOUBLE) {
185       return new PackedCoordinateSequence.Double(size, dimension , DEFAULT_MEASURES);
186     } else {
187       return new PackedCoordinateSequence.Float(size, dimension, DEFAULT_MEASURES );
188     }
189   }
190   
191   /**
192    * @see org.locationtech.jts.geom.CoordinateSequenceFactory#create(int, int, int)
193    */
194   public CoordinateSequence create(int size, int dimension, int measures) {
195     if (type == DOUBLE) {
196       return new PackedCoordinateSequence.Double(size, dimension, measures);
197     } else {
198       return new PackedCoordinateSequence.Float(size, dimension, measures);
199     }
200   }
201 }
202