forked from geodmms/xdgnjobs

Dennis Kao
2013-06-05 fc9880cd77a09dcfdea2791dee418f54f5721cf3
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.ximple.eofms.util.postjts;
 
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence;
import org.geotools.geometry.jts.JTSFactoryFinder;
 
public class JTSShape implements Shape {
    static GeometryFactory fac = JTSFactoryFinder.getGeometryFactory(null);
 
    Geometry geom;
 
    final static LinearRing[] NOSHELLS = {};
 
    public JTSShape(Geometry _geom) {
        this.geom = _geom;
    }
 
    public JTSShape(JtsGeometry _geom) {
        this(_geom.getGeometry());
    }
 
    public boolean contains(Point2D p) {
        return contains(p.getX(), p.getY());
    }
 
    public boolean contains(double x, double y) {
        Coordinate c = new Coordinate(x, y);
        Point p = fac.createPoint(c);
        return geom.contains(p);
    }
 
    public boolean contains(Rectangle2D r) {
        return contains(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight());
    }
 
    public boolean contains(double x, double y, double w, double h) {
        Polygon p = createRect(x, y, w, h);
        return geom.contains(p);
    }
 
    protected Polygon createRect(double x, double y, double w, double h) {
        double[] arr = {x, y, x + w, y, x + w, y + h, x, y + h, x, y};
        PackedCoordinateSequence shell = new PackedCoordinateSequence.Double(arr, 2);
        Polygon p = fac.createPolygon(fac.createLinearRing(shell), NOSHELLS);
        return p;
    }
 
    public Rectangle2D getBounds2D() {
        Envelope env = geom.getEnvelopeInternal();
        return new Rectangle2D.Double(env.getMinX(), env.getMaxX(), env.getWidth(), env.getHeight());
    }
 
    public Rectangle getBounds() {
        // We deal simple code for efficiency here, the getBounds() rounding
        // rules are ugly...
        return getBounds2D().getBounds();
    }
 
