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 / DefaultMultiSurface.java @ 47755
History | View | Annotate | Download (11.1 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.util.ArrayList; |
26 |
import java.util.Collections; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
import org.apache.commons.lang3.StringUtils; |
30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.GeometryException; |
32 |
import org.gvsig.fmap.geom.GeometryLocator; |
33 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
34 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
35 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
36 |
import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
37 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
38 |
import org.gvsig.fmap.geom.jts.GeometryJTS; |
39 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
40 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
41 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
42 |
import org.gvsig.fmap.geom.primitive.Polygon; |
43 |
import org.gvsig.fmap.geom.primitive.Primitive; |
44 |
import org.gvsig.fmap.geom.primitive.Surface; |
45 |
import org.gvsig.fmap.geom.type.GeometryType; |
46 |
|
47 |
|
48 |
/**
|
49 |
* @author fdiaz
|
50 |
*
|
51 |
*/
|
52 |
public class DefaultMultiSurface extends AbstractMultiSurface { |
53 |
|
54 |
private static final long serialVersionUID = 9211543270911740110L; |
55 |
|
56 |
|
57 |
public DefaultMultiSurface(GeometryType geometryType) {
|
58 |
super(geometryType.getSubType());
|
59 |
} |
60 |
|
61 |
@Override
|
62 |
public Geometry cloneGeometry() {
|
63 |
DefaultMultiSurface clone = new DefaultMultiSurface(getGeometryType());
|
64 |
for(int i=0; i<primitives.size(); i++){ |
65 |
clone.addPrimitive((Primitive)primitives.get(i).cloneGeometry()); |
66 |
} |
67 |
clone.setProjection(this.getProjection());
|
68 |
return clone;
|
69 |
} |
70 |
|
71 |
@Override
|
72 |
public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException { |
73 |
DefaultMultiSurface other = new DefaultMultiSurface(getGeometryType());
|
74 |
other.setProjection(this.getProjection());
|
75 |
other.ensureCapacity(primitives.size()); |
76 |
for (Primitive primitive : primitives) {
|
77 |
other.addPrimitive((Primitive)primitive.forceSubtype(Geometry.SUBTYPES.GEOM2D)); |
78 |
} |
79 |
return other;
|
80 |
} |
81 |
|
82 |
@Override
|
83 |
public Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException { |
84 |
DefaultMultiSurface other = new DefaultMultiSurface(getGeometryType());
|
85 |
other.setProjection(this.getProjection());
|
86 |
other.ensureCapacity(primitives.size()); |
87 |
for (Primitive primitive : primitives) {
|
88 |
other.addPrimitive((Primitive)primitive.forceSubtype(Geometry.SUBTYPES.GEOM2DM)); |
89 |
} |
90 |
return other;
|
91 |
} |
92 |
|
93 |
@Override
|
94 |
public Geometry force3D() throws GeometryOperationNotSupportedException, GeometryOperationException { |
95 |
DefaultMultiSurface other = new DefaultMultiSurface(getGeometryType());
|
96 |
other.setProjection(this.getProjection());
|
97 |
other.ensureCapacity(primitives.size()); |
98 |
for (Primitive primitive : primitives) {
|
99 |
other.addPrimitive((Primitive)primitive.forceSubtype(Geometry.SUBTYPES.GEOM3D)); |
100 |
} |
101 |
return other;
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public Geometry force3DM() throws GeometryOperationNotSupportedException, GeometryOperationException { |
106 |
DefaultMultiSurface other = new DefaultMultiSurface(getGeometryType());
|
107 |
other.setProjection(this.getProjection());
|
108 |
other.ensureCapacity(primitives.size()); |
109 |
for (Primitive primitive : primitives) {
|
110 |
other.addPrimitive((Primitive)primitive.forceSubtype(Geometry.SUBTYPES.GEOM3DM)); |
111 |
} |
112 |
return other;
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public void addSurface(Surface surface) { |
117 |
GeometryType geometryType = surface.getGeometryType(); |
118 |
if(geometryType.getSubType() == getGeometryType().getSubType()){
|
119 |
primitives.add(surface); |
120 |
return;
|
121 |
} |
122 |
String msg = StringUtils.replaceEach(
|
123 |
"Only a surface subtype %(subtypeSurface)s can be added to a MultiSurface subtype %(subtype)s",
|
124 |
new String[]{"%(subtypeSurface)s","%(subtype)s"}, |
125 |
new String[]{String.valueOf(geometryType.getSubType()), String.valueOf(getGeometryType().getSubType())} |
126 |
); |
127 |
LOGGER.warn(msg); |
128 |
throw new UnsupportedOperationException(msg); |
129 |
|
130 |
} |
131 |
|
132 |
|
133 |
@Override
|
134 |
public int getDimension() { |
135 |
switch (getGeometryType().getSubType()) {
|
136 |
case Geometry.SUBTYPES.GEOM2D:
|
137 |
return 2; |
138 |
case Geometry.SUBTYPES.GEOM2DM:
|
139 |
return 3; |
140 |
case Geometry.SUBTYPES.GEOM3D:
|
141 |
return 3; |
142 |
case Geometry.SUBTYPES.GEOM3DM:
|
143 |
return 4; |
144 |
default:
|
145 |
return 0; |
146 |
} |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public MultiPoint toPoints() throws GeometryException { |
151 |
MultiPoint multiPoint = GeometryLocator.getGeometryManager().createMultiPoint(getGeometryType().getSubType()); |
152 |
for (Primitive primitive : primitives) {
|
153 |
MultiPoint primitivePoints = primitive.toPoints(); |
154 |
for (int i=0; i<primitivePoints.getPrimitivesNumber(); i++) { |
155 |
multiPoint.addPoint(primitivePoints.getPointAt(i)); |
156 |
} |
157 |
} |
158 |
return multiPoint;
|
159 |
} |
160 |
|
161 |
@Override
|
162 |
public MultiLine toLines() throws GeometryException { |
163 |
MultiLine multiLine = GeometryLocator.getGeometryManager().createMultiLine(getGeometryType().getSubType()); |
164 |
for (Primitive primitive : primitives) {
|
165 |
MultiLine primitiveLines = primitive.toLines(); |
166 |
for (int i=0; i<primitiveLines.getPrimitivesNumber(); i++) { |
167 |
multiLine.addPrimitive((Primitive)primitiveLines.getPrimitiveAt(i).cloneGeometry()); |
168 |
} |
169 |
} |
170 |
return multiLine;
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public MultiPolygon toPolygons() throws GeometryException { |
175 |
MultiPolygon multiPolygon = GeometryLocator.getGeometryManager().createMultiPolygon(getGeometryType().getSubType()); |
176 |
for (Primitive primitive : primitives) {
|
177 |
MultiPolygon primitivePolygons = primitive.toPolygons(); |
178 |
for (int i=0; i<primitivePolygons.getPrimitivesNumber(); i++) { |
179 |
multiPolygon.addPrimitive((Primitive)primitivePolygons.getPrimitiveAt(i).cloneGeometry()); |
180 |
} |
181 |
} |
182 |
return multiPolygon;
|
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException { |
187 |
for (Iterator iterator = primitives.iterator(); iterator.hasNext();) { |
188 |
GeometryJTS primitive = (GeometryJTS) iterator.next(); |
189 |
primitive.flip(); |
190 |
} |
191 |
Collections.reverse(primitives);
|
192 |
} |
193 |
|
194 |
/* (non-Javadoc)
|
195 |
* @see org.gvsig.fmap.geom.jts.aggregate.AbstractMultiPrimitive#fixPrimitive(org.gvsig.fmap.geom.primitive.Primitive)
|
196 |
*/
|
197 |
@Override
|
198 |
protected Geometry fixPrimitive(Primitive primitive) {
|
199 |
int primitiveSubType = primitive.getGeometryType().getSubType();
|
200 |
int subType = getGeometryType().getSubType();
|
201 |
if(primitiveSubType == subType){
|
202 |
return primitive;
|
203 |
} |
204 |
|
205 |
String message = StringUtils.replace("This MultiSurface only accept subtype %(subtype)s primitives", "%(subtype)s", String.valueOf(subType)); |
206 |
notifyDeprecated(message); |
207 |
throw new UnsupportedOperationException(message); |
208 |
} |
209 |
|
210 |
|
211 |
@Override
|
212 |
public com.vividsolutions.jts.geom.Geometry getJTS() {
|
213 |
List<com.vividsolutions.jts.geom.Polygon> polygons =new ArrayList<>(primitives.size()); |
214 |
com.vividsolutions.jts.geom.Polygon polygon; |
215 |
for (Primitive primitive : primitives) {
|
216 |
if (primitive instanceof Polygon) { |
217 |
polygon = (com.vividsolutions.jts.geom.Polygon) ((GeometryJTS) primitive).getJTS(); |
218 |
polygons.add(polygon); |
219 |
} else {
|
220 |
MultiPolygon multipolygon; |
221 |
try {
|
222 |
multipolygon = primitive.toPolygons(); |
223 |
for (int j = 0; j < multipolygon.getPrimitivesNumber(); j++) { |
224 |
polygon = |
225 |
(com.vividsolutions.jts.geom.Polygon) ((GeometryJTS) multipolygon.getPrimitiveAt(j)).getJTS(); |
226 |
polygons.add(polygon); |
227 |
} |
228 |
} catch (GeometryException e) {
|
229 |
GeometryType geomType = primitive.getGeometryType(); |
230 |
LOGGER.warn(StringUtils.replaceEach( |
231 |
"Can't convert primitive type=%(type)s, %(subtype)s to MultiPolygon",
|
232 |
new String[] {"%(type)s", "%(subtype)s" }, |
233 |
new String[] { String.valueOf(geomType.getType()), String.valueOf(geomType.getSubType()) })); |
234 |
} |
235 |
} |
236 |
} |
237 |
return JTSUtils
|
238 |
.createJTSMultiPolygon(getProjection(), polygons.toArray(new com.vividsolutions.jts.geom.Polygon[polygons.size()]));
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
243 |
MultiSurface result = new DefaultMultiSurface(getGeometryType());
|
244 |
for (Primitive primitive : primitives) {
|
245 |
Geometry offset = primitive.offset(distance); |
246 |
if(offset instanceof MultiPrimitive){ |
247 |
MultiPrimitive multiOffset = (MultiPrimitive)offset; |
248 |
for(int i=0; i<multiOffset.getPrimitivesNumber(); i++){ |
249 |
result.addPrimitive(multiOffset.getPrimitiveAt(i)); |
250 |
} |
251 |
} else {
|
252 |
result.addPrimitive((Primitive)primitive.offset(distance)); |
253 |
} |
254 |
} |
255 |
return result;
|
256 |
} |
257 |
|
258 |
@Override
|
259 |
public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
260 |
MultiSurface result = new DefaultMultiSurface(getGeometryType());
|
261 |
for (Primitive primitive : primitives) {
|
262 |
Geometry offset = primitive.offset(joinStyle, distance); |
263 |
if(offset instanceof MultiPrimitive){ |
264 |
MultiPrimitive multiOffset = (MultiPrimitive)offset; |
265 |
for(int i=0; i<multiOffset.getPrimitivesNumber(); i++){ |
266 |
result.addPrimitive(multiOffset.getPrimitiveAt(i)); |
267 |
} |
268 |
} else {
|
269 |
result.addPrimitive((Primitive)primitive.offset(joinStyle, distance)); |
270 |
} |
271 |
} |
272 |
return result;
|
273 |
} |
274 |
|
275 |
} |