View Javadoc
1   /*
2    * Copyright (c) 2012-2014, Dienst Landelijk Gebied - Ministerie van Economische Zaken
3    * 
4    * Gepubliceerd onder de BSD 2-clause licentie, 
5    * zie https://github.com/MinELenI/CBSviewer/blob/master/LICENSE.md voor de volledige licentie.
6    */
7   package nl.mineleni.openls.databinding.gml;
8   
9   import nl.mineleni.openls.XmlNamespaceConstants;
10  
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  /**
15   * GML Pos. Beschrijft het Pos element uit GML 3.2.1.
16   * http://schemas.opengis.net/gml/3.2.1/geometryBasic0d1d.xsd
17   * 
18   * <pre>
19   * 
20   * &lt;complexType name="DirectPositionType"&gt;
21   *   &lt;annotation&gt;
22   *     &lt;documentation&gt;Direct position instances hold the coordinates
23   *     for a position within some coordinate reference system (CRS).
24   *     Since direct positions, as data types, will often be included
25   *     in larger objects (such as geometry elements) that have
26   *     references to CRS, the srsName attribute will in general be
27   *     missing, if this particular direct position is included in a
28   *     larger element with such a reference to a CRS. In this case,
29   *     the CRS is implicitly assumed to take on the value of the
30   *     containing object's CRS. if no srsName attribute is given, the
31   *     CRS shall be specified as part of the larger context this
32   *     geometry element is part of, typically a geometric object like
33   *     a point, curve, etc.&lt;/documentation&gt;
34   *   &lt;/annotation&gt;
35   *   &lt;simpleContent&gt;
36   *     &lt;extension base="gml:doubleList"&gt;
37   *       &lt;attributeGroup ref="gml:SRSReferenceGroup" /&gt;
38   *     &lt;/extension&gt;
39   *   &lt;/simpleContent&gt;
40   * &lt;/complexType&gt;
41   * &lt;element name="pos" type="gml:DirectPositionType" /&gt;
42   * 
43   * </pre>
44   * 
45   * @author Mark
46   */
47  public class Pos implements XmlNamespaceConstants {
48  	/**
49  	 * serialisation id.
50  	 */
51  	private static final long serialVersionUID = -1063398145364381070L;
52  
53  	/** logger. */
54  	private static final Logger LOGGER = LoggerFactory.getLogger(Pos.class);
55  
56  	/** The x. */
57  	private Double x;
58  
59  	/** The y. */
60  	private Double y;
61  
62  	/** The dimension. */
63  	private int dimension;
64  
65  	/** The has x. */
66  	private boolean hasX;
67  
68  	/** The has y. */
69  	private boolean hasY;
70  
71  	/** The has xy. */
72  	private boolean hasXY;
73  
74  	/** The has dimension. */
75  	private boolean hasDimension;
76  
77  	/**
78  	 * Instantiates a new pos.
79  	 */
80  	public Pos() {
81  		this.hasX = false;
82  		this.hasY = false;
83  		this.hasXY = false;
84  		this.hasDimension = false;
85  	}
86  
87  	/**
88  	 * Sets the xy.
89  	 * 
90  	 * @param xy
91  	 *            the new xy
92  	 */
93  	public void setXY(final String xy) {
94  		try {
95  			final String[] xySplit = xy.split(" ");
96  			if (xySplit.length == 2) {
97  				this.setX(Double.parseDouble(xySplit[0]));
98  				this.setY(Double.parseDouble(xySplit[1]));
99  			}
100 			this.hasXY = true;
101 		} catch (final NumberFormatException e) {
102 			LOGGER.error("Verwerken van puntlocatie mislukt, waarde: " + xy
103 					+ ": ", e);
104 		}
105 	}
106 
107 	/**
108 	 * Sets the x.
109 	 * 
110 	 * @param x
111 	 *            the new x
112 	 */
113 	public void setX(final Double x) {
114 		this.hasX = true;
115 		if (this.hasY) {
116 			this.hasXY = true;
117 		}
118 		this.x = x;
119 	}
120 
121 	/**
122 	 * Gets the x.
123 	 * 
124 	 * @return the x
125 	 */
126 	public Double getX() {
127 		return this.x;
128 	}
129 
130 	/**
131 	 * Sets the y.
132 	 * 
133 	 * @param y
134 	 *            the new y
135 	 */
136 	public void setY(final Double y) {
137 		this.hasY = true;
138 		if (this.hasX) {
139 			this.hasXY = true;
140 		}
141 		this.y = y;
142 	}
143 
144 	/**
145 	 * Gets the y.
146 	 * 
147 	 * @return the y
148 	 */
149 	public Double getY() {
150 		return this.y;
151 	}
152 
153 	/**
154 	 * Gets the xy.
155 	 * 
156 	 * @return the xy
157 	 */
158 	public String getXY() {
159 		return this.x.toString() + " " + this.y.toString();
160 	}
161 
162 	/**
163 	 * Checks for xy.
164 	 * 
165 	 * @return true, if successful
166 	 */
167 	public boolean hasXY() {
168 		return this.hasXY;
169 	}
170 
171 	/**
172 	 * Sets the dimension.
173 	 * 
174 	 * @param dimension
175 	 *            the new dimension
176 	 */
177 	public void setDimension(final int dimension) {
178 		this.hasDimension = true;
179 		this.dimension = dimension;
180 	}
181 
182 	/**
183 	 * Gets the dimension.
184 	 * 
185 	 * @return the dimension
186 	 */
187 	public int getDimension() {
188 		return this.dimension;
189 	}
190 
191 	/**
192 	 * Checks for dimension.
193 	 * 
194 	 * @return true, if successful
195 	 */
196 	public boolean hasDimension() {
197 		return this.hasDimension;
198 	}
199 
200 	/*
201 	 * (non-Javadoc)
202 	 * 
203 	 * @see nl.mineleni.openls.XmlNamespaceConstants#toXML()
204 	 */
205 	@Override
206 	public String toXML() {
207 		String xml = "<" + XmlNamespaceConstants.OGC_GML_NAMESPACE_PREFIX
208 				+ ":pos";
209 		if (this.hasDimension()) {
210 			xml += " dimension=\"" + this.getDimension() + "\"";
211 		}
212 		xml += ">";
213 		if (this.hasXY()) {
214 			xml += this.getXY();
215 		}
216 		xml += "</" + XmlNamespaceConstants.OGC_GML_NAMESPACE_PREFIX + ":pos>";
217 		return xml;
218 	}
219 }