-
Notifications
You must be signed in to change notification settings - Fork 63
/
ST_AddZ.java
101 lines (88 loc) · 3.05 KB
/
ST_AddZ.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* H2GIS is a library that brings spatial support to the H2 Database Engine
* <a href="http://www.h2database.com">http://www.h2database.com</a>. H2GIS is developed by CNRS
* <a href="http://www.cnrs.fr/">http://www.cnrs.fr/</a>.
*
* This code is part of the H2GIS project. H2GIS is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation;
* version 3.0 of the License.
*
* H2GIS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details <http://www.gnu.org/licenses/>.
*
*
* For more information, please consult: <a href="http://www.h2gis.org/">http://www.h2gis.org/</a>
* or contact directly: info_at_h2gis.org
*/
package org.h2gis.functions.spatial.edit;
import org.h2gis.api.DeterministicScalarFunction;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateSequence;
import org.locationtech.jts.geom.CoordinateSequenceFilter;
import org.locationtech.jts.geom.Geometry;
import java.sql.SQLException;
/**
* This function add a z value to the z component of (each vertex of) the
* geometric parameter to the corresponding value given by a field.
*
* @author Erwan Bocher
*/
public class ST_AddZ extends DeterministicScalarFunction {
public ST_AddZ() {
addProperty(PROP_REMARKS, "This function do a sum with the z value of (each vertex of) the\n"
+ " geometric parameter to the corresponding value given by a field.");
}
@Override
public String getJavaStaticMethod() {
return "addZ";
}
/**
* Add a z with to the existing value (do the sum). NaN values are not
* updated.
*
* @param geometry
* @param z
* @return
* @throws java.sql.SQLException
*/
public static Geometry addZ(Geometry geometry, double z) throws SQLException {
if(geometry == null){
return null;
}
geometry.apply(new AddZCoordinateSequenceFilter(z));
return geometry;
}
/**
* Add a z value to each vertex of the Geometry.
*
*/
public static class AddZCoordinateSequenceFilter implements CoordinateSequenceFilter {
private boolean done = false;
private final double z;
public AddZCoordinateSequenceFilter(double z) {
this.z = z;
}
@Override
public boolean isGeometryChanged() {
return true;
}
@Override
public boolean isDone() {
return done;
}
@Override
public void filter(CoordinateSequence seq, int i) {
Coordinate coord = seq.getCoordinate(i);
double currentZ = coord.z;
if (!Double.isNaN(currentZ)) {
seq.setOrdinate(i, 2, currentZ + z);
}
if (i == seq.size()) {
done = true;
}
}
}
}