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

History | View | Annotate | Download (7.3 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.Iterator;
30
import java.util.List;
31

    
32
import org.apache.commons.lang3.StringUtils;
33

    
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryException;
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.Primitive;
45
import org.gvsig.fmap.geom.primitive.Surface;
46
import org.gvsig.fmap.geom.type.GeometryType;
47

    
48

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

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

    
60
    /**
61
     * @param type
62
     */
63
    public AbstractMultiPolygon(int subtype) {
64
        super(Geometry.TYPES.MULTIPOLYGON, subtype);
65
    }
66

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

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

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

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

    
100

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

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

    
127
    }
128

    
129
    /* (non-Javadoc)
130
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
131
     */
132
    public Shape getShape(AffineTransform affineTransform) {
133
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
134
    }
135

    
136
    /* (non-Javadoc)
137
     * @see org.gvsig.fmap.geom.Geometry#getShape()
138
     */
139
    public Shape getShape() {
140
        return getShape(null);
141
    }
142

    
143
    protected class MultiPolygonIterator extends GeneralPathXIterator {
144

    
145
        /** Transform applied on the coordinates during iteration */
146
        private AffineTransform at;
147

    
148
        /** True when the point has been read once */
149
        private boolean done;
150
        private int index = 0;
151
        private List<PathIterator>iterators = new ArrayList<PathIterator>(primitives.size());
152

    
153
        /**
154
         * Creates a new PointIterator object.
155
         *
156
         * @param p
157
         *            The polygon
158
         * @param at
159
         *            The affine transform applied to coordinates during
160
         *            iteration
161
         */
162
        public MultiPolygonIterator(AffineTransform at) {
163
            super(new GeneralPathX());
164
            if (at == null) {
165
                at = new AffineTransform();
166
            }
167

    
168
            this.at = at;
169
            for (Iterator iterator = primitives.iterator(); iterator.hasNext();) {
170
                Primitive primitive = (Primitive) iterator.next();
171
                iterators.add(primitive.getPathIterator(at));
172
            }
173
            done = false;
174
        }
175

    
176
        /**
177
         * Return the winding rule for determining the interior of the path.
178
         *
179
         * @return <code>WIND_EVEN_ODD</code> by default.
180
         */
181
        public int getWindingRule() {
182
            return PathIterator.WIND_EVEN_ODD;
183
        }
184

    
185
        /**
186
         * @see java.awt.geom.PathIterator#next()
187
         */
188
        public void next() {
189
            PathIterator pathIteratorPrimitive = iterators.get(index);
190
            pathIteratorPrimitive.next();
191
            if(pathIteratorPrimitive.isDone()){
192
                index++;
193
                done = (index==primitives.size());
194
            }
195
        }
196

    
197
        /**
198
         * @see java.awt.geom.PathIterator#isDone()
199
         */
200
        public boolean isDone() {
201
            return done;
202
        }
203

    
204
        /**
205
         * @see java.awt.geom.PathIterator#currentSegment(double[])
206
         */
207
        public int currentSegment(double[] coords) {
208
            return iterators.get(index).currentSegment(coords);
209
        }
210

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

    
221
}