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

History | View | Annotate | Download (7.75 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
    /**
62
     * @param type
63
     */
64
    public AbstractMultiPolygon(int subtype) {
65
        super(Geometry.TYPES.MULTIPOLYGON, subtype);
66
    }
67

    
68
    /* (non-Javadoc)
69
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
70
     */
71
    public com.vividsolutions.jts.geom.Geometry getJTS() {
72
        com.vividsolutions.jts.geom.Polygon[] polygons = new com.vividsolutions.jts.geom.Polygon[primitives.size()];
73
        for (int i=0; i<primitives.size(); i++){
74
            polygons[i] = (com.vividsolutions.jts.geom.Polygon) ((GeometryJTS)primitives.get(i)).getJTS();
75
        }
76
        return JTSUtils.createJTSMultiPolygon(polygons);
77
    }
78

    
79
    /* (non-Javadoc)
80
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform)
81
     */
82
    public PathIterator getPathIterator(AffineTransform at) {
83
        MultiPolygonIterator pi = new MultiPolygonIterator(at);
84
        return pi;
85
    }
86

    
87
    /* (non-Javadoc)
88
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform, double)
89
     */
90
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
91
        return getPathIterator(at);
92
    }
93

    
94
    /* (non-Javadoc)
95
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
96
     */
97
    public GeneralPathX getGeneralPath() {
98
        return new DefaultGeneralPathX(getPathIterator(null), false, 0);
99
    }
100

    
101

    
102
    /* (non-Javadoc)
103
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
104
     */
105
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
106
        for (Iterator iterator = primitives.iterator(); iterator.hasNext();) {
107
            ((GeometryJTS)iterator.next()).flip();
108
        }
109
        Collections.reverse(primitives);
110
    }
111

    
112
    /* (non-Javadoc)
113
     * @see org.gvsig.fmap.geom.aggregate.MultiSurface#addSurface(org.gvsig.fmap.geom.primitive.Surface)
114
     */
115
    public void addSurface(Surface surface) {
116
        GeometryType geometryType = surface.getGeometryType();
117
        if(geometryType.getType() == Geometry.TYPES.POLYGON && geometryType.getSubType() == getGeometryType().getSubType()){
118
            primitives.add(surface);
119
            return;
120
        }
121
        String msg = StringUtils.replaceEach(
122
            "Only a polygon subtype  %(subtypeSurface)s can be added to a MultiPolygon subtype %(subtype)s",
123
            new String[]{"%(subtypeSurface)s","%(subtype)s"},
124
            new String[]{String.valueOf(geometryType.getSubType()), String.valueOf(getGeometryType().getSubType())}
125
        );
126
        logger.warn(msg);
127
        throw new UnsupportedOperationException(msg);
128

    
129
    }
130

    
131
    /* (non-Javadoc)
132
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
133
     */
134
    public Shape getShape(AffineTransform affineTransform) {
135
        int capacity = 0;
136
        for( int i = 0; i < this.getPrimitivesNumber(); i++ ) {
137
            Polygon polygon = (Polygon) this.getPrimitiveAt(i);
138
            capacity += polygon.getNumVertices();
139
            for( int r = 0; r < polygon.getNumInteriorRings(); r++ ) {
140
                capacity += polygon.getInteriorRing(r).getNumVertices();
141
            }        
142
        }
143
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0, capacity);
144
    }
145

    
146
    /* (non-Javadoc)
147
     * @see org.gvsig.fmap.geom.Geometry#getShape()
148
     */
149
    public Shape getShape() {
150
        return getShape(null);
151
    }
152

    
153
    protected class MultiPolygonIterator extends GeneralPathXIterator {
154

    
155
        /** Transform applied on the coordinates during iteration */
156
        private AffineTransform at;
157

    
158
        /** True when the point has been read once */
159
        private boolean done;
160
        private int index = 0;
161
        private List<PathIterator>iterators = new ArrayList<PathIterator>(primitives.size());
162

    
163
        /**
164
         * Creates a new PointIterator object.
165
         *
166
         * @param p
167
         *            The polygon
168
         * @param at
169
         *            The affine transform applied to coordinates during
170
         *            iteration
171
         */
172
        public MultiPolygonIterator(AffineTransform at) {
173
            super(new GeneralPathX());
174
            if (at == null) {
175
                at = new AffineTransform();
176
            }
177

    
178
            this.at = at;
179
            for (Iterator iterator = primitives.iterator(); iterator.hasNext();) {
180
                Primitive primitive = (Primitive) iterator.next();
181
                iterators.add(primitive.getPathIterator(at));
182
            }
183
            done = false;
184
        }
185

    
186
        /**
187
         * Return the winding rule for determining the interior of the path.
188
         *
189
         * @return <code>WIND_EVEN_ODD</code> by default.
190
         */
191
        public int getWindingRule() {
192
            return PathIterator.WIND_EVEN_ODD;
193
        }
194

    
195
        /**
196
         * @see java.awt.geom.PathIterator#next()
197
         */
198
        public void next() {
199
            PathIterator pathIteratorPrimitive = iterators.get(index);
200
            pathIteratorPrimitive.next();
201
            if(pathIteratorPrimitive.isDone()){
202
                index++;
203
                done = (index==primitives.size());
204
            }
205
        }
206

    
207
        /**
208
         * @see java.awt.geom.PathIterator#isDone()
209
         */
210
        public boolean isDone() {
211
            return done;
212
        }
213

    
214
        /**
215
         * @see java.awt.geom.PathIterator#currentSegment(double[])
216
         */
217
        public int currentSegment(double[] coords) {
218
            return iterators.get(index).currentSegment(coords);
219
        }
220

    
221
        /*
222
         * (non-Javadoc)
223
         *
224
         * @see java.awt.geom.PathIterator#currentSegment(float[])
225
         */
226
        public int currentSegment(float[] coords) {
227
            return iterators.get(index).currentSegment(coords);
228
        }
229
    }
230

    
231
}