Revision 1293

View differences:

org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.union/src/main/java/org/gvsig/geoprocess/algorithm/union/UnionAlgorithm.java
48 48

  
49 49
/**
50 50
 * Union algorithm
51
 *
51 52
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
52 53
 */
53 54
public class UnionAlgorithm extends AbstractSextanteGeoProcess {
54 55

  
55
	public static final String  RESULT1               = "RESULT1";
56
	public static final String  RESULT2               = "RESULT2";
57
	public static final String  LAYER1                = "LAYER1";
58
	public static final String  LAYER2                = "LAYER2";
59
	public static final String  SELECTGEOM_INPUT      = "SELECTGEOM_INPUT";
60
	public static final String  SELECTGEOM_OVERLAY    = "SELECTGEOM_OVERLAY";
56
    public static final String RESULT1 = "RESULT1";
57
    public static final String RESULT2 = "RESULT2";
58
    public static final String LAYER1 = "LAYER1";
59
    public static final String LAYER2 = "LAYER2";
60
    public static final String SELECTGEOM_INPUT = "SELECTGEOM_INPUT";
61
    public static final String SELECTGEOM_OVERLAY = "SELECTGEOM_OVERLAY";
61 62
    private NamesTranslator nameTranslator;
62
	
63
	/*
63

  
64
    /*
64 65
	 * (non-Javadoc)
65 66
	 * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
66
	 */
67
	public void defineCharacteristics() {
67
     */
68
    public void defineCharacteristics() {
68 69
        setName(getTranslation("Union"));
69 70
        setGroup(getTranslation("basic_vect_algorithms"));
70 71
        // setGeneratesUserDefinedRasterOutput(false);
71
		
72
		try {
73
			m_Parameters.addInputVectorLayer(LAYER1, 
74
                getTranslation("Input_layer") + " 1",
75
												IVectorLayer.SHAPE_TYPE_WRONG, 
76
												true);
77
			m_Parameters.addInputVectorLayer(LAYER2, 
78
                getTranslation("Input_layer") + " 2",
79
												IVectorLayer.SHAPE_TYPE_WRONG, 
80
												true);
81
            m_Parameters.addBoolean(SELECTGEOM_INPUT, 
82
            		getTranslation("Selected_geometries_input_layer_union"), false);
83
            m_Parameters.addBoolean(SELECTGEOM_OVERLAY, 
84
            		getTranslation("Selected_geometries_overlay_layer_union"), false);
85
		} catch (RepeatedParameterNameException e) {
86
			Sextante.addErrorToLog(e);
87
		}
88
		addOutputVectorLayer(RESULT1, getTranslation("Union_l1"),
89
								OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
90
		addOutputVectorLayer(RESULT2, getTranslation("Union_l2"),
91
								OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
92
	}
93
	
94
	/*
72

  
73
        try {
74
            m_Parameters.addInputVectorLayer(LAYER1,
75
                    getTranslation("Input_layer") + " 1",
76
                    IVectorLayer.SHAPE_TYPE_WRONG,
77
                    true);
78
            m_Parameters.addInputVectorLayer(LAYER2,
79
                    getTranslation("Input_layer") + " 2",
80
                    IVectorLayer.SHAPE_TYPE_WRONG,
81
                    true);
82
            m_Parameters.addBoolean(SELECTGEOM_INPUT,
83
                    getTranslation("Selected_geometries_input_layer_union"), false);
84
            m_Parameters.addBoolean(SELECTGEOM_OVERLAY,
85
                    getTranslation("Selected_geometries_overlay_layer_union"), false);
86
        } catch (RepeatedParameterNameException e) {
87
            Sextante.addErrorToLog(e);
88
        }
89
        addOutputVectorLayer(RESULT1, getTranslation("Union_l1"),
90
                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
91
        addOutputVectorLayer(RESULT2, getTranslation("Union_l2"),
92
                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
93
    }
94

  
95
    /*
95 96
	 * (non-Javadoc)
96 97
	 * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
97
	 */
98
	public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
99
		if(existsOutPutFile(UnionAlgorithm.RESULT1, 0)) {
100
    		throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
101
    	}
102
		if(existsOutPutFile(UnionAlgorithm.RESULT2, 0)) {
103
    		throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
104
    	}
105
		
106
		IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1);
107
		IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2);
108
		boolean selectedGeomInput = m_Parameters.getParameter(SELECTGEOM_INPUT).getParameterValueAsBoolean();
109
		boolean selectedGeomOverlay = m_Parameters.getParameter(SELECTGEOM_OVERLAY).getParameterValueAsBoolean();
110
                this.nameTranslator = NamesTranslator.createTrimTranslator(11);
111
		
112
		FeatureStore storeLayer1 = null;
113
		FeatureStore storeLayer2 = null;
114
		if(layer1 instanceof FlyrVectIVectorLayer && 
115
			layer2 instanceof FlyrVectIVectorLayer) {
116
			storeLayer1 = ((FlyrVectIVectorLayer)layer1).getFeatureStore();
117
			storeLayer2 = ((FlyrVectIVectorLayer)layer2).getFeatureStore();
118
		} else
119
			return false;
98
     */
99
    public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
100
        if (existsOutPutFile(UnionAlgorithm.RESULT1, 0)) {
101
            throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
102
        }
103
        if (existsOutPutFile(UnionAlgorithm.RESULT2, 0)) {
104
            throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
105
        }
120 106

  
121
		try {
122
			FeatureType featureType1 = storeLayer1.getDefaultFeatureType();
107
        IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1);
108
        IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2);
109
        boolean selectedGeomInput = m_Parameters.getParameter(SELECTGEOM_INPUT).getParameterValueAsBoolean();
110
        boolean selectedGeomOverlay = m_Parameters.getParameter(SELECTGEOM_OVERLAY).getParameterValueAsBoolean();
111
        this.nameTranslator = NamesTranslator.createTrimTranslator(11);
123 112

  
124
			FeatureSet features2 = null;
125
			features2 = storeLayer2.getFeatureSet();
126
			FeatureType featureType2 = features2.getDefaultFeatureType();
113
        FeatureStore storeLayer1 = null;
114
        FeatureStore storeLayer2 = null;
115
        if (layer1 instanceof FlyrVectIVectorLayer
116
                && layer2 instanceof FlyrVectIVectorLayer) {
117
            storeLayer1 = ((FlyrVectIVectorLayer) layer1).getFeatureStore();
118
            storeLayer2 = ((FlyrVectIVectorLayer) layer2).getFeatureStore();
119
        } else {
120
            return false;
121
        }
127 122

  
128
			FeatureStore featStoreOut1 = null;
129
			FeatureStore featStoreOut2 = null;
130
			
131
			//POL-POL
132
			if(isPolygon(storeLayer1) && isPolygon(storeLayer2)) { 
133
				featStoreOut1 = buildOutPutStoreFromUnion(featureType1, 
134
						featureType2, 
135
						layer1.getShapeType(), 
136
						getTranslation("Union_polygon"),
123
        try {
124
            FeatureType featureType1 = storeLayer1.getDefaultFeatureType();
125

  
126
            FeatureSet features2 = null;
127
            features2 = storeLayer2.getFeatureSet();
128
            FeatureType featureType2 = features2.getDefaultFeatureType();
129

  
130
            FeatureStore featStoreOut1 = null;
131
            FeatureStore featStoreOut2 = null;
132

  
133
            //POL-POL
134
            if (isPolygon(storeLayer1) && isPolygon(storeLayer2)) {
135
                featStoreOut1 = buildOutPutStoreFromUnion(featureType1,
136
                        featureType2,
137
                        layer1.getShapeType(),
138
                        getTranslation("Union_polygon"),
137 139
                        RESULT1);
138
				computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
139
			} else
140
			//PTO-PTO
141
			if(isPoint(storeLayer1) && isPoint(storeLayer2)) { 
142
				featStoreOut1 = buildOutPutStoreFromUnion(featureType1, 
143
						featureType2, 
144
						layer1.getShapeType(), 
145
						getTranslation("Union_point"),
140
                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
141
            } else //PTO-PTO
142
            if (isPoint(storeLayer1) && isPoint(storeLayer2)) {
143
                featStoreOut1 = buildOutPutStoreFromUnion(featureType1,
144
                        featureType2,
145
                        layer1.getShapeType(),
146
                        getTranslation("Union_point"),
146 147
                        RESULT1);
147
				computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
148
			} else
149
			//LIN-LIN	
150
			if(isLine(storeLayer1) && isLine(storeLayer2)) {
151
				featStoreOut1 = buildOutPutStoreFromUnion(featureType1, 
152
						featureType2, 
153
						IVectorLayer.SHAPE_TYPE_POINT, 
154
                        getTranslation("Union_point"), 
148
                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
149
            } else //LIN-LIN	
150
            if (isLine(storeLayer1) && isLine(storeLayer2)) {
151
                featStoreOut1 = buildOutPutStoreFromUnion(featureType1,
152
                        featureType2,
153
                        IVectorLayer.SHAPE_TYPE_POINT,
154
                        getTranslation("Union_point"),
155 155
                        RESULT1);
156
				computesIntersection(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay, true);
157
				featStoreOut2 = buildOutPutStoreFromUnion(featureType1, 
158
						featureType2, 
159
						IVectorLayer.SHAPE_TYPE_LINE, 
160
                        getTranslation("Union_line"), 
156
                computesIntersection(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay, true);
157
                featStoreOut2 = buildOutPutStoreFromUnion(featureType1,
158
                        featureType2,
159
                        IVectorLayer.SHAPE_TYPE_LINE,
160
                        getTranslation("Union_line"),
161 161
                        RESULT2);
162
				computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay, false);
163
				computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeomInput, selectedGeomOverlay, true);
164
			} else
165
			//POL-LIN
166
			if( (isPolygon(storeLayer1) && isLine(storeLayer2)) || 
167
				(isLine(storeLayer1) && isPolygon(storeLayer2))) { 
168
				featStoreOut1 = buildOutPutStoreFromUnion(featureType1, 
169
						featureType2, 
170
						IVectorLayer.SHAPE_TYPE_LINE, 
162
                computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay, false);
163
                computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeomInput, selectedGeomOverlay, true);
164
            } else //POL-LIN
165
            if ((isPolygon(storeLayer1) && isLine(storeLayer2))
166
                    || (isLine(storeLayer1) && isPolygon(storeLayer2))) {
167
                featStoreOut1 = buildOutPutStoreFromUnion(featureType1,
168
                        featureType2,
169
                        IVectorLayer.SHAPE_TYPE_LINE,
171 170
                        getTranslation("Union_line"), RESULT1);
172
				computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
173
				featStoreOut2 = buildOutPutStoreFromUnion(featureType1, 
174
						featureType2, 
175
						IVectorLayer.SHAPE_TYPE_POLYGON, 
176
                        getTranslation("Union_polygon"), 
171
                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
172
                featStoreOut2 = buildOutPutStoreFromUnion(featureType1,
173
                        featureType2,
174
                        IVectorLayer.SHAPE_TYPE_POLYGON,
175
                        getTranslation("Union_polygon"),
177 176
                        RESULT2);
178
				computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay, false);
179
				computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeomInput, selectedGeomOverlay, true);
180
			} else
181
			//POL-PTO
182
			if( (isPolygon(storeLayer1) && isPoint(storeLayer2)) || 
183
				(isPoint(storeLayer1) && isPolygon(storeLayer2))) { 
184
				featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT, 
177
                computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay, false);
178
                computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeomInput, selectedGeomOverlay, true);
179
            } else //POL-PTO
180
            if ((isPolygon(storeLayer1) && isPoint(storeLayer2))
181
                    || (isPoint(storeLayer1) && isPolygon(storeLayer2))) {
182
                featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT,
185 183
                        getTranslation("Union_point"), RESULT1);
186
				computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
187
				featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POLYGON, 
184
                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
185
                featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_MULTIPOLYGON,
188 186
                        getTranslation("Union_polygon"), RESULT2);
