Revision 1291 org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.intersection/src/main/java/org/gvsig/geoprocess/algorithm/intersection/IntersectionAlgorithm.java

View differences:

IntersectionAlgorithm.java
48 48
 * <LI>Line-Line: 2 layers (point, line)</LI>
49 49
 * <LI>Point-Point: 1 layer (point)</LI>
50 50
 * </UL>
51
 *
51 52
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
52 53
 */
53 54
public class IntersectionAlgorithm extends AbstractSextanteGeoProcess {
54 55

  
55
	public static final String  RESULT_POL            = "RESULT_POL";
56
	public static final String  RESULT_POINT          = "RESULT_POINT";
57
	public static final String  RESULT_LINE           = "RESULT_LINE";
58
	public static final String  LAYER                 = "LAYER";
59
	public static final String  INTER                 = "INTER";
60
	public static final String  SELECTGEOM_INPUT      = "SELECTGEOM_INPUT";
61
	public static final String  SELECTGEOM_OVERLAY    = "SELECTGEOM_OVERLAY";
56
    public static final String RESULT_POL = "RESULT_POL";
57
    public static final String RESULT_POINT = "RESULT_POINT";
58
    public static final String RESULT_LINE = "RESULT_LINE";
59
    public static final String LAYER = "LAYER";
60
    public static final String INTER = "INTER";
61
    public static final String SELECTGEOM_INPUT = "SELECTGEOM_INPUT";
62
    public static final String SELECTGEOM_OVERLAY = "SELECTGEOM_OVERLAY";
62 63

  
63 64

  
64
	/*
65
    /*
65 66
	 * (non-Javadoc)
66 67
	 * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
67
	 */
68
	public void defineCharacteristics() {
68
     */
69
    public void defineCharacteristics() {
69 70
        setName(getTranslation("Intersection"));
70 71
        setGroup(getTranslation("basic_vect_algorithms"));
71 72
        // setGeneratesUserDefinedRasterOutput(false);
72 73

  
73
		try {
74
			m_Parameters.addInputVectorLayer(LAYER,
75
                getTranslation("Input_layer"),
76
												IVectorLayer.SHAPE_TYPE_WRONG,
77
												true);
78
			m_Parameters.addInputVectorLayer(INTER,
79
                getTranslation("Overlays_layer"),
80
												IVectorLayer.SHAPE_TYPE_WRONG,
81
												true);
74
        try {
75
            m_Parameters.addInputVectorLayer(LAYER,
76
                    getTranslation("Input_layer"),
77
                    IVectorLayer.SHAPE_TYPE_WRONG,
78
                    true);
79
            m_Parameters.addInputVectorLayer(INTER,
80
                    getTranslation("Overlays_layer"),
81
                    IVectorLayer.SHAPE_TYPE_WRONG,
82
                    true);
82 83
            m_Parameters.addBoolean(SELECTGEOM_INPUT,
83
            		getTranslation("Selected_geometries_input_layer_inters"), false);
84
                    getTranslation("Selected_geometries_input_layer_inters"), false);
84 85
            m_Parameters.addBoolean(SELECTGEOM_OVERLAY,
85
            		getTranslation("Selected_geometries_overlay_layer_inters"), false);
86
		} catch (RepeatedParameterNameException e) {
87
			Sextante.addErrorToLog(e);
88
		}
89
		addOutputVectorLayer(RESULT_POL, getTranslation("Intersection_polygon"),
90
								OutputVectorLayer.SHAPE_TYPE_POLYGON);
91
		addOutputVectorLayer(RESULT_LINE, getTranslation("Intersection_line"),
92
								OutputVectorLayer.SHAPE_TYPE_LINE);
93
		addOutputVectorLayer(RESULT_POINT, getTranslation("Intersection_point"),
94
								OutputVectorLayer.SHAPE_TYPE_POINT);
95
	}
86
                    getTranslation("Selected_geometries_overlay_layer_inters"), false);
87
        } catch (RepeatedParameterNameException e) {
88
            Sextante.addErrorToLog(e);
89
        }
