Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.base / src / main / java / org / gvsig / sextante / app / algorithm / base / core / ScalableUnionOperation.java @ 30

History | View | Annotate | Download (5.4 KB)

1
/*
2
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2010 Generalitat Valenciana.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 */
21
package org.gvsig.sextante.app.algorithm.base.core;
22

    
23
import java.util.Iterator;
24
import java.util.List;
25

    
26
import org.gvsig.fmap.dal.exception.DataException;
27
import org.gvsig.fmap.dal.feature.DisposableIterator;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FeatureSet;
30
import org.gvsig.fmap.dal.feature.FeatureStore;
31
import org.gvsig.fmap.geom.GeometryLocator;
32
import org.gvsig.fmap.geom.GeometryManager;
33
import org.gvsig.fmap.geom.Geometry.TYPES;
34
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
35
import org.gvsig.fmap.geom.operation.GeometryOperationException;
36
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
37
import org.gvsig.fmap.geom.operation.fromjts.FromJTS;
38
import org.gvsig.fmap.geom.operation.tojts.ToJTS;
39
import org.gvsig.sextante.app.algorithm.base.util.JTSFacade;
40
import org.gvsig.sextante.app.algorithm.base.visitor.exception.VisitorException;
41
import org.gvsig.sextante.app.extension.core.gvVectorLayer;
42

    
43
import com.vividsolutions.jts.geom.Geometry;
44

    
45
import es.unex.sextante.core.Sextante;
46
import es.unex.sextante.dataObjects.IVectorLayer;
47
import es.unex.sextante.exceptions.NullParameterValueException;
48
import es.unex.sextante.exceptions.WrongParameterIDException;
49
import es.unex.sextante.exceptions.WrongParameterTypeException;
50

    
51
/**
52
 * Union operation between two geometries.
53
 * @author Nacho Brodin (nachobrodin@gmail.com)
54
 */
55
public class ScalableUnionOperation {
56
        //geometry type of the layer whose features we are going to fussion
57
        //(polygon features are optimized in jts with buffer(0) trick, the
58
        //nor the rest
59
        private Geometry            geometry         = null;
60
        private GeometryManager     geometryManager  = null;
61
        
62
        public ScalableUnionOperation() {
63
                geometryManager = GeometryLocator.getGeometryManager();
64
        }
65
        
66
        /**
67
         * Returns FMap convex hull geometry.
68
         * @return
69
         */
70
        public org.gvsig.fmap.geom.Geometry getGeometry() {
71
                GeometryOperationContext ctx = new GeometryOperationContext();
72
                ctx.setAttribute(FromJTS.PARAM, geometry);
73
                try {
74
                        return (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx);
75
                } catch (GeometryOperationNotSupportedException e) {
76
                        Sextante.addErrorToLog(e);
77
                        return null;
78
                } catch (GeometryOperationException e) {
79
                        Sextante.addErrorToLog(e);
80
                        return null;
81
                }
82
        }
83
        
84
        public org.gvsig.fmap.geom.Geometry invoke(org.gvsig.fmap.geom.Geometry g, int geometryType) {
85
                if(g == null)
86
                        return null;
87
                
88
                if(g.getGeometryType().getType() != TYPES.SURFACE)
89
                        return null;
90
                
91
                com.vividsolutions.jts.geom.Geometry actualGeometry = null;
92
                try {
93
                        actualGeometry = (Geometry)g.invokeOperation(ToJTS.CODE, null);
94
                } catch (GeometryOperationNotSupportedException e1) {
95
                        Sextante.addErrorToLog(e1);
96
                } catch (GeometryOperationException e1) {
97
                        Sextante.addErrorToLog(e1);
98
                }
99
                
100
                if(geometry == null){
101
                        geometry = actualGeometry;
102
                }else{
103
                        Geometry[] geoms = new Geometry[2];
104
                        geoms[0] = geometry;
105
                        geoms[1] = actualGeometry;
106
                        
107
                        geometry = JTSFacade.union(geoms, geometryType);
108
                }
109
                return getGeometry();
110
        }
111
        
112
        /**
113
         * Computes union of all geometries of the clipping layer. The input resources won't 
114
         * be released.
115
         *
116
         * @return one geometry
117
         * @throws NullParameterValueException 
118
         * @throws WrongParameterIDException 
119
         * @throws WrongParameterTypeException 
120
         * @throws com.iver.cit.gvsig.fmap.DriverException
121
         * @throws ReadDriverException
122
         * @throws VisitorException
123
         * @throws ExpansionFileReadException
124
         */
125
        @SuppressWarnings("unchecked")
126
        public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(IVectorLayer input) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
127
                ScalableUnionOperation operation = new ScalableUnionOperation();
128
                
129
                FeatureStore store = null;
130
                
131
                if(input instanceof gvVectorLayer)
132
                        store = ((gvVectorLayer)input).getFeatureStore();
133
                else
134
                        return null;
135
                
136
                FeatureSet features = null;
137
                try {
138
                        features = store.getFeatureSet();
139
                        DisposableIterator it = features.iterator();
140
                        while( it.hasNext() ) {
141
                                Feature feature = (Feature)it.next();
142
                                List geomList = feature.getGeometries();
143
                                if(geomList == null) {
144
                                        org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
145
                                        if(geom != null)
146
                                                operation.invoke(geom, geom.getType());
147
                                        continue;
148
                                }
149
                                Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
150
                                while(itGeom.hasNext()) {
151
                                        org.gvsig.fmap.geom.Geometry g = itGeom.next();
152
                                        operation.invoke(g, g.getType());
153
                                }
154
                        }
155
                        return operation.getGeometry();
156
                } catch (DataException e) {
157
                        Sextante.addErrorToLog(e);
158
                }
159
                return null;
160
        }
161
}
162