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

History | View | Annotate | Download (7.14 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.force2D());
80
        }
81
        return other;
82
    }
83

    
84
    @Override
85
    public PathIterator getPathIterator(AffineTransform at) {
86
        MultiPolygonIterator pi = new MultiPolygonIterator(at);
87
        return pi;
88
    }
89

    
90
    @Override
91
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
92
        return getPathIterator(at);
93
    }
94

    
95
    @Override
96
    public GeneralPathX getGeneralPath() {
97
        return new DefaultGeneralPathX(getPathIterator(null), false, 0);
98
    }
99

    
100

    
101
    @Override
102
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
103
        for (Iterator iterator = primitives.iterator(); iterator.hasNext();) {
104
            ((GeometryJTS)iterator.next()).flip();
105
        }
106
        Collections.reverse(primitives);
107
    }
108

    
109
    @Override
110
    public void addSurface(Surface surface) {
111
        GeometryType geometryType = surface.getGeometryType();
112
        if(geometryType.getType() == Geometry.TYPES.POLYGON && geometryType.getSubType() == getGeometryType().getSubType()){
113
            primitives.add(surface);
114
            return;
115
        }
116
        String msg = StringUtils.replaceEach(
117
            "Only a polygon subtype  %(subtypeSurface)s can be added to a MultiPolygon subtype %(subtype)s",
118
            new String[]{"%(subtypeSurface)s","%(subtype)s"},
119
            new String[]{String.valueOf(geometryType.getSubType()), String.valueOf(getGeometryType().getSubType())}
120
        );
121
        logger.warn(msg);
122
        throw new UnsupportedOperationException(msg);
123

    
124
    }
125

    
126
    @Override
127
    public Shape getShape(AffineTransform affineTransform) {
128
        int capacity = 0;
129
        for( int i = 0; i < this.getPrimitivesNumber(); i++ ) {
130
            Polygon polygon = (Polygon) this.getPrimitiveAt(i);
131
            capacity += polygon.getNumVertices();
132
            for( int r = 0; r < polygon.getNumInteriorRings(); r++ ) {
133
                capacity += polygon.getInteriorRing(r).getNumVertices();
134
            }        
135
        }
136
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0, capacity);
137
    }
138

    
139
    @Override
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 final AffineTransform at;
148

    
149
        /** True when the point has been read once */
150
        private boolean done;
151
        private int index = 0;
152
        private final List<PathIterator>iterators;
153

    
154
        public MultiPolygonIterator(AffineTransform at) {
155
            super(new GeneralPathX());
156
            this.iterators = new ArrayList<>(primitives.size());
157
            if (at == null) {
158
                at = new AffineTransform();
159
            }
160

    
161
            this.at = at;
162
            for (Primitive primitive : primitives) {
163
                iterators.add(primitive.getPathIterator(at));
164
            }
165
            done = false;
166
        }
167

    
168
        /*
169
         * Return the winding rule for determining the interior of the path.
170
         *
171
         * @return <code>WIND_EVEN_ODD</code> by default.
172
         */
173
        @Override
174
        public int getWindingRule() {
175
            return PathIterator.WIND_EVEN_ODD;
176
        }
177

    
178
        /*
179
         * @see java.awt.geom.PathIterator#next()
180
         */
181
        @Override
182
        public void next() {
183
            PathIterator pathIteratorPrimitive = iterators.get(index);
184
            pathIteratorPrimitive.next();
185
            if(pathIteratorPrimitive.isDone()){
186
                index++;
187
                done = (index==primitives.size());
188
            }
189
        }
190

    
191
        /*
192
         * @see java.awt.geom.PathIterator#isDone()
193
         */
194
        @Override
195
        public boolean isDone() {
196
            return done;
197
        }
198

    
199
        /*
200
         * @see java.awt.geom.PathIterator#currentSegment(double[])
201
         */
202
        @Override
203
        public int currentSegment(double[] coords) {
204
            return iterators.get(index).currentSegment(coords);
205
        }
206

    
207
        /*
208
         * @see java.awt.geom.PathIterator#currentSegment(float[])
209
         */
210
        @Override
211
        public int currentSegment(float[] coords) {
212
            return iterators.get(index).currentSegment(coords);
213
        }
214
    }
215

    
216
}