90
        addOutputVectorLayer(RESULT_POL, getTranslation("Intersection_polygon"),
91
                OutputVectorLayer.SHAPE_TYPE_POLYGON);
92
        addOutputVectorLayer(RESULT_LINE, getTranslation("Intersection_line"),
93
                OutputVectorLayer.SHAPE_TYPE_LINE);
94
        addOutputVectorLayer(RESULT_POINT, getTranslation("Intersection_point"),
95
                OutputVectorLayer.SHAPE_TYPE_POINT);
96
    }
96 97

  
97
	/*
98
    /*
98 99
	 * (non-Javadoc)
99 100
	 * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
100
	 */
101
	public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
102
		if(existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) {
103
    		throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
104
    	}
105
		if(existsOutPutFile(IntersectionAlgorithm.RESULT_POINT, 0)) {
106
    		throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
107
    	}
108
		if(existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) {
109
    		throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
110
    	}
111
		IVectorLayer inter = m_Parameters.getParameterValueAsVectorLayer(INTER);
112
		IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
113
		boolean selectedGeomInput = m_Parameters.getParameter(SELECTGEOM_INPUT).getParameterValueAsBoolean();
114
		boolean selectedGeomOverlay = m_Parameters.getParameter(SELECTGEOM_OVERLAY).getParameterValueAsBoolean();
115
		boolean error = false;
101
     */
102
    public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
103
        if (existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) {
104
            throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
105
        }
106
        if (existsOutPutFile(IntersectionAlgorithm.RESULT_POINT, 0)) {
107
            throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
108
        }
109
        if (existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) {
110
            throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
111
        }
112
        IVectorLayer inter = m_Parameters.getParameterValueAsVectorLayer(INTER);
113
        IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
114
        boolean selectedGeomInput = m_Parameters.getParameter(SELECTGEOM_INPUT).getParameterValueAsBoolean();
115
        boolean selectedGeomOverlay = m_Parameters.getParameter(SELECTGEOM_OVERLAY).getParameterValueAsBoolean();
116
        boolean error = false;
116 117

  
117
		try {
118
			error = computesIntersection(layer, inter, layer.getShapeType(), selectedGeomInput, selectedGeomOverlay);
119
		} catch (DataException e) {
120
			Sextante.addErrorToLog(e);
121
			return false;
122
		}
118
        try {
119
            error = computesIntersection(layer, inter, layer.getShapeType(), selectedGeomInput, selectedGeomOverlay);
120
        } catch (DataException e) {
121
            Sextante.addErrorToLog(e);
122
            return false;
123
        }
123 124

  
124
		if(getTaskMonitor().isCanceled())
125
			return false;
126
		if(error)
127
			JOptionPane.showMessageDialog(null,
128
	                getTranslation("problems_with_some_geometries"), "Error",
129
	                JOptionPane.WARNING_MESSAGE);
130
		return true;
131
	}
125
        if (getTaskMonitor().isCanceled()) {
126
            return false;
127
        }
128
        if (error) {
129
            JOptionPane.showMessageDialog(null,
130
                    getTranslation("problems_with_some_geometries"), "Error",
131
                    JOptionPane.WARNING_MESSAGE);
132
        }
133
        return true;
134
    }
132 135

  
133
	/**
134
	 * Builds a layer with the intersection between the input layer and the templateGeometry
135
	 * @param layer
136
	 *        Input layer
137
	 * @param templateGeometry
138
	 * @param shapeType
139
	 *        Output shape type
140
	 * @param selectedGeom
141
	 *        If it's true only selected geometries will be computed
142
	 * @throws GeoAlgorithmExecutionException
143
	 */
