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 / AbstractMultiPolygon.java @ 42358
History | View | Annotate | Download (7.32 KB)
1 |
/* 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 |
import java.util.ArrayList; |
29 |
import java.util.Collections; |
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
|
33 |
import org.apache.commons.lang3.StringUtils; |
34 |
|
35 |
import org.gvsig.fmap.geom.Geometry; |
36 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
37 |
import org.gvsig.fmap.geom.jts.GeometryJTS; |
38 |
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX; |
39 |
import org.gvsig.fmap.geom.jts.gputils.GeneralPathXIterator; |
40 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
41 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
42 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
43 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
44 |
import org.gvsig.fmap.geom.primitive.Primitive; |
45 |
import org.gvsig.fmap.geom.primitive.Surface; |
46 |
import org.gvsig.fmap.geom.type.GeometryType; |
47 |
|
48 |
|
49 |
/**
|
50 |
* @author fdiaz
|
51 |
*
|
52 |
*/
|
53 |
public abstract class AbstractMultiPolygon extends AbstractMultiSurface implements MultiPolygon { |
54 |
|
55 |
/**
|
56 |
*
|
57 |
*/
|
58 |
private static final long serialVersionUID = 6663703214829442424L; |
59 |
|
60 |
/**
|
61 |
* @param type
|
62 |
*/
|
63 |
public AbstractMultiPolygon(int subtype) { |
64 |
super(Geometry.TYPES.MULTIPOLYGON, subtype);
|
65 |
} |
66 |
|
67 |
/* (non-Javadoc)
|
68 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
|
69 |
*/
|
70 |
public com.vividsolutions.jts.geom.Geometry getJTS() {
|
71 |
com.vividsolutions.jts.geom.Polygon[] polygons = new com.vividsolutions.jts.geom.Polygon[primitives.size()]; |
72 |
for (int i=0; i<primitives.size(); i++){ |
73 |
polygons[i] = (com.vividsolutions.jts.geom.Polygon) ((GeometryJTS)primitives.get(i)).getJTS(); |
74 |
} |
75 |
return JTSUtils.createJTSMultiPolygon(polygons);
|
76 |
} |
77 |
|
78 |
/* (non-Javadoc)
|
79 |
* @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform)
|
80 |
*/
|
81 |
public PathIterator getPathIterator(AffineTransform at) { |
82 |
MultiPolygonIterator pi = new MultiPolygonIterator(at);
|
83 |
return pi;
|
84 |
} |
85 |
|
86 |
/* (non-Javadoc)
|
87 |
* @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform, double)
|
88 |
*/
|
89 |
public PathIterator getPathIterator(AffineTransform at, double flatness) { |
90 |
return getPathIterator(at);
|
91 |
} |
92 |
|
93 |
/* (non-Javadoc)
|
94 |
* @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
|
95 |
*/
|
96 |
public GeneralPathX getGeneralPath() {
|
97 |
return new DefaultGeneralPathX(getPathIterator(null), false, 0); |
98 |
} |
99 |
|
100 |
|
101 |
/* (non-Javadoc)
|
102 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
|
103 |
*/
|
104 |
public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException { |
105 |
for (Iterator iterator = primitives.iterator(); iterator.hasNext();) { |
106 |
((GeometryJTS)iterator.next()).flip(); |
107 |
} |
108 |
Collections.reverse(primitives);
|
109 |
} |
110 |
|
111 |
/* (non-Javadoc)
|
112 |
* @see org.gvsig.fmap.geom.aggregate.MultiSurface#addSurface(org.gvsig.fmap.geom.primitive.Surface)
|
113 |
*/
|
114 |
public void addSurface(Surface surface) { |
115 |
GeometryType geometryType = surface.getGeometryType(); |
116 |
if(geometryType.getType() == Geometry.TYPES.POLYGON && geometryType.getSubType() == getGeometryType().getSubType()){
|
117 |
primitives.add(surface); |
118 |
return;
|
119 |
} |
120 |
String msg = StringUtils.replaceEach(
|
121 |
"Only a polygon subtype %(subtypeSurface)s can be added to a MultiPolygon subtype %(subtype)s",
|
122 |
new String[]{"%(subtypeSurface)s","%(subtype)s"}, |
123 |
new String[]{String.valueOf(geometryType.getSubType()), String.valueOf(getGeometryType().getSubType())} |
124 |
); |
125 |
logger.warn(msg); |
126 |
throw new UnsupportedOperationException(msg); |
127 |
|
128 |
} |
129 |
|
130 |
/* (non-Javadoc)
|
131 |
* @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
|
132 |
*/
|
133 |
public Shape getShape(AffineTransform affineTransform) { |
134 |
return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0); |
135 |
} |
136 |
|
137 |
/* (non-Javadoc)
|
138 |
* @see org.gvsig.fmap.geom.Geometry#getShape()
|
139 |
*/
|
140 |
public Shape getShape() { |
141 |
return getShape(null); |
142 |
} |
143 |
|
144 |
protected class MultiPolygonIterator extends GeneralPathXIterator { |
145 |
|
146 |
/** Transform applied on the coordinates during iteration */
|
147 |
private AffineTransform at; |
148 |
|
149 |
/** True when the point has been read once */
|
150 |
private boolean done; |
151 |
private int index = 0; |
152 |
private List<PathIterator>iterators = new ArrayList<PathIterator>(primitives.size()); |
153 |
|
154 |
/**
|
155 |
* Creates a new PointIterator object.
|
156 |
*
|
157 |
* @param p
|
158 |
* The polygon
|
159 |
* @param at
|
160 |
* The affine transform applied to coordinates during
|
161 |
* iteration
|
162 |
*/
|
163 |
public MultiPolygonIterator(AffineTransform at) { |
164 |
super(new GeneralPathX()); |
165 |
if (at == null) { |
166 |
at = new AffineTransform(); |
167 |
} |
168 |
|
169 |
this.at = at;
|
170 |
for (Iterator iterator = primitives.iterator(); iterator.hasNext();) { |
171 |
Primitive primitive = (Primitive) iterator.next(); |
172 |
iterators.add(primitive.getPathIterator(at)); |
173 |
} |
174 |
done = false;
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* Return the winding rule for determining the interior of the path.
|
179 |
*
|
180 |
* @return <code>WIND_EVEN_ODD</code> by default.
|
181 |
*/
|
182 |
public int getWindingRule() { |
183 |
return PathIterator.WIND_EVEN_ODD; |
184 |
} |
185 |
|
186 |
/**
|
187 |
* @see java.awt.geom.PathIterator#next()
|
188 |
*/
|
189 |
public void next() { |
190 |
PathIterator pathIteratorPrimitive = iterators.get(index);
|
191 |
pathIteratorPrimitive.next(); |
192 |
if(pathIteratorPrimitive.isDone()){
|
193 |
index++; |
194 |
done = (index==primitives.size()); |
195 |
} |
196 |
} |
197 |
|
198 |
/**
|
199 |
* @see java.awt.geom.PathIterator#isDone()
|
200 |
*/
|
201 |
public boolean isDone() { |
202 |
return done;
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* @see java.awt.geom.PathIterator#currentSegment(double[])
|
207 |
*/
|
208 |
public int currentSegment(double[] coords) { |
209 |
return iterators.get(index).currentSegment(coords);
|
210 |
} |
211 |
|
212 |
/*
|
213 |
* (non-Javadoc)
|
214 |
*
|
215 |
* @see java.awt.geom.PathIterator#currentSegment(float[])
|
216 |
*/
|
217 |
public int currentSegment(float[] coords) { |
218 |
return iterators.get(index).currentSegment(coords);
|
219 |
} |
220 |
} |
221 |
|
222 |
} |