|
| 1 | +package org.geoscript.js.geom; |
| 2 | + |
| 3 | +import com.vividsolutions.jts.geom.Coordinate; |
| 4 | +import org.geotools.geometry.jts.CurvedGeometryFactory; |
| 5 | +import org.mozilla.javascript.*; |
| 6 | +import org.mozilla.javascript.annotations.JSConstructor; |
| 7 | +import org.mozilla.javascript.annotations.JSGetter; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class CircularString extends LineString implements Wrapper { |
| 13 | + |
| 14 | + /** serialVersionUID */ |
| 15 | + private static final long serialVersionUID = -5048539260091857410L; |
| 16 | + |
| 17 | + /** |
| 18 | + * Prototype constructor. |
| 19 | + * @return |
| 20 | + */ |
| 21 | + public CircularString() { |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Constructor for config object. |
| 26 | + * @param config |
| 27 | + */ |
| 28 | + public CircularString(NativeObject config) { |
| 29 | + NativeArray array = (NativeArray) config.get("coordinates", config); |
| 30 | + double tolerance = config.has("tolerance", config) ? (Double) config.get("tolerance", config) : Double.MAX_VALUE; |
| 31 | + Coordinate[] coords = arrayToCoords(array); |
| 32 | + setGeometry(createCircularString(coords, tolerance)); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructor for config object (without new keyword). |
| 37 | + * @param scope |
| 38 | + * @param config |
| 39 | + */ |
| 40 | + public CircularString(Scriptable scope, NativeObject config) { |
| 41 | + this(config); |
| 42 | + this.setParentScope(scope); |
| 43 | + this.setPrototype(Module.getClassPrototype(CircularString.class)); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructor from JTS geometry. |
| 48 | + * @param geometry |
| 49 | + */ |
| 50 | + public CircularString(Scriptable scope, org.geotools.geometry.jts.CircularString geometry) { |
| 51 | + this.setParentScope(scope); |
| 52 | + this.setPrototype(Module.getClassPrototype(CircularString.class)); |
| 53 | + setGeometry(geometry); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * JavaScript constructor. |
| 58 | + * @param cx |
| 59 | + * @param args |
| 60 | + * @param ctorObj |
| 61 | + * @param inNewExpr |
| 62 | + * @return |
| 63 | + */ |
| 64 | + @JSConstructor |
| 65 | + public static Object constructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) { |
| 66 | + if (args.length != 1) { |
| 67 | + throw ScriptRuntime.constructError("Error", "CircularString constructor takes a single argument"); |
| 68 | + } |
| 69 | + NativeObject config = prepConfig(cx, (Scriptable) args[0]); |
| 70 | + CircularString line = null; |
| 71 | + if (inNewExpr) { |
| 72 | + line = new CircularString(config); |
| 73 | + } else { |
| 74 | + line = new CircularString(config.getParentScope(), config); |
| 75 | + } |
| 76 | + return line; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Create a CircularString from an array of Coordinates and a tolerance used to linearize the curve. |
| 81 | + * @param coords The Array of Coordinates |
| 82 | + * @param tolerance The tolerance used to linearize the curve |
| 83 | + * @return A CircularString |
| 84 | + */ |
| 85 | + private com.vividsolutions.jts.geom.Geometry createCircularString(Coordinate[] coords, double tolerance) { |
| 86 | + CurvedGeometryFactory factory = new CurvedGeometryFactory(tolerance); |
| 87 | + double[] values = new double[coords.length * 2]; |
| 88 | + for(int i = 0; i < coords.length; i++) { |
| 89 | + int c = i * 2; |
| 90 | + values[c] = coords[i].x; |
| 91 | + values[c + 1] = coords[i].y; |
| 92 | + } |
| 93 | + return new org.geotools.geometry.jts.CircularString(values, factory, tolerance); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Get the curved WKT |
| 98 | + * @return The curved WKT |
| 99 | + */ |
| 100 | + @JSGetter |
| 101 | + public String getCurvedWkt() { |
| 102 | + return ((org.geotools.geometry.jts.CircularString)getGeometry()).toCurvedText(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get the original control Points (not the linearized Points) |
| 107 | + * @return The original control Points |
| 108 | + */ |
| 109 | + @JSGetter |
| 110 | + public NativeArray getControlPoints() { |
| 111 | + Context cx = getCurrentContext(); |
| 112 | + List<Point> points = new ArrayList<>(); |
| 113 | + org.geotools.geometry.jts.CircularString cs = (org.geotools.geometry.jts.CircularString)getGeometry(); |
| 114 | + double[] cp = cs.getControlPoints(); |
| 115 | + for(int i=0; i<cp.length; i=i+2) { |
| 116 | + Point pt = new Point(getParentScope(), factory.createPoint(new Coordinate(cp[i], cp[i+1]))); |
| 117 | + points.add(pt); |
| 118 | + } |
| 119 | + return (NativeArray) cx.newArray(getParentScope(), points.toArray()); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Get the linearized Geometry |
| 124 | + * @return The linearized Geometry |
| 125 | + */ |
| 126 | + @JSGetter |
| 127 | + public Geometry getLinear() { |
| 128 | + org.geotools.geometry.jts.CircularString cs = (org.geotools.geometry.jts.CircularString)getGeometry(); |
| 129 | + return (Geometry) GeometryWrapper.wrap(getParentScope(), cs.linearize()); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns underlying JTS geometry. |
| 134 | + */ |
| 135 | + public org.geotools.geometry.jts.CircularString unwrap() { |
| 136 | + return (org.geotools.geometry.jts.CircularString) getGeometry(); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments