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 / DefaultMultiCurve.java @ 44612
History | View | Annotate | Download (7.95 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.List; |
28 |
|
29 |
import com.vividsolutions.jts.geom.LineString; |
30 |
|
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
|
33 |
import org.gvsig.fmap.geom.Geometry; |
34 |
import org.gvsig.fmap.geom.GeometryException; |
35 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
36 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
37 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
38 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
39 |
import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
40 |
import org.gvsig.fmap.geom.jts.GeometryJTS; |
41 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
42 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
43 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
44 |
import org.gvsig.fmap.geom.primitive.Line; |
45 |
import org.gvsig.fmap.geom.primitive.Primitive; |
46 |
import org.gvsig.fmap.geom.type.GeometryType; |
47 |
|
48 |
|
49 |
/**
|
50 |
* @author fdiaz
|
51 |
*
|
52 |
*/
|
53 |
public class DefaultMultiCurve extends AbstractMultiCurve { |
54 |
|
55 |
/**
|
56 |
* @param geometryType
|
57 |
*/
|
58 |
public DefaultMultiCurve(GeometryType geometryType) {
|
59 |
super(geometryType.getSubType());
|
60 |
} |
61 |
|
62 |
/**
|
63 |
* @param subtype
|
64 |
*/
|
65 |
public DefaultMultiCurve(int subtype) { |
66 |
super(subtype);
|
67 |
} |
68 |
|
69 |
@Override
|
70 |
public Geometry cloneGeometry() {
|
71 |
DefaultMultiCurve clone = new DefaultMultiCurve(getGeometryType());
|
72 |
for(int i=0; i<primitives.size(); i++){ |
73 |
clone.addPrimitive((Primitive)primitives.get(i).cloneGeometry()); |
74 |
} |
75 |
return clone;
|
76 |
} |
77 |
|
78 |
@Override
|
79 |
public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException { |
80 |
DefaultMultiCurve other = new DefaultMultiCurve(getGeometryType());
|
81 |
other.setProjection(this.getProjection());
|
82 |
other.ensureCapacity(primitives.size()); |
83 |
for (Primitive primitive : primitives) {
|
84 |
other.addPrimitive((Primitive)primitive.force2D()); |
85 |
} |
86 |
return other;
|
87 |
} |
88 |
|
89 |
@Override
|
90 |
public int getDimension() { |
91 |
switch (getGeometryType().getSubType()) {
|
92 |
case Geometry.SUBTYPES.GEOM2D:
|
93 |
return 2; |
94 |
case Geometry.SUBTYPES.GEOM2DM:
|
95 |
return 3; |
96 |
case Geometry.SUBTYPES.GEOM3D:
|
97 |
return 3; |
98 |
case Geometry.SUBTYPES.GEOM3DM:
|
99 |
return 4; |
100 |
default:
|
101 |
return 0; |
102 |
} |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public MultiPoint toPoints() throws GeometryException { |
107 |
MultiPoint multipoint = null;
|
108 |
for (int i = 0; i < this.getPrimitivesNumber(); i++) { |
109 |
if(multipoint==null){ |
110 |
multipoint = this.getPrimitiveAt(i).toPoints();
|
111 |
} else {
|
112 |
MultiPoint points = (MultiPoint)this.getPrimitiveAt(i).toPoints();
|
113 |
for (int j = 0; j < points.getPrimitivesNumber(); j++) { |
114 |
multipoint.addPrimitive(points.getPrimitiveAt(j)); |
115 |
} |
116 |
} |
117 |
} |
118 |
return multipoint;
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
public MultiLine toLines() throws GeometryException { |
123 |
MultiLine multiline = null;
|
124 |
for (int i = 0; i < this.getPrimitivesNumber(); i++) { |
125 |
if(multiline==null){ |
126 |
multiline = this.getPrimitiveAt(i).toLines();
|
127 |
} else {
|
128 |
MultiLine lines = (MultiLine)this.getPrimitiveAt(i).toLines();
|
129 |
for (int j = 0; j < lines.getPrimitivesNumber(); j++) { |
130 |
multiline.addPrimitive(lines.getPrimitiveAt(j)); |
131 |
} |
132 |
} |
133 |
} |
134 |
return multiline;
|
135 |
} |
136 |
|
137 |
@Override
|
138 |
public MultiPolygon toPolygons() throws GeometryException { |
139 |
MultiPolygon multipolygon = null;
|
140 |
for (int i = 0; i < this.getPrimitivesNumber(); i++) { |
141 |
if(multipolygon==null){ |
142 |
multipolygon = this.getPrimitiveAt(i).toPolygons();
|
143 |
} else {
|
144 |
MultiPolygon polygons = (MultiPolygon)this.getPrimitiveAt(i).toPolygons();
|
145 |
for (int j = 0; j < polygons.getPrimitivesNumber(); j++) { |
146 |
multipolygon.addPrimitive(polygons.getPrimitiveAt(j)); |
147 |
} |
148 |
} |
149 |
} |
150 |
return multipolygon;
|
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException { |
155 |
for (int i = 0; i < this.getPrimitivesNumber(); i++) { |
156 |
this.getPrimitiveAt(i).flip();
|
157 |
} |
158 |
Collections.reverse(primitives);
|
159 |
} |
160 |
|
161 |
|
162 |
@Override
|
163 |
protected Geometry fixPrimitive(Primitive primitive) {
|
164 |
int primitiveSubType = primitive.getGeometryType().getSubType();
|
165 |
int subType = getGeometryType().getSubType();
|
166 |
if(primitiveSubType == subType){
|
167 |
return primitive;
|
168 |
} |
169 |
|
170 |
String message = StringUtils.replace("This MultiCurve only accept subtype %(subtype)s primitives", "%(subtype)s", String.valueOf(subType)); |
171 |
notifyDeprecated(message); |
172 |
throw new UnsupportedOperationException(message); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public com.vividsolutions.jts.geom.Geometry getJTS() {
|
177 |
List<LineString> lineStrings = new ArrayList<>(primitives.size()); |
178 |
|
179 |
LineString line; |
180 |
for (Primitive primitive : primitives) {
|
181 |
if (primitive instanceof Line) { |
182 |
line = (LineString) ((GeometryJTS) primitive).getJTS(); |
183 |
lineStrings.add(line); |
184 |
} else {
|
185 |
MultiLine multiline; |
186 |
try {
|
187 |
multiline = primitive.toLines(); |
188 |
for (int j = 0; j < multiline.getPrimitivesNumber(); j++) { |
189 |
line = |
190 |
(LineString) ((GeometryJTS) multiline.getPrimitiveAt(j)).getJTS(); |
191 |
lineStrings.add(line); |
192 |
} |
193 |
} catch (GeometryException e) {
|
194 |
GeometryType geomType = primitive.getGeometryType(); |
195 |
logger.warn(StringUtils.replaceEach( |
196 |
"Can't convert primitive type=%(type)s, %(subtype)s to MultiLine",
|
197 |
new String[] {"%(type)s", "%(subtype)s" }, |
198 |
new String[] { String.valueOf(geomType.getType()), String.valueOf(geomType.getSubType()) })); |
199 |
} |
200 |
} |
201 |
} |
202 |
return JTSUtils.createJTSMultiLineString(lineStrings.toArray(new LineString[lineStrings.size()])); |
203 |
} |
204 |
|
205 |
@Override
|
206 |
public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
207 |
MultiCurve result = new DefaultMultiCurve(getGeometryType());
|
208 |
for (Primitive primitive : primitives) {
|
209 |
Geometry offset = primitive.offset(distance); |
210 |
if(offset instanceof MultiPrimitive){ |
211 |
MultiPrimitive multiOffset = (MultiPrimitive)offset; |
212 |
for(int i=0; i<multiOffset.getPrimitivesNumber(); i++){ |
213 |
result.addPrimitive(multiOffset.getPrimitiveAt(i)); |
214 |
} |
215 |
} else {
|
216 |
result.addPrimitive((Primitive)primitive.offset(distance)); |
217 |
} |
218 |
} |
219 |
return result;
|
220 |
} |
221 |
} |