Class Coordinates

 1 /*
 2  * Copyright (c) 2018 Contributors to the Eclipse Foundation
 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  * Useful utility functions for handling Coordinate objects.
16  */
17 public class Coordinates {
18   /**
19    * Factory method providing access to common Coordinate implementations.
20    * 
21    * @param dimension
22    * @return created coordinate
23    */
24   public static Coordinate create(int dimension)
25   {
26     return create(dimension, 0);
27   }
28  
29   /**
30    * Factory method providing access to common Coordinate implementations.
31    * 
32    * @param dimension
33    * @param measures
34    * @return created coordinate
35    */
36   public static Coordinate create(int dimension, int measures)
37   {
38     if (dimension == 2) {
39       return new CoordinateXY();
40     } else if (dimension == 3 && measures == 0) {
41       return new Coordinate();
42     } else if (dimension == 3 && measures == 1) {
43       return new CoordinateXYM();
44     } else if (dimension == 4 && measures == 1) {
45       return new CoordinateXYZM();
46     }
47     return new Coordinate();
48   }
49   
50   /**
51    * Determine dimension based on subclass of {@link Coordinate}.
52    * 
53    * @param coordinate supplied coordinate
54    * @return number of ordinates recorded
55    */
56   public static int dimension(Coordinate coordinate)
57   {
58     if (coordinate instanceof CoordinateXY) {
59       return 2;
60     } else if (coordinate instanceof CoordinateXYM) {
61       return 3;
62     } else if (coordinate instanceof CoordinateXYZM) {
63       return 4;      
64     } else if (coordinate instanceof Coordinate) {
65       return 3;
66     } 
67     return 3;
68   }
69  
70   /**
71    * Determine number of measures based on subclass of {@link Coordinate}.
72    * 
73    * @param coordinate supplied coordinate
74    * @return number of measures recorded
75    */
76   public static int measures(Coordinate coordinate)
77   {
78     if (coordinate instanceof CoordinateXY) {
79       return 0;
80     } else if (coordinate instanceof CoordinateXYM) {
81       return 1;
82     } else if (coordinate instanceof CoordinateXYZM) {
83       return 1;
84     } else if (coordinate instanceof Coordinate) {
85       return 0;
86     } 
87     return 0;
88   }
89     
90 }
91