svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / aggregate / AbstractMultiPoint.java @ 47432
History | View | Annotate | Download (8.22 KB)
1 | 42267 | fdiaz | /* gvSIG. Desktop Geographic Information System.
|
---|---|---|---|
2 | *
|
||
3 | * Copyright ? 2007-2015 gvSIG Association
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
18 | * MA 02110-1301, USA.
|
||
19 | *
|
||
20 | * For any additional information, do not hesitate to contact us
|
||
21 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
22 | */
|
||
23 | package org.gvsig.fmap.geom.jts.aggregate; |
||
24 | |||
25 | import java.awt.Shape; |
||
26 | import java.awt.geom.AffineTransform; |
||
27 | import java.awt.geom.PathIterator; |
||
28 | 42281 | fdiaz | import java.util.Collections; |
29 | 42267 | fdiaz | import java.util.Iterator; |
30 | import org.gvsig.fmap.geom.Geometry; |
||
31 | import org.gvsig.fmap.geom.aggregate.MultiPoint; |
||
32 | import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX; |
||
33 | import org.gvsig.fmap.geom.jts.gputils.GeneralPathXIterator; |
||
34 | import org.gvsig.fmap.geom.jts.primitive.point.PointJTS; |
||
35 | import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence; |
||
36 | import org.gvsig.fmap.geom.jts.util.JTSUtils; |
||
37 | 42281 | fdiaz | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
38 | import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
||
39 | 42267 | fdiaz | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
40 | import org.gvsig.fmap.geom.primitive.Point; |
||
41 | import org.gvsig.fmap.geom.primitive.Primitive; |
||
42 | |||
43 | |||
44 | /**
|
||
45 | * @author fdiaz
|
||
46 | *
|
||
47 | */
|
||
48 | public abstract class AbstractMultiPoint extends AbstractMultiPrimitive implements MultiPoint { |
||
49 | |||
50 | /**
|
||
51 | *
|
||
52 | */
|
||
53 | private static final long serialVersionUID = 7056816718895043774L; |
||
54 | |||
55 | /**
|
||
56 | * @param subtype
|
||
57 | */
|
||
58 | public AbstractMultiPoint(int subtype) { |
||
59 | super(Geometry.TYPES.MULTIPOINT, subtype);
|
||
60 | } |
||
61 | |||
62 | 44612 | jjdelcerro | @Override
|
63 | 42267 | fdiaz | public com.vividsolutions.jts.geom.Geometry getJTS() {
|
64 | ArrayListCoordinateSequence coordinateSequence = new ArrayListCoordinateSequence();
|
||
65 | 42271 | fdiaz | for (Iterator<Primitive> iterator = primitives.iterator(); iterator.hasNext();) { |
66 | 42267 | fdiaz | PointJTS point = (PointJTS) iterator.next(); |
67 | coordinateSequence.add(point.getJTSCoordinate()); |
||
68 | } |
||
69 | return JTSUtils.createJTSMultiPoint(coordinateSequence);
|
||
70 | } |
||
71 | |||
72 | 44612 | jjdelcerro | |
73 | @Override
|
||
74 | public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
75 | MultiPoint2D other = new MultiPoint2D();
|
||
76 | other.setProjection(this.getProjection());
|
||
77 | other.ensureCapacity(primitives.size()); |
||
78 | for (Primitive primitive : primitives) {
|
||
79 | other.addPrimitive((Primitive)primitive.force2D()); |
||
80 | } |
||
81 | return other;
|
||
82 | } |
||
83 | |||
84 | @Override
|
||
85 | 47432 | fdiaz | public Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException { |
86 | MultiPoint2DM other = new MultiPoint2DM();
|
||
87 | other.setProjection(this.getProjection());
|
||
88 | other.ensureCapacity(primitives.size()); |
||
89 | for (Primitive primitive : primitives) {
|
||
90 | other.addPrimitive(((Point) primitive).force2DM());
|
||
91 | } |
||
92 | return other;
|
||
93 | } |
||
94 | |||
95 | @Override
|
||
96 | public Geometry force3D() throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
97 | MultiPoint3D other = new MultiPoint3D();
|
||
98 | other.setProjection(this.getProjection());
|
||
99 | other.ensureCapacity(primitives.size()); |
||
100 | for (Primitive primitive : primitives) {
|
||
101 | other.addPrimitive(((Point) primitive).force3D());
|
||
102 | } |
||
103 | return other;
|
||
104 | } |
||
105 | |||
106 | @Override
|
||
107 | public Geometry force3DM() throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
108 | MultiPoint3DM other = new MultiPoint3DM();
|
||
109 | other.setProjection(this.getProjection());
|
||
110 | other.ensureCapacity(primitives.size()); |
||
111 | for (Primitive primitive : primitives) {
|
||
112 | other.addPrimitive(((Point) primitive).force3DM());
|
||
113 | } |
||
114 | return other;
|
||
115 | } |
||
116 | |||
117 | |||
118 | @Override
|
||
119 | public Geometry forceSubtype(int subtype) throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
120 | switch(subtype){
|
||
121 | case Geometry.SUBTYPES.GEOM2D:
|
||
122 | return force2D();
|
||
123 | case Geometry.SUBTYPES.GEOM2DM:
|
||
124 | return force2DM();
|
||
125 | case Geometry.SUBTYPES.GEOM3D:
|
||
126 | return force3D();
|
||
127 | case Geometry.SUBTYPES.GEOM3DM:
|
||
128 | return force3DM();
|
||
129 | } |
||
130 | return this; |
||
131 | } |
||
132 | |||
133 | |||
134 | |||
135 | @Override
|
||
136 | 42267 | fdiaz | public void addPoint(Point point) { |
137 | 42875 | fdiaz | point = fixPoint(point); |
138 | 42267 | fdiaz | primitives.add(point); |
139 | } |
||
140 | |||
141 | 44612 | jjdelcerro | @Override
|
142 | 42267 | fdiaz | public Point getPointAt(int index) { |
143 | return (Point)primitives.get(index); |
||
144 | } |
||
145 | |||
146 | 44612 | jjdelcerro | @Override
|
147 | 42267 | fdiaz | public Shape getShape(AffineTransform affineTransform) { |
148 | return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0); |
||
149 | } |
||
150 | |||
151 | 44612 | jjdelcerro | @Override
|
152 | 42267 | fdiaz | public Shape getShape() { |
153 | return new DefaultGeneralPathX(getPathIterator(null),false,0); |
||
154 | } |
||
155 | |||
156 | 44612 | jjdelcerro | @Override
|
157 | 42267 | fdiaz | public PathIterator getPathIterator(AffineTransform at) { |
158 | PointIterator pi = new PointIterator(at);
|
||
159 | return pi;
|
||
160 | } |
||
161 | |||
162 | 44612 | jjdelcerro | @Override
|
163 | 42267 | fdiaz | public PathIterator getPathIterator(AffineTransform at, double flatness) { |
164 | return getPathIterator(at);
|
||
165 | } |
||
166 | |||
167 | protected class PointIterator extends GeneralPathXIterator { |
||
168 | |||
169 | /** Transform applied on the coordinates during iteration */
|
||
170 | 44612 | jjdelcerro | private final AffineTransform at; |
171 | 42267 | fdiaz | |
172 | /** True when the point has been read once */
|
||
173 | private boolean done; |
||
174 | private int index = 0; |
||
175 | |||
176 | public PointIterator(AffineTransform at) { |
||
177 | super(new GeneralPathX()); |
||
178 | if (at == null) { |
||
179 | at = new AffineTransform(); |
||
180 | } |
||
181 | |||
182 | this.at = at;
|
||
183 | done = false;
|
||
184 | } |
||
185 | |||
186 | /**
|
||
187 | * Return the winding rule for determining the interior of the path.
|
||
188 | *
|
||
189 | * @return <code>WIND_EVEN_ODD</code> by default.
|
||
190 | */
|
||
191 | 44612 | jjdelcerro | @Override
|
192 | 42267 | fdiaz | public int getWindingRule() { |
193 | return PathIterator.WIND_EVEN_ODD; |
||
194 | } |
||
195 | |||
196 | /**
|
||
197 | * @see java.awt.geom.PathIterator#next()
|
||
198 | */
|
||
199 | 44612 | jjdelcerro | @Override
|
200 | 42267 | fdiaz | public void next() { |
201 | 42707 | fdiaz | done = (primitives.size() == ++index); |
202 | 42267 | fdiaz | } |
203 | |||
204 | /**
|
||
205 | 44612 | jjdelcerro | * @return
|
206 | 42267 | fdiaz | * @see java.awt.geom.PathIterator#isDone()
|
207 | */
|
||
208 | 44612 | jjdelcerro | @Override
|
209 | 42267 | fdiaz | public boolean isDone() { |
210 | return done;
|
||
211 | } |
||
212 | |||
213 | /**
|
||
214 | 44612 | jjdelcerro | * @return
|
215 | 42267 | fdiaz | * @see java.awt.geom.PathIterator#currentSegment(double[])
|
216 | */
|
||
217 | 44612 | jjdelcerro | @Override
|
218 | 42267 | fdiaz | public int currentSegment(double[] coords) { |
219 | Point point = (Point) primitives.get(index); |
||
220 | coords[0] = point.getX();
|
||
221 | coords[1] = point.getY();
|
||
222 | at.transform(coords, 0, coords, 0, 1); |
||
223 | return PathIterator.SEG_MOVETO; |
||
224 | } |
||
225 | |||
226 | /*
|
||
227 | * @see java.awt.geom.PathIterator#currentSegment(float[])
|
||
228 | */
|
||
229 | 44612 | jjdelcerro | @Override
|
230 | 42267 | fdiaz | public int currentSegment(float[] coords) { |
231 | Point point = (Point) primitives.get(index); |
||
232 | coords[0] = (float)point.getX(); |
||
233 | coords[1] = (float)point.getY(); |
||
234 | at.transform(coords, 0, coords, 0, 1); |
||
235 | return PathIterator.SEG_MOVETO; |
||
236 | } |
||
237 | } |
||
238 | |||
239 | 44612 | jjdelcerro | @Override
|
240 | 42281 | fdiaz | public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException { |
241 | Collections.reverse(primitives);
|
||
242 | } |
||
243 | 42441 | fdiaz | |
244 | |||
245 | 44612 | jjdelcerro | @Override
|
246 | 42441 | fdiaz | public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
247 | 44099 | jjdelcerro | return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance)); |
248 | 42441 | fdiaz | } |
249 | 42875 | fdiaz | |
250 | 45762 | fdiaz | @Override
|
251 | public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
252 | return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance, JTSUtils.calculateQuadrantSegments(joinStyle))); |
||
253 | } |
||
254 | |||
255 | 42875 | fdiaz | protected abstract Point fixPoint(Point point); |
256 | 42267 | fdiaz | } |