Revision 737 org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.base/src/main/java/org/gvsig/geoprocess/algorithm/base/core/ScalableUnionOperation.java

View differences:

ScalableUnionOperation.java
3 3
 *
4 4
 * Copyright (C) 2007-2012 gvSIG Association.
5 5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 2 of the License, or (at your option) any later
9
 * version.
10 10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.geoprocess.algorithm.base.core;
25 24

  
......
54 53

  
55 54
/**
56 55
 * Joins all geometries in a layer
56
 *
57 57
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
58 58
 */
59 59
public class ScalableUnionOperation extends GeometryOperation {
60

  
60 61
    private static final Logger LOG = LoggerFactory
61
        .getLogger(ScalableUnionOperation.class);
62
            .getLogger(ScalableUnionOperation.class);
62 63
	//geometry type of the layer whose features we are going to fussion
63
	//(polygon features are optimized in jts with buffer(0) trick, the
64
	//nor the rest
65
	private Geometry            geometry         = null;
64
    //(polygon features are optimized in jts with buffer(0) trick, the
65
    //nor the rest
66
    private Geometry geometry = null;
66 67

  
67
	public ScalableUnionOperation() {
68
		super(null);
69
	}
68
    public ScalableUnionOperation() {
69
        super(null);
70
    }
70 71

  
71
	public ScalableUnionOperation(AbstractSextanteGeoProcess p) {
72
		super(p);
73
	}
72
    public ScalableUnionOperation(AbstractSextanteGeoProcess p) {
73
        super(p);
74
    }
74 75

  
75
	/*
76
	 * (non-Javadoc)
77
	 * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
78
	 */
79
	public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
80
		if(g == null)
81
			return null;
76
    /*
77
     * (non-Javadoc)
78
     * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
79
     */
80
    public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
81
        if (g == null) {
82
            return null;
83
        }
82 84

  
83
		if(g.getGeometryType().getType() != TYPES.SURFACE)
84
			return null;
85
        if (g.getGeometryType().getType() != TYPES.SURFACE) {
86
            return null;
87
        }
85 88

  
86
		com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g);
89
        com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g);
87 90

  
88
		if(geometry == null){
89
			geometry = actualGeometry;
90
		}else{
91
			Geometry[] geoms = new Geometry[2];
92
			geoms[0] = geometry;
93
			geoms[1] = actualGeometry;
91
        if (geometry == null) {
92
            geometry = actualGeometry;
93
        } else {
94
            Geometry[] geoms = new Geometry[2];
95
            geoms[0] = geometry;
96
            geoms[1] = actualGeometry;
94 97

  
95
			geometry = JTSFacade.union(geoms, g.getType());
96
		}
97
		return null;
98
	}
98
            geometry = JTSFacade.union(geoms, g.getType());
99
        }
100
        return null;
101
    }
99 102

  
100
	/*
101
	 * (non-Javadoc)
102
	 * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
103
	 */
104
	public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) {
105
		invoke(g, featureInput);
106
	}
103
    /*
104
     * (non-Javadoc)
105
     * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
106
     */
107
    public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) {
108
        invoke(g, featureInput);
109
    }
107 110

  
108
	public void setFeatureStore(FeatureStore out, String[] attrNames)
109
			throws DataException {
110
	}
111
    public void setFeatureStore(FeatureStore out, String[] attrNames)
112
            throws DataException {
113
    }
111 114

  
112
	/*
113
	 * (non-Javadoc)
114
	 * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
115
	 */
116
	public Object getResult() {
117
		return getGeometry();
118
	}
115
    /*
116
     * (non-Javadoc)
117
     * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
118
     */
119
    public Object getResult() {
120
        return getGeometry();
121
    }
119 122

  
120
	/**
121
	 * Returns FMap convex hull geometry.
122
	 * @return
123
	 */
124
	private org.gvsig.fmap.geom.Geometry getGeometry() {
125
		return GeometryUtil.jtsToGeom(geometry);
126
	}
123
    /**
124
     * Returns FMap convex hull geometry.
125
     *
126
     * @return
127
     */
128
    private org.gvsig.fmap.geom.Geometry getGeometry() {
129
        return GeometryUtil.jtsToGeom(geometry);
130
    }
127 131

  
128
	/**
129
	 * Joins two geometries
130
	 * @param g
131
	 * @param geometryType
132
	 * @return
133
	 */