    public PathIterator getPathIterator(AffineTransform at) {
        return getPathIterator(geom, at);
    }
 
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
        // we don't have much work here, as we only have linear segments, no
        // "flattening" necessary.
        return getPathIterator(at);
    }
 
    public boolean intersects(Rectangle2D r) {
        return intersects(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight());
    }
 
    public boolean intersects(double x, double y, double w, double h) {
        Polygon p = createRect(x, y, w, h);
        return geom.intersects(p);
    }
 
    public static GeometryPathIterator getPathIterator(Geometry geometry, AffineTransform _at) {
        if (geometry instanceof Point) {
            return new PointPathIterator((Point) geometry, _at);
        } else if (geometry instanceof LineString) {
            return new LineStringPathIterator((LineString) geometry, _at);
        } else if (geometry instanceof Polygon) {
            return new PolygonPathIterator((Polygon) geometry, _at);
        } else {
            return new GeometryCollectionPathIterator((GeometryCollection) geometry, _at);
        }
    }
 
    public static abstract class GeometryPathIterator implements PathIterator {
 
        protected final AffineTransform at;
        protected int index = 0;
 
        GeometryPathIterator(AffineTransform _at) {
            this.at = _at;
        }
 
        public final int getWindingRule() {
            return PathIterator.WIND_EVEN_ODD;
        }
 
        public void next() {
            index++;
        }
    }
 
    public static class PointPathIterator extends GeometryPathIterator {
        final Point p;
 
        public PointPathIterator(Point _p, AffineTransform _at) {
            super(_at);
            p = _p;
        }
 
        public int currentSegment(float[] coords) {
            switch (index) {
            case 0:
                coords[0] = (float) p.getX();
                coords[1] = (float) p.getY();
                at.transform(coords, 0, coords, 0, 1);
                return PathIterator.SEG_MOVETO;
            case 1:
                return PathIterator.SEG_CLOSE;
            default:
                throw new IllegalStateException();
            }
        }
 
        public int currentSegment(double[] coords) {
            switch (index) {
            case 0:
                coords[0] = p.getX();
                coords[1] = p.getY();
                at.transform(coords, 0, coords, 0, 1);
                return PathIterator.SEG_MOVETO;
            case 1:
                return PathIterator.SEG_CLOSE;
            default:
                throw new IllegalStateException();
            }
        }
 
        public boolean isDone() {
            return index > 1;
        }
    }
 
    public static class LineStringPathIterator extends GeometryPathIterator {
        CoordinateSequence cs;
 
        final boolean isRing;
 
        public LineStringPathIterator(LineString ls, AffineTransform _at) {
            super(_at);
            cs = ls.getCoordinateSequence();
            isRing = ls instanceof LinearRing;
        }
 
        /**
         * only to be called from PolygonPathIterator subclass
         */
        protected void reInit(CoordinateSequence _cs) {
            cs = _cs;
            index = 0;
        }
 
        public int currentSegment(float[] coords) {
            if (index == 0) {
                coords[0] = (float) cs.getOrdinate(index, 0);
                coords[1] = (float) cs.getOrdinate(index, 1);
                at.transform(coords, 0, coords, 0, 1);
                return PathIterator.SEG_MOVETO;
            } else if (index < cs.size()) {
                coords[0] = (float) cs.getOrdinate(index, 0);
                coords[1] = (float) cs.getOrdinate(index, 1);
                at.transform(coords, 0, coords, 0, 1);
                return PathIterator.SEG_LINETO;
            } else if (isRing && index == cs.size()) {
                return PathIterator.SEG_CLOSE;
            } else {
                throw new IllegalStateException();
            }
        }
 
        public int currentSegment(double[] coords) {
            if (index == 0) {
                coords[0] = cs.getOrdinate(index, 0);
                coords[1] = cs.getOrdinate(index, 1);
                at.transform(coords, 0, coords, 0, 1);
                return PathIterator.SEG_MOVETO;
            } else if (index < cs.size()) {
                coords[0] = cs.getOrdinate(index, 0);
                coords[1] = cs.getOrdinate(index, 1);
                at.transform(coords, 0, coords, 0, 1);
                return PathIterator.SEG_LINETO;
            } else if (isRing && index == cs.size()) {
                return PathIterator.SEG_CLOSE;
            } else {
                throw new IllegalStateException();
            }
        }
 
        public boolean isDone() {
            return isRing ? index > cs.size() : index >= cs.size();
        }
    }
 
    public static class PolygonPathIterator extends LineStringPathIterator {
        final Polygon pg;
        int outerindex = -1;
 
        public PolygonPathIterator(Polygon _pg, AffineTransform _at) {
            super(_pg.getExteriorRing(), _at);
            pg = _pg;
            index = -1;
        }
 
        public boolean isDone() {
            return outerindex >= pg.getNumInteriorRing();
        }
 
        public void next() {
            super.next();
            if (super.isDone()) {
                outerindex++;
                if (outerindex < pg.getNumInteriorRing()) {
                    super.reInit(pg.getInteriorRingN(outerindex).getCoordinateSequence());
                }
            }
        }
    }
 
    public static class GeometryCollectionPathIterator extends GeometryPathIterator {
        final GeometryCollection coll;
        GeometryPathIterator current;
 
        public GeometryCollectionPathIterator(GeometryCollection _coll, AffineTransform _at) {
            super(_at);
            coll = _coll;
            current = getPathIterator(coll.getGeometryN(index), _at);
        }
 
        public boolean isDone() {
            return index > coll.getNumGeometries();
        }
 
        public void next() {
            current.next();
            if (current.isDone()) {
                index++;
                if (index < coll.getNumGeometries()) {
                    current = getPathIterator(coll.getGeometryN(index), at);
                }
            }
        }
 
        public int currentSegment(float[] coords) {
            return current.currentSegment(coords);
        }
 
        public int currentSegment(double[] coords) {
            return current.currentSegment(coords);
        }
    }
}