189
				computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay);
190
			} else
191
			//PTO-LIN
192
			if( (isPoint(storeLayer1) && isLine(storeLayer2)) || 
193
				(isLine(storeLayer1) && isPoint(storeLayer2))) { 
194
				featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT, 
187
                computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay);
188
            } else //PTO-LIN
189
            if ((isPoint(storeLayer1) && isLine(storeLayer2))
190
                    || (isLine(storeLayer1) && isPoint(storeLayer2))) {
191
                featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT,
195 192
                        getTranslation("Union_point"), RESULT1);
196
				computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
197
				featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_LINE, 
193
                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
194
                featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_MULTILINE,
198 195
                        getTranslation("Union_line"), RESULT2);
199
				computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay);
200
			}
201
			
202
			if(featStoreOut2 == null) {
203
				featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_WRONG, 
196
                computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay);
197
            }
198

  
199
            if (featStoreOut2 == null) {
200
                featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_WRONG,
204 201
                        getTranslation("Null_Layer"), RESULT2);
205
			}
206
			
207
		} catch (ReadException e) {
208
			throw new GeoAlgorithmExecutionException(e.getMessage());
209
		} catch (DataException e) {
210
			throw new GeoAlgorithmExecutionException(e.getMessage());
211
		}