144
	private boolean computesIntersection(IVectorLayer layer,
145
								IVectorLayer overlay,
146
								int shapeType,
147
								boolean selectedGeomInput,
148
								boolean selectedGeomOverlay) throws DataException, GeoAlgorithmExecutionException {
149
		FeatureStore storeLayer = null;
150
		FeatureStore storeOverlay = null;
151
		if(layer instanceof FlyrVectIVectorLayer &&
152
			overlay instanceof FlyrVectIVectorLayer) {
153
			storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
154
			storeOverlay = ((FlyrVectIVectorLayer)overlay).getFeatureStore();
155
		} else
156
			return false;
136
    /**
137
     * Builds a layer with the intersection between the input layer and the
138
     * templateGeometry
139
     *
140
     * @param layer Input layer
141
     * @param templateGeometry
142
     * @param shapeType Output shape type
143
     * @param selectedGeom If it's true only selected geometries will be
144
     * computed
145
     * @throws GeoAlgorithmExecutionException
146
     */
147
    private boolean computesIntersection(IVectorLayer layer,
148
            IVectorLayer overlay,
149
            int shapeType,
150
            boolean selectedGeomInput,
151
            boolean selectedGeomOverlay) throws DataException, GeoAlgorithmExecutionException {
152
        FeatureStore storeLayer = null;
153
        FeatureStore storeOverlay = null;
154
        if (layer instanceof FlyrVectIVectorLayer
155
                && overlay instanceof FlyrVectIVectorLayer) {
156
            storeLayer = ((FlyrVectIVectorLayer) layer).getFeatureStore();
157
            storeOverlay = ((FlyrVectIVectorLayer) overlay).getFeatureStore();
158
        } else {
159
            return false;
160
        }
157 161

  
158
		FeatureType featureType1 = storeLayer.getDefaultFeatureType();
159
		FeatureType featureType2 = storeOverlay.getDefaultFeatureType();
162
        FeatureType featureType1 = storeLayer.getDefaultFeatureType();
163
        FeatureType featureType2 = storeOverlay.getDefaultFeatureType();
160 164

  
161
		FeatureStore outFeatStorePol = null;
162
		FeatureStore outFeatStoreLine = null;
163
		FeatureStore outFeatStorePoint = null;
165
        FeatureStore outFeatStorePol = null;
166
        FeatureStore outFeatStoreLine = null;
167
        FeatureStore outFeatStorePoint = null;
164 168

  
165
		IntersectionOperation operation = new IntersectionOperation(storeOverlay, this);
166
		operation.setTaskStatus(getStatus());
167
                this.namesTranslator = NamesTranslator.createTrimTranslator(11);
169
        IntersectionOperation operation = new IntersectionOperation(storeOverlay, this);
170
        operation.setTaskStatus(getStatus());
171
        this.namesTranslator = NamesTranslator.createTrimTranslator(11);
168 172

  
169
		//La de puntos se genera siempre
170
		outFeatStorePoint =
171
			buildOutPutStoreFromUnion(featureType1, featureType2,
172
					IVectorLayer.SHAPE_TYPE_POINT,
173
					getTranslation("Intersection_point"), RESULT_POINT);
173
        //La de puntos se genera siempre
174
        outFeatStorePoint
175
                = buildOutPutStoreFromUnion(featureType1, featureType2,
176
                        IVectorLayer.SHAPE_TYPE_POINT,
177
                        getTranslation("Intersection_point"), RESULT_POINT);
174 178

  
175 179
        if (outFeatStorePoint != null) {
176 180
            getStatus().setTitle("Point");
177 181
            operation.computesGeometryOperation(storeLayer, outFeatStorePoint,
178
                attrNames, selectedGeomInput, selectedGeomOverlay, true);
182
                    attrNames, selectedGeomInput, selectedGeomOverlay, true);
179 183
        }
180
		//La de pol?gonos solo si es intersecci?n entre pol?gonos
181
		if (isPolygon(storeLayer) && isPolygon(storeOverlay)) {
182
			outFeatStorePol =
183
              buildOutPutStoreFromUnion(featureType1, featureType2,
184
            		  IVectorLayer.SHAPE_TYPE_POLYGON, getTranslation("Intersection_polygon"), RESULT_POL);
184
        //La de pol?gonos solo si es intersecci?n entre pol?gonos
185
        if (isPolygon(storeLayer) && isPolygon(storeOverlay)) {
186
            outFeatStorePol
187
                    = buildOutPutStoreFromUnion(featureType1, featureType2,
188
                            IVectorLayer.SHAPE_TYPE_POLYGON, getTranslation("Intersection_polygon"), RESULT_POL);
185 189
            if (outFeatStorePol != null) {
186 190
                getStatus().setTitle("Polygon");
187 191
                operation.computesGeometryOperation(storeLayer,
188
                    outFeatStorePol, attrNames, selectedGeomInput,
189
                    selectedGeomOverlay, true);
192
                        outFeatStorePol, attrNames, selectedGeomInput,
193
                        selectedGeomOverlay, true);
190 194
            }
191
		}
195
        }