134
	private void union(org.gvsig.fmap.geom.Geometry g, int geometryType) {
135
	    Date time1 = new Date();
136
		if(g == null)
137
			return;
132
    /**
133
     * Joins two geometries
134
     *
135
     * @param g
136
     * @param geometryType
137
     * @return
138
     */
139
    private void union(org.gvsig.fmap.geom.Geometry g, int geometryType) {
140
        Date time1 = new Date();
141
        if (g == null) {
142
            return;
143
        }
138 144

  
139
		if(g.getGeometryType().getType() != TYPES.SURFACE)
140
			return;
145
        if (g.getGeometryType().getType() != TYPES.SURFACE) {
146
            return;
147
        }
141 148

  
142
		com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g);
149
        com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g);
143 150

  
144
		if(geometry == null){
145
			geometry = actualGeometry;
146
		}else{
147
			Geometry[] geoms = new Geometry[2];
148
			geoms[0] = geometry;
149
			geoms[1] = actualGeometry;
151
        if (geometry == null) {
152
            geometry = actualGeometry;
153
        } else {
154
            Geometry[] geoms = new Geometry[2];
155
            geoms[0] = geometry;
156
            geoms[1] = actualGeometry;
150 157

  
151
			geometry = JTSFacade.union(geoms, geometryType);
152
		}
158
            geometry = JTSFacade.union(geoms, geometryType);
159
        }
153 160
        Date time2 = new Date();
154
        LOG.info("scalableUnion time = "+(time2.getTime()-time1.getTime()+" ms"));
161
        LOG.info("scalableUnion time = " + (time2.getTime() - time1.getTime() + " ms"));
155 162

  
156
	}
163
    }
157 164

  
158
	   /**
165
    /**
159 166
     * Joins an array of surface geometries
167
     *
160 168
     * @param geoms
161 169
     * @param geometryType
162 170
     * @return
163 171
     */