212
		
213
		if(getTaskMonitor().isCanceled())
214
			return false;
215
		
216
		return true;
217
	}
218
	
219
	/**
220
	 * Computes union
221
	 * @param storeLayer1
222
	 * @param storeLayer2
223
	 * @param featStoreOut
224
	 * @param selectedGeom
225
	 * @throws DataException
226
	 * @throws WrongParameterTypeException
227
	 * @throws WrongParameterIDException
228
	 * @throws NullParameterValueException
229
	 */
230
	private void computesUnion(FeatureStore storeLayer1, FeatureStore storeLayer2, 
231
			FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
232
		computesIntersection(storeLayer1, storeLayer2, featStoreOut, selectedGeomInput, selectGeomOverlay, false);
233
		computesDifference(storeLayer1, storeLayer2, featStoreOut, selectedGeomInput, selectGeomOverlay, false);
234
		computesDifference(storeLayer2, storeLayer1, featStoreOut, selectedGeomInput, selectGeomOverlay, true);
235
	}
236
	
237
	/**
238
	 * Computes intersection
239
	 * @param storeLayer1
240
	 * @param storeLayer2
241
	 * @param featStoreOut
242
	 * @param selectedGeom
243
	 * @param close
244
	 * @throws DataException
245
	 * @throws WrongParameterTypeException
246
	 * @throws WrongParameterIDException
247
	 * @throws NullParameterValueException
248
	 */
