Debian Codegraph @

src/main/org/h2/value/ValueGeometry.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.value;
7
8 import static org.h2.util.geometry.EWKBUtils.EWKB_SRID;
9
10 import org.h2.api.ErrorCode;
11 import org.h2.message.DbException;
12 import org.h2.util.Bits;
13 import org.h2.util.MathUtils;
14 import org.h2.util.StringUtils;
15 import org.h2.util.geometry.EWKBUtils;
16 import org.h2.util.geometry.EWKTUtils;
17 import org.h2.util.geometry.GeometryUtils;
18 import org.h2.util.geometry.GeometryUtils.EnvelopeTarget;
19 import org.h2.util.geometry.JTSUtils;
20 import org.h2.util.geometry.EWKTUtils.EWKTTarget;
21 import org.locationtech.jts.geom.Geometry;
22
23 /**
24 * Implementation of the GEOMETRY data type.
25 *
26 * @author Thomas Mueller
27 * @author Noel Grandin
28 * @author Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888
29 */
30 public final class ValueGeometry extends ValueBytesBase {
31
32 private static final double[] UNKNOWN_ENVELOPE = new double[0];
33
34 /**
35 * Geometry type and dimension system in OGC geometry code format (type +
36 * dimensionSystem * 1000).
37 */
38 private final int typeAndDimensionSystem;
39
40 /**
41 * Spatial reference system identifier.
42 */
43 private final int srid;
44
45 /**
46 * The envelope of the value. Calculated only on request.
47 */
48 private double[] envelope;
49
50 /**
51 * The value. Converted from WKB only on request as conversion from/to WKB
52 * cost a significant amount of CPU cycles.
53 */
54 private Object geometry;
55
56 /**
57 * Create a new geometry object.
58 *
59 * @param bytes the EWKB bytes
60 * @param envelope the envelope
61 */
62 private ValueGeometry(byte[] bytes, double[] envelope) {
63 super(bytes);
64 if (bytes.length < 9 || bytes[0] != 0) {
65 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, StringUtils.convertBytesToHex(bytes));
66 }
67 this.value = bytes;
68 this.envelope = envelope;
69 int t = Bits.readInt(bytes, 1);
70 srid = (t & EWKB_SRID) != 0 ? Bits.readInt(bytes, 5) : 0;
71 typeAndDimensionSystem = (t & 0xffff) % 1_000 + EWKBUtils.type2dimensionSystem(t) * 1_000;
72 }
73
74 /**
75 * Get or create a geometry value for the given geometry.
76 *
77 * @param o the geometry object (of type
78 * org.locationtech.jts.geom.Geometry)
79 * @return the value
80 */
81 public static ValueGeometry getFromGeometry(Object o) {
82 try {
83 Geometry g = (Geometry) o;
84 return (ValueGeometry) Value.cache(new ValueGeometry(JTSUtils.geometry2ewkb(g), UNKNOWN_ENVELOPE));
85 } catch (RuntimeException ex) {
86 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, String.valueOf(o));
87 }
88 }
89
90 /**
91 * Get or create a geometry value for the given geometry.
92 *
93 * @param s the WKT or EWKT representation of the geometry
94 * @return the value
95 */
96 public static ValueGeometry get(String s) {
97 try {
98 return (ValueGeometry) Value.cache(new ValueGeometry(EWKTUtils.ewkt2ewkb(s), UNKNOWN_ENVELOPE));
99 } catch (RuntimeException ex) {
100 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, s);
101 }
102 }
103
104 /**
105 * Get or create a geometry value for the given internal EWKB representation.
106 *
107 * @param bytes the WKB representation of the geometry. May not be modified.
108 * @return the value
109 */
110 public static ValueGeometry get(byte[] bytes) {
111 return (ValueGeometry) Value.cache(new ValueGeometry(bytes, UNKNOWN_ENVELOPE));
112 }
113
114 /**
115 * Get or create a geometry value for the given EWKB value.
116 *
117 * @param bytes the WKB representation of the geometry
118 * @return the value
119 */
120 public static ValueGeometry getFromEWKB(byte[] bytes) {
121 try {
122 return (ValueGeometry) Value.cache(new ValueGeometry(EWKBUtils.ewkb2ewkb(bytes), UNKNOWN_ENVELOPE));
123 } catch (RuntimeException ex) {
124 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, StringUtils.convertBytesToHex(bytes));
125 }
126 }
127
128 /**
129 * Creates a geometry value for the given envelope.
130 *
131 * @param envelope envelope. May not be modified.
132 * @return the value
133 */
134 public static Value fromEnvelope(double[] envelope) {
135 return envelope != null
136 ? Value.cache(new ValueGeometry(EWKBUtils.envelope2wkb(envelope), envelope))
137 : ValueNull.INSTANCE;
138 }
139
140 /**
141 * Get a copy of geometry object. Geometry object is mutable. The returned
142 * object is therefore copied before returning.
143 *
144 * @return a copy of the geometry object
145 */
146 public Geometry getGeometry() {
147 if (geometry == null) {
148 try {
149 geometry = JTSUtils.ewkb2geometry(value, getDimensionSystem());
150 } catch (RuntimeException ex) {
151 throw DbException.convert(ex);
152 }
153 }
154 return ((Geometry) geometry).copy();
155 }
156
157 /**
158 * Returns geometry type and dimension system in OGC geometry code format
159 * (type + dimensionSystem * 1000).
160 *
161 * @return geometry type and dimension system
162 */
163 public int getTypeAndDimensionSystem() {
164 return typeAndDimensionSystem;
165 }
166
167 /**
168 * Returns geometry type.
169 *
170 * @return geometry type and dimension system
171 */
172 public int getGeometryType() {
173 return typeAndDimensionSystem % 1_000;
174 }
175
176 /**
177 * Return a minimal dimension system that can be used for this geometry.
178 *
179 * @return dimension system
180 */
181 public int getDimensionSystem() {
182 return typeAndDimensionSystem / 1_000;
183 }
184
185 /**
186 * Return a spatial reference system identifier.
187 *
188 * @return spatial reference system identifier
189 */
190 public int getSRID() {
191 return srid;
192 }
193
194 /**
195 * Return an envelope of this geometry. Do not modify the returned value.
196 *
197 * @return envelope of this geometry
198 */
199 public double[] getEnvelopeNoCopy() {
200 if (envelope == UNKNOWN_ENVELOPE) {
201 EnvelopeTarget target = new EnvelopeTarget();
202 EWKBUtils.parseEWKB(value, target);
203 envelope = target.getEnvelope();
204 }
205 return envelope;
206 }
207
208 /**
209 * Test if this geometry envelope intersects with the other geometry
210 * envelope.
211 *
212 * @param r the other geometry
213 * @return true if the two overlap
214 */
215 public boolean intersectsBoundingBox(ValueGeometry r) {
216 return GeometryUtils.intersects(getEnvelopeNoCopy(), r.getEnvelopeNoCopy());
217 }
218
219 /**
220 * Get the union.
221 *
222 * @param r the other geometry
223 * @return the union of this geometry envelope and another geometry envelope
224 */
225 public Value getEnvelopeUnion(ValueGeometry r) {
226 return fromEnvelope(GeometryUtils.union(getEnvelopeNoCopy(), r.getEnvelopeNoCopy()));
227 }
228
229 @Override
230 public TypeInfo getType() {
231 return TypeInfo.TYPE_GEOMETRY;
232 }
233
234 @Override
235 public int getValueType() {
236 return GEOMETRY;
237 }
238
239 @Override
240 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
241 builder.append("GEOMETRY ");
242 if ((sqlFlags & ADD_PLAN_INFORMATION) != 0) {
243 EWKBUtils.parseEWKB(value, new EWKTTarget(builder.append('\''), getDimensionSystem()));
244 builder.append('\'');
245 } else {
246 super.getSQL(builder, DEFAULT_SQL_FLAGS);
247 }
248 return builder;
249 }
250
251 @Override
252 public String getString() {
253 return EWKTUtils.ewkb2ewkt(value, getDimensionSystem());
254 }
255
256 @Override
257 public int getMemory() {
258 return MathUtils.convertLongToInt(value.length * 20L + 24);
259 }
260
261 }