192 196

  
193
		//La capa de l?neas se genera cuando ning?na de las dos es de puntos
194
		if (!isPoint(storeLayer) && !isPoint(storeOverlay)) {
195
			outFeatStoreLine =
196
              buildOutPutStoreFromUnion(featureType1, featureType2,
197
            		  IVectorLayer.SHAPE_TYPE_LINE, getTranslation("Intersection_line"), RESULT_LINE);
197
        //La capa de l?neas se genera cuando ning?na de las dos es de puntos
198
        if (!isPoint(storeLayer) && !isPoint(storeOverlay)) {
199
            outFeatStoreLine
200
                    = buildOutPutStoreFromUnion(featureType1, featureType2,
201
                            IVectorLayer.SHAPE_TYPE_LINE, getTranslation("Intersection_line"), RESULT_LINE);
198 202
            if (outFeatStoreLine != null) {
199 203
                getStatus().setTitle("Line");
200 204
                operation.computesGeometryOperation(storeLayer,
201
                    outFeatStoreLine, attrNames, selectedGeomInput,
202
                    selectedGeomOverlay, true);
205
                        outFeatStoreLine, attrNames, selectedGeomInput,
206
                        selectedGeomOverlay, true);
203 207
            }
204
		}
208
        }
205 209

  
206
		if(outFeatStorePol == null) {
207
			getNewVectorLayer(RESULT_POL, getTranslation("Null_polygon"),
210
        if (outFeatStorePol == null) {
211
            getNewVectorLayer(RESULT_POL, getTranslation("Null_polygon"),
208 212
                    OutputVectorLayer.SHAPE_TYPE_POLYGON, new Class[]{Integer.class}, new String[]{""});
209
		}
213
        }
210 214

  
211
		if(outFeatStoreLine == null) {
212
			getNewVectorLayer(RESULT_LINE, getTranslation("Null_line"),
215
        if (outFeatStoreLine == null) {
216
            getNewVectorLayer(RESULT_LINE, getTranslation("Null_line"),
213 217
                    OutputVectorLayer.SHAPE_TYPE_LINE, new Class[]{Integer.class}, new String[]{""});
214
		}
218
        }
215 219

  
216
		if(outFeatStorePoint == null) {
217
			getNewVectorLayer(RESULT_POINT, getTranslation("Null_point"),
220
        if (outFeatStorePoint == null) {
221
            getNewVectorLayer(RESULT_POINT, getTranslation("Null_point"),
218 222
                    OutputVectorLayer.SHAPE_TYPE_POINT, new Class[]{Integer.class}, new String[]{""});
219
		}
223
        }
220 224

  
221
		return operation.getErrorInfo();
222
	}
225
        return operation.getErrorInfo();
226
    }
223 227

  
224 228
}

Also available in: Unified diff