Revision 47362 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/fmap/dal/swing/impl/searchPostProcess/distinctOn/STUnionAggregateOperation.java

View differences:

STUnionAggregateOperation.java
6 6
package org.gvsig.fmap.dal.swing.impl.searchPostProcess.distinctOn;
7 7

  
8 8
//    public static void selfRegister(){
9

  
9
import java.util.Iterator;
10 10
import java.util.List;
11
import org.gvsig.fmap.dal.DataTypes;
12 11
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
13 12
import org.gvsig.fmap.geom.Geometry;
13
import static org.gvsig.fmap.geom.Geometry.TYPES.COMPLEX;
14
import static org.gvsig.fmap.geom.Geometry.TYPES.CURVE;
15
import static org.gvsig.fmap.geom.Geometry.TYPES.GEOMETRY;
16
import static org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE;
17
import static org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT;
18
import static org.gvsig.fmap.geom.Geometry.TYPES.MULTISURFACE;
19
import static org.gvsig.fmap.geom.Geometry.TYPES.POINT;
20
import static org.gvsig.fmap.geom.Geometry.TYPES.SURFACE;
21
import org.gvsig.fmap.geom.GeometryLocator;
22
import org.gvsig.fmap.geom.GeometryManager;
23
import org.gvsig.fmap.geom.aggregate.MultiCurve;
24
import org.gvsig.fmap.geom.aggregate.MultiPoint;
25
import org.gvsig.fmap.geom.aggregate.MultiSurface;
26
import org.gvsig.fmap.geom.complex.Complex;
27
import org.gvsig.fmap.geom.type.GeometryType;
14 28
import static org.gvsig.tools.dynobject.DynField.RELATION_TYPE_NONE;
15 29

  
30
public class STUnionAggregateOperation extends AbstractAggregateOperation {
16 31

  
17
 public class STUnionAggregateOperation extends AbstractAggregateOperation {
18
    
19
    public static class SumAggregateOperationFactory implements AggregateOperationFactory{
32
    public static class STUnionAggregateOperationFactory implements AggregateOperationFactory {
20 33

  
21
        public static final String NAME = "ST_Union";
34
        public static final String NAME = "Combine";
22 35

  
23 36
        @Override
24 37
        public String getName() {
......
34 47
        public boolean isApplicable(Object... value) {
35 48
            return Geometry.class.isAssignableFrom((Class<?>) value[0]);
36 49
        }
37
        
50

  
38 51
    }
39
    
40
    Geometry geom;
41 52

  
53
    private static final int HAS_POINT = 1;
54
    private static final int HAS_CURVE = 2;
55
    private static final int HAS_SURFACE = 4;
56

  
57
    MultiPoint multiPoint;
58
    MultiCurve multiCurve;
59
    MultiSurface multiSurface;
60
    int resultType;
61
    int resultSubType;
62
    GeometryManager geometryManager;
63

  
42 64
    public STUnionAggregateOperation() {
43
        this.geom = null;
65
        this.multiPoint = null;
66
        this.multiCurve = null;
67
        this.multiSurface = null;
68
        this.resultType = 0;
69
        this.resultSubType = Geometry.SUBTYPES.UNKNOWN;
70
        geometryManager = GeometryLocator.getGeometryManager();
44 71
    }
45 72

  
46 73
    @Override
......
50 77

  
51 78
    @Override
52 79
    public void reset() {
53
        this.geom = null;
80
        this.multiPoint = null;
81
        this.multiCurve = null;
82
        this.multiSurface = null;
83
        this.resultType = 0;
84
        this.resultSubType = Geometry.SUBTYPES.UNKNOWN;
54 85
    }
55 86

  
56 87
    @Override
......
59 90
            if (value == null) {
60 91
                return;
61 92
            }
62
            if( geom == null ) {
63
                geom = (Geometry)value;
64
                return;
93

  
94
            Geometry geom = (Geometry) value;
95
            GeometryType type = geom.getGeometryType();
96

  
97
            if (multiPoint == null) {
98
                this.resultSubType = type.getSubType();
99
                multiPoint = geometryManager.createMultiPoint(resultSubType);
100
                multiCurve = geometryManager.createMultiCurve(resultSubType);
101
                multiSurface = geometryManager.createMultiSurface(resultSubType);
65 102
            }
66
            geom = geom.union((Geometry) value);
103

  
104
            if (type.isTypeOf(POINT)
105
                    || type.isTypeOf(MULTIPOINT)) {
106
                multiPoint.addPrimitives(geom);
107
                resultType |= HAS_POINT;
108
            } else if (type.isTypeOf(CURVE)
109
                    || type.isTypeOf(MULTICURVE)) {
110
                multiCurve.addPrimitives(geom);
111
                resultType |= HAS_CURVE;
112
            } else if (type.isTypeOf(SURFACE)
113
                    || type.isTypeOf(MULTISURFACE)) {
114
                multiSurface.addPrimitives(geom);
115
                resultType |= HAS_SURFACE;
116
            } else if (type.isTypeOf(COMPLEX)) {
117
                Complex complex = (Complex) value;
118
                Iterator<Geometry> complexIt = complex.iterator();
119
                while (complexIt.hasNext()) {
120
                    Geometry geom1 = complexIt.next();
121
                    if (geom1.getGeometryType().isTypeOf(POINT)
122
                            || geom1.getGeometryType().isTypeOf(MULTIPOINT)) {
123
                        multiPoint.addPrimitives(geom1);
124
                        resultType |= HAS_POINT;
125
                    } else if (geom1.getGeometryType().isTypeOf(CURVE)
126
                            || geom1.getGeometryType().isTypeOf(MULTICURVE)) {
127
                        multiCurve.addPrimitives(geom1);
128
                        resultType |= HAS_CURVE;
129

  
130
                    } else if (geom1.getGeometryType().isTypeOf(SURFACE)
131
                            || geom1.getGeometryType().isTypeOf(MULTISURFACE)) {
132
                        multiSurface.addPrimitives(geom1);
133
                        resultType |= HAS_SURFACE;
134
                    }
135
                }
136
            }
137

  
67 138
        } catch (Exception ex) {
68
            throw new RuntimeException("",ex);
139
            throw new RuntimeException("", ex);
69 140
        }
70 141
    }
71 142

  
72 143
    @Override
73 144
    public Object getValue() {
74
        return this.geom;
145
        try {
146

  
147
            if (resultType == HAS_POINT) {
148
                return multiPoint.union();
149
            }
150
            if (resultType == HAS_CURVE) {
151
                return multiCurve.dissolve();
152
            }
153
            if (resultType == HAS_SURFACE) {
154
                return multiSurface.union();
155
            }
156
            Geometry combinedGeometry = geometryManager.create(COMPLEX, this.resultSubType);
157
            Complex complex = (Complex) combinedGeometry;
158
            if ((resultType & HAS_POINT) == HAS_POINT) {
159
                complex.addPrimitives(multiPoint.union());
160
            }
161
            if ((resultType & HAS_CURVE) == HAS_CURVE) {
162
                complex.addPrimitives(multiCurve.dissolve());
163
            }
164
            if ((resultType & HAS_SURFACE) == HAS_SURFACE) {
165
                complex.addPrimitives(multiSurface.union());
166
            }
167
            return complex;
168
        } catch (Exception ex) {
169
            throw new RuntimeException("", ex);
170
        }
171

  
75 172
    }
76 173

  
77 174
    @Override
78 175
    public void fixAttributeDescriptor(EditableFeatureAttributeDescriptor descriptor) {
79 176
        super.fixAttributeDescriptor(descriptor);
80
        
177

  
81 178
//        descriptor.setDataType(DataTypes.GEOMETRY);
82 179
//        descriptor.setGeometryType(Geometry.TYPES.POLYGON, Geometry.SUBTYPES.GEOM2D);
83

  
84 180
        descriptor.setRelationType(RELATION_TYPE_NONE);
85 181
        descriptor.getForeingKey().clean();
86
        descriptor.setAvailableValues((List)null);
182
        descriptor.setAvailableValues((List) null);
87 183
    }
88
    
89
    
184

  
90 185
}

Also available in: Unified diff