249
	private void computesIntersection(FeatureStore storeLayer1, FeatureStore storeLayer2, 
250
			FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay, boolean close) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
251
		GeometryOperation intersection = new IntersectionOperation(storeLayer2, this);
202
            }
203

  
204
        } catch (ReadException e) {
205
            throw new GeoAlgorithmExecutionException(e.getMessage());
206
        } catch (DataException e) {
207
            throw new GeoAlgorithmExecutionException(e.getMessage());
208
        }
209

  
210
        if (getTaskMonitor().isCanceled()) {
211
            return false;
212
        }
213

  
214
        return true;
215
    }
216

  
217
    /**
218
     * Computes union
219
     *
220
     * @param storeLayer1
221
     * @param storeLayer2
222
     * @param featStoreOut
223
     * @param selectedGeom
224
     * @throws DataException
225
     * @throws WrongParameterTypeException
226
     * @throws WrongParameterIDException
227
     * @throws NullParameterValueException
228
     */
229
    private void computesUnion(FeatureStore storeLayer1, FeatureStore storeLayer2,
230
            FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
231
        computesIntersection(storeLayer1, storeLayer2, featStoreOut, selectedGeomInput, selectGeomOverlay, false);
232
        computesDifference(storeLayer1, storeLayer2, featStoreOut, selectedGeomInput, selectGeomOverlay, false);
233
        computesDifference(storeLayer2, storeLayer1, featStoreOut, selectedGeomInput, selectGeomOverlay, true);
234
    }
