Statistics
| Revision:

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 @ 47432

History | View | Annotate | Download (8.51 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.Polygon;
45
import org.gvsig.fmap.geom.primitive.Primitive;
46
import org.gvsig.fmap.geom.primitive.Surface;
47
import org.gvsig.fmap.geom.type.GeometryType;
48

    
49

    
50
/**
51
 * @author fdiaz
52
 *
53
 */
54
public abstract class AbstractMultiPolygon extends AbstractMultiSurface implements MultiPolygon {
55

    
56
    /**
57
     *
58
     */
59
    private static final long serialVersionUID = 6663703214829442424L;
60

    
61
    public AbstractMultiPolygon(int subtype) {
62
        super(Geometry.TYPES.MULTIPOLYGON, subtype);
63
    }
64

    
65
    public com.vividsolutions.jts.geom.Geometry getJTS() {
66
        com.vividsolutions.jts.geom.Polygon[] polygons = new com.vividsolutions.jts.geom.Polygon[primitives.size()];
67
        for (int i=0; i<primitives.size(); i++){
68
            polygons[i] = (com.vividsolutions.jts.geom.Polygon) ((GeometryJTS)primitives.get(i)).getJTS();
69
        }
70
        return JTSUtils.createJTSMultiPolygon(polygons);
71
    }
72

    
73
    @Override
74
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
75
        MultiPolygon2D other = new MultiPolygon2D();
76
        other.setProjection(this.getProjection());
77
        other.ensureCapacity(primitives.size());
78
        for (Primitive primitive : primitives) {
79
            other.addPrimitive((Primitive)primitive.forceSubtype(Geometry.SUBTYPES.GEOM2D));
80
        }
81
        return other;
82
    }
83

    
84
    @Override
85
    public Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
86
        MultiPolygon2DM other = new MultiPolygon2DM();
87
        other.setProjection(this.getProjection());
88
        other.ensureCapacity(primitives.size());
89
        for (Primitive primitive : primitives) {
90
            other.addPrimitive((Primitive)primitive.forceSubtype(Geometry.SUBTYPES.GEOM2DM));
91
        }
92
        return other;
93
    }
94

    
95
    @Override
96
    public Geometry force3D() throws GeometryOperationNotSupportedException, GeometryOperationException {
97
        MultiPolygon3D other = new MultiPolygon3D();
98
        other.setProjection(this.getProjection());
99
        other.ensureCapacity(primitives.size());
100
        for (Primitive primitive : primitives) {
101
            other.addPrimitive((Primitive)primitive.forceSubtype(Geometry.SUBTYPES.GEOM3D));
102
        }
103
        return other;
104
    }
105

    
106
    @Override
107
    public Geometry force3DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
108
        MultiPolygon3DM other = new MultiPolygon3DM();
109
        other.setProjection(this.getProjection());
110
        other.ensureCapacity(primitives.size());
111
        for (Primitive primitive : primitives) {
112
            other.addPrimitive((Primitive)primitive.forceSubtype(Geometry.SUBTYPES.GEOM3DM));
113
        }
114
        return other;
115
    }
116

    
117
    @Override
118
    public PathIterator getPathIterator(AffineTransform at) {
119
        MultiPolygonIterator pi = new MultiPolygonIterator(at);
120
        return pi;
121
    }
122

    
123
    @Override
124
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
125
        return getPathIterator(at);
126
    }
127

    
128
    @Override
129
    public GeneralPathX getGeneralPath() {
130
        return new DefaultGeneralPathX(getPathIterator(null), false, 0);
131
    }
132

    
133

    
134
    @Override
135
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
136
        for (Iterator iterator = primitives.iterator(); iterator.hasNext();) {
137
            ((GeometryJTS)iterator.next()).flip();
138
        }
139
        Collections.reverse(primitives);
140
    }
141

    
142
    @Override
143
    public void addSurface(Surface surface) {
144
        GeometryType geometryType = surface.getGeometryType();
145
        if(geometryType.getType() == Geometry.TYPES.POLYGON && geometryType.getSubType() == getGeometryType().getSubType()){
146
            primitives.add(surface);
147
            return;
148
        }
149
        String msg = StringUtils.replaceEach(
150
            "Only a polygon subtype  %(subtypeSurface)s can be added to a MultiPolygon subtype %(subtype)s",
151
            new String[]{"%(subtypeSurface)s","%(subtype)s"},
152
            new String[]{String.valueOf(geometryType.getSubType()), String.valueOf(getGeometryType().getSubType())}
153
        );
154
        LOGGER.warn(msg);
155
        throw new UnsupportedOperationException(msg);
156

    
157
    }
158

    
159
    @Override
160
    public Shape getShape(AffineTransform affineTransform) {
161
        int capacity = 0;
162
        for( int i = 0; i < this.getPrimitivesNumber(); i++ ) {
163
            Polygon polygon = (Polygon) this.getPrimitiveAt(i);
164
            capacity += polygon.getNumVertices();
165
            for( int r = 0; r < polygon.getNumInteriorRings(); r++ ) {
166
                capacity += polygon.getInteriorRing(r).getNumVertices();
167
            }        
168
        }
169
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0, capacity);
170
    }
171

    
172
    @Override
173
    public Shape getShape() {
174
        return getShape(null);
175
    }
176

    
177
    protected class MultiPolygonIterator extends GeneralPathXIterator {
178

    
179
        /** Transform applied on the coordinates during iteration */
180
        private final AffineTransform at;
181

    
182
        /** True when the point has been read once */
183
        private boolean done;
184
        private int index = 0;
185
        private final List<PathIterator>iterators;
186

    
187
        public MultiPolygonIterator(AffineTransform at) {
188
            super(new GeneralPathX());
189
            this.iterators = new ArrayList<>(primitives.size());
190
            if (at == null) {
191
                at = new AffineTransform();
192
            }
193

    
194
            this.at = at;
195
            for (Primitive primitive : primitives) {
196
                iterators.add(primitive.getPathIterator(at));
197
            }
198
            done = false;
199
        }
200

    
201
        /*
202
         * Return the winding rule for determining the interior of the path.
203
         *
204
         * @return <code>WIND_EVEN_ODD</code> by default.
205
         */
206
        @Override
207
        public int getWindingRule() {
208
            return PathIterator.WIND_EVEN_ODD;
209
        }
210

    
211
        /*
212
         * @see java.awt.geom.PathIterator#next()
213
         */
214
        @Override
215
        public void next() {
216
            PathIterator pathIteratorPrimitive = iterators.get(index);
217
            pathIteratorPrimitive.next();
218
            if(pathIteratorPrimitive.isDone()){
219
                index++;
220
                done = (index==primitives.size());
221
            }
222
        }
223

    
224
        /*
225
         * @see java.awt.geom.PathIterator#isDone()
226
         */
227
        @Override
228
        public boolean isDone() {
229
            return done;
230
        }
231

    
232
        /*
233
         * @see java.awt.geom.PathIterator#currentSegment(double[])
234
         */
235
        @Override
236
        public int currentSegment(double[] coords) {
237
            return iterators.get(index).currentSegment(coords);
238
        }
239

    
240
        /*
241
         * @see java.awt.geom.PathIterator#currentSegment(float[])
242
         */
243
        @Override
244
        public int currentSegment(float[] coords) {
245
            return iterators.get(index).currentSegment(coords);
246
        }
247
    }
248

    
249
}