Skip to content

Commit e45d343

Browse files
committed
Merge pull request #44 from jericks/curved_geom
Add curved geometry support: CircularStrings and CompoundCurves
2 parents 251de09 + bf26811 commit e45d343

File tree

12 files changed

+583
-2
lines changed

12 files changed

+583
-2
lines changed

doc/api/geom.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Constructors
2424
geom/multilinestring
2525
geom/multipolygon
2626
geom/bounds
27+
geom/circularstring
28+
geom/compoundcurve
2729

2830

2931
Module Data

doc/api/geom/circularstring.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
:class:`geom.CircularString`
2+
========================
3+
4+
.. class:: geom.CircularString(coords)
5+
6+
:arg Array coords: Coordinates array.
7+
8+
Create a new circularstring.
9+
10+
11+
Example Use
12+
-----------
13+
14+
Sample code to new circularstring:
15+
16+
.. code-block:: javascript
17+
18+
>> var CircularString = require("geoscript/geom").CircularString;
19+
>> var cs = new CircularString([[6.12, 10.0], [7.07, 7.07], [10.0, 0.0]]);
20+
>> cs.controlPoints.length
21+
3
22+
>> cs.linear.getGeometryType()
23+
LineString
24+
>> cs.curvedWkt
25+
CIRCULARSTRING(6.12 10.0, 7.07 7.07, 10.0 0.0)
26+
27+
28+
Properties
29+
----------
30+
31+
In addition to the properties common to :class:`geom.LineString` subclasses,
32+
circularstring geometries have the properties documented below.
33+
34+
35+
.. attribute:: CircularString.curvedWkt
36+
37+
:class:`String`
38+
The curved WKT as a string.
39+
40+
.. attribute:: CircularString.controlPoints
41+
42+
``Array``
43+
An array of the original control Points.
44+
45+
46+
.. attribute:: CircularString.linear
47+
48+
:class:`geom.LineString`
49+
A linearized LineString.
50+
51+
52+
53+
Methods
54+
-------
55+
56+
CircularString geometries have the methods common to :class:`geom.LineString`
57+
subclasses.

doc/api/geom/compoundcurve.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
:class:`geom.CompoundCurve`
2+
===========================
3+
4+
.. class:: geom.CompoundCurve(lineStrings)
5+
6+
:arg Array lineStrings: An array of LineStrings or CircularStrings.
7+
8+
Create a new CompoundCurve.
9+
10+
11+
Example Use
12+
-----------
13+
14+
Sample code to new compoundcurve:
15+
16+
.. code-block:: javascript
17+
18+
>> var GEOM = require("geoscript/geom");
19+
>> var cs = new GEOM.CircularString([[10.0, 10.0], [0.0, 20.0], [-10.0, 10.0]]);
20+
>> var line = new GEOM.LineString([[-10.0, 10.0], [-10.0, 0.0], [10.0, 0.0], [5.0, 5.0]])
21+
>> var cc = new GEOM.CompoundCurve([cs, line]);
22+
>> cc.components.length
23+
2
24+
>> cc.linear.getGeometryType()
25+
LineString
26+
>> cc.curvedWkt
27+
COMPOUNDCURVE(CIRCULARSTRING(10.0 10.0, 0.0 20.0, -10.0 10.0), (-10.0 10.0, -10.0 0.0, 10.0 0.0, 5.0 5.0))
28+
29+
30+
Properties
31+
----------
32+
33+
In addition to the properties common to all :class:`geom.LineString` subclasses,
34+
compoundcurve geometries have the properties documented below.
35+
36+
37+
.. attribute:: CompoundCurve.components
38+
39+
:class:`geom.LineString`
40+
The original LineString or CircularStrings.
41+
42+
.. attribute:: CircularString.curvedWkt
43+
44+
:class:`String`
45+
The curved WKT as a string.
46+
47+
.. attribute:: CircularString.linear
48+
49+
:class:`geom.LineString`
50+
A linearized LineString.
51+
52+
Methods
53+
-------
54+
55+
CompoundCurve geometries have the methods common to all :class:`geom.LineString`
56+
subclasses.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)