235

  
236
    /**
237
     * Computes intersection
238
     *
239
     * @param storeLayer1
240
     * @param storeLayer2
241
     * @param featStoreOut
242
     * @param selectedGeom
243
     * @param close
244
     * @throws DataException
245
     * @throws WrongParameterTypeException
246
     * @throws WrongParameterIDException
247
     * @throws NullParameterValueException
248
     */
249
    private void computesIntersection(FeatureStore storeLayer1, FeatureStore storeLayer2,
250
            FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay, boolean close) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
251
        GeometryOperation intersection = new IntersectionOperation(storeLayer2, this);
252 252
        intersection.setTaskStatus(getStatus());
253
		intersection.computesGeometryOperation(storeLayer1, featStoreOut, attrNames, 
254
				selectedGeomInput, selectGeomOverlay, close);
255
	}
256
	
257
	/**
258
	 * Computes difference
259
	 * @param storeLayer1
260
	 * @param storeLayer2
261
	 * @param featStoreOut
262
	 * @param selectedGeom
263
	 * @param close
264
	 * @throws DataException
265
	 * @throws WrongParameterTypeException
266
	 * @throws WrongParameterIDException
267
	 * @throws NullParameterValueException
268
	 */
269
	private void computesDifference(FeatureStore storeLayer1, FeatureStore storeLayer2, 
270
			FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay, boolean close) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {		
271
		Geometry unionGeom1 = ScalableUnionOperation.joinLayerGeometries(storeLayer2, selectGeomOverlay);
272
		GeometryOperation differenceL1_L2 = new DifferenceOperation(unionGeom1, this);
253
        intersection.computesGeometryOperation(storeLayer1, featStoreOut, attrNames,
254
                selectedGeomInput, selectGeomOverlay, close);
255
    }
256

  
257
    /**
258
     * Computes difference
259
     *
260
     * @param storeLayer1
261
     * @param storeLayer2
262
     * @param featStoreOut
263
     * @param selectedGeom
264
     * @param close
265
     * @throws DataException
266
     * @throws WrongParameterTypeException
267
     * @throws WrongParameterIDException
268
     * @throws NullParameterValueException
269
     */
270
    private void computesDifference(FeatureStore storeLayer1, FeatureStore storeLayer2,
271
            FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay, boolean close) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
272
        Geometry unionGeom1 = ScalableUnionOperation.joinLayerGeometries(storeLayer2, selectGeomOverlay);
273
        GeometryOperation differenceL1_L2 = new DifferenceOperation(unionGeom1, this);
273 274
        differenceL1_L2.setTaskStatus(getStatus());
274
		differenceL1_L2.computesGeometryOperation(storeLayer1, featStoreOut, attrNames, 
275
				selectedGeomInput, selectGeomOverlay, close);
276
	}
277
	
275
        differenceL1_L2.computesGeometryOperation(storeLayer1, featStoreOut, attrNames,
276
                selectedGeomInput, selectGeomOverlay, close);
277
    }
278

  
278 279
}

Also available in: Unified diff