164 172
    private void union(org.gvsig.fmap.geom.Geometry[] geoms) {
165 173
        Date time1 = new Date();
166
        if(geoms == null)
174
        if (geoms == null) {
167 175
            return;
176
        }
168 177

  
169 178
        ArrayList<Geometry> geometries = new ArrayList<Geometry>();
170 179

  
171
        if(geometry != null){
180
        if (geometry != null) {
172 181
            geometries.add(geometry);
173 182
        }
174 183
        for (int i = 0; i < geoms.length; i++) {
175
            if(geoms[i].getGeometryType().getType() == TYPES.SURFACE) {
184
            if (geoms[i].getGeometryType().getType() == TYPES.SURFACE) {
176 185
                geometries.add(GeometryUtil.geomToJTS(geoms[i]));
177 186
            }
178 187
        }
179
        geometry = JTSFacade.union((Geometry[])geometries.toArray(new Geometry[0]), TYPES.SURFACE);
188
        geometry = JTSFacade.union((Geometry[]) geometries.toArray(new Geometry[0]), TYPES.SURFACE);
180 189

  
181 190
        Date time2 = new Date();
182
        LOG.info("scalableUnion time = "+(time2.getTime()-time1.getTime()+" ms"));
191
        LOG.info("scalableUnion time = " + (time2.getTime() - time1.getTime() + " ms"));
183 192

  
184 193
    }
185 194

  
186
	/**
187
	 * Computes union of all geometries of the clipping layer. The input resources won't
188
	 * be released.
189
	 *
190
	 * @return one geometry
191
	 * @throws NullParameterValueException
192
	 * @throws WrongParameterIDException
193
	 * @throws WrongParameterTypeException
194
	 * @throws com.iver.cit.gvsig.fmap.DriverException
195
	 * @throws ReadDriverException
196
	 * @throws VisitorException
197
	 * @throws ExpansionFileReadException
198
	 */
199
	public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(IVectorLayer input, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
200
		FeatureStore store = null;
195
    /**
196
     * Computes union of all geometries of the clipping layer. The input
197
     * resources won't be released.
198
     *
199
     * @return one geometry
200
     * @throws NullParameterValueException
201
     * @throws WrongParameterIDException
202
     * @throws WrongParameterTypeException
203
     * @throws com.iver.cit.gvsig.fmap.DriverException
204
     * @throws ReadDriverException
205
     * @throws VisitorException
206
     * @throws ExpansionFileReadException
207
     */
208
    public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(IVectorLayer input, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
209
        FeatureStore store = null;
201 210

  
202
		if(input instanceof FlyrVectIVectorLayer)
203
			store = ((FlyrVectIVectorLayer)input).getFeatureStore();
204
		else
205
			return null;
211
        if (input instanceof FlyrVectIVectorLayer) {
212
            store = ((FlyrVectIVectorLayer) input).getFeatureStore();
213
        } else {
214
            return null;
215
        }
206 216

  
207
		return joinLayerGeometries(store, selectedGeom);
208
	}
217
        return joinLayerGeometries(store, selectedGeom);
218
    }
209 219

  
210
	/**
211
	 * Computes union of all geometries of the clipping layer. The input resources won't
212
	 * be released.
213
	 *
214
	 * @return one geometry
215
	 * @throws NullParameterValueException
216
	 * @throws WrongParameterIDException
217
	 * @throws WrongParameterTypeException
218
	 * @throws com.iver.cit.gvsig.fmap.DriverException
219
	 * @throws ReadDriverException
220
	 * @throws VisitorException
221
	 * @throws ExpansionFileReadException
222
	 */
223
	@SuppressWarnings({ "unchecked", "deprecation" })
224
	public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(FeatureStore store, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
225
		ScalableUnionOperation operation = new ScalableUnionOperation();
220
    /**
221
     * Computes union of all geometries of the clipping layer. The input
222
     * resources won't be released.
223
     *
224
     * @return one geometry
225
     * @throws NullParameterValueException
226
     * @throws WrongParameterIDException
227
     * @throws WrongParameterTypeException
228
     * @throws com.iver.cit.gvsig.fmap.DriverException
229
     * @throws ReadDriverException
230
     * @throws VisitorException
231
     * @throws ExpansionFileReadException
232
     */
233
    @SuppressWarnings({"unchecked", "deprecation"})
234
    public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(FeatureStore store, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
235
        ScalableUnionOperation operation = new ScalableUnionOperation();
226 236

  
227
		FeatureSet features = null;
228
		try {
229
			if(selectedGeom) {
230
				features = (FeatureSet)store.getSelection();
231
			} else {
232
				features = store.getFeatureSet();
233
			}
234
			DisposableIterator it = features.iterator();
235
			int i=0;
236
			List geometries = new ArrayList<org.gvsig.fmap.geom.Geometry>();
237
        FeatureSet features = null;
238
        try {
239
            if (selectedGeom) {
240
                features = (FeatureSet) store.getSelection();
241
            } else {
242
                features = store.getFeatureSet();
243
            }
244
            DisposableIterator it = features.iterator();
245
            int i = 0;
246
            List geometries = new ArrayList<org.gvsig.fmap.geom.Geometry>();
237 247
            List types = new ArrayList();
238
			while( it.hasNext() ) {
239
			    i++;
240
				Feature feature = (Feature)it.next();
241
				List geomList = feature.getGeometries();
242
				if(geomList == null) {
243
					org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
244
					if(geom != null){
245
						geometries.add(geom);
246
						types.add(geom.getType());
247
					}
248
					continue;
249
				}
250
				Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
251
				while(itGeom.hasNext()) {
252
					org.gvsig.fmap.geom.Geometry g = itGeom.next();
248
            while (it.hasNext()) {
249
                i++;
250
                Feature feature = (Feature) it.next();
251
                List geomList = feature.getGeometries();
252
                if (geomList == null) {
253
                    org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
254
                    if (geom != null) {
255
                        geometries.add(geom);
256
                        types.add(geom.getType());
257
                    }
258
                    continue;
259
                }
260
                Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
261
                while (itGeom.hasNext()) {
262
                    org.gvsig.fmap.geom.Geometry g = itGeom.next();
253 263
                    geometries.add(g);
254 264
                    types.add(g.getType());
255
				}
256
			}
257
			operation.union((org.gvsig.fmap.geom.Geometry[])geometries.toArray(new org.gvsig.fmap.geom.Geometry[0]));
258
			return operation.getGeometry();
259
		} catch (DataException e) {
260
			Sextante.addErrorToLog(e);
261
		}
262
		return null;
263
	}
265
                }
266
            }
267
            operation.union((org.gvsig.fmap.geom.Geometry[]) geometries.toArray(new org.gvsig.fmap.geom.Geometry[0]));
268
            return operation.getGeometry();
269
        } catch (DataException e) {
270
            Sextante.addErrorToLog(e);
271
        }
272
        return null;
273
    }
264 274
}
265

  

Also available in: Unified diff