Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extTopology / src / com / iver / cit / gvsig / cad / SmoothPolylinesExtension.java @ 21266

History | View | Annotate | Download (5.96 KB)

1
/*
2
 * Created on 10-abr-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id: 
47
* $Log: 
48
*/
49
package com.iver.cit.gvsig.cad;
50

    
51
import org.gvsig.fmap.core.FGeometryUtil;
52
import org.gvsig.jts.JtsUtil;
53
import org.gvsig.topology.ui.util.GUIUtil;
54

    
55
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
56
import com.iver.andami.PluginServices;
57
import com.iver.andami.messages.NotificationManager;
58
import com.iver.andami.plugins.Extension;
59
import com.iver.cit.gvsig.CADExtension;
60
import com.iver.cit.gvsig.EditionUtilities;
61
import com.iver.cit.gvsig.drivers.featureiterators.FeatureBitsetIterator;
62
import com.iver.cit.gvsig.fmap.core.IFeature;
63
import com.iver.cit.gvsig.fmap.core.IGeometry;
64
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
65
import com.iver.cit.gvsig.project.documents.view.gui.View;
66
import com.vividsolutions.jts.geom.Geometry;
67

    
68
/**
69
 * This extension only will active when active layer
70
 * is a linear (dimension = 1) vectorial layer with
71
 * selected lines.
72
 * 
73
 * It smooths the selected lines.
74
 * 
75
 * @author Alvaro Zabala
76
 *
77
 */
78
public class SmoothPolylinesExtension extends Extension {
79

    
80
        private View view;
81
        
82
        public void execute(String actionCommand) {
83
                
84
                try {
85
                        FLyrVect lv = (FLyrVect) CADExtension.
86
                                                                getEditionManager().
87
                                                                getActiveLayerEdited().
88
                                                                getLayer();
89
                        int geometryDimensions = FGeometryUtil.getDimensions(lv.getShapeType());
90
                        
91
                        FeatureBitsetIterator iterator = new FeatureBitsetIterator(lv.getSource());
92
                        while(iterator.hasNext()){
93
                                IFeature feature = iterator.next();
94
                                IGeometry geometry = feature.getGeometry();
95
                                Geometry jtsGeometry = geometry.toJTSGeometry();
96
                                Geometry generalizedGeometry = null;
97
                                if(geometryDimensions == 1)
98
                                        generalizedGeometry = JtsUtil.douglasPeuckerSimplify(jtsGeometry, JtsUtil.GENERALIZATION_FACTOR.doubleValue());
99
                                else 
100
                                        generalizedGeometry = JtsUtil.topologyPreservingSimplify(jtsGeometry, JtsUtil.GENERALIZATION_FACTOR.doubleValue());
101
                                
102
                        }
103
                        
104
                        
105
                        
106
//                          IGeometry geom1=ShapeFactory.createPolyline2D(newGp1);
107
//                     IGeometry geom2=ShapeFactory.createPolyline2D(newGp2);
108
//
109
//                     DefaultFeature dfLine1 = (DefaultFeature) dre.getLinkedRow().cloneRow();
110
//                     dfLine1.setGeometry(geom1);
111
//            int indexLine1 = addGeometry(geom1, dfLine1
112
//              .getAttributes());
113
//
114
//            selectedRowAux.add(new DefaultRowEdited(dfLine1,
115
//              IRowEdited.STATUS_ADDED, indexLine1));
116
//
117
//            DefaultFeature dfLine2 = (DefaultFeature) dre.getLinkedRow().cloneRow();
118
//                  dfLine2.setGeometry(geom2);
119
//            int indexLine2 = addGeometry(geom2, dfLine2
120
//               .getAttributes());
121
//
122
//            selectedRowAux.add(new DefaultRowEdited(dfLine2,
123
//               IRowEdited.STATUS_ADDED, indexLine2));
124
//                         vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
125
//                     vea.endComplexRow(getName());
126
//                   vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
127
                        
128
                } catch (ReadDriverException e) {
129
                        String title = PluginServices.getText(this, "Error_De_Acceso_Datos");
130
                        String msg = PluginServices.getText(this, "Error_accediendo_a_los_datos");
131
                        GUIUtil.getInstance().messageBox(msg, title);
132
                        e.printStackTrace();
133
                }
134
        }
135

    
136
        public void initialize() {
137
                registerIcons();
138
        }
139
        
140
        private void registerIcons(){
141
                PluginServices.getIconTheme().registerDefault("smooth-geometry",
142
                                                                                                           this.getClass().
143
                                                                                                           getClassLoader().
144
                                                                                                           getResource("images/smooth-geometry.gif")
145
                );
146
        }
147

    
148
        /**
149
         * Returns if this Edit CAD tool is visible. 
150
         * For this, there must be an active vectorial editing lyr in the TOC, which geometries'
151
         * dimension would must be linear or polygonal, and with at least one selected geometry.
152
         *  
153
         */
154
        public boolean isEnabled() {
155
                try {
156
                        if (EditionUtilities.getEditionStatus() == 
157
                                EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
158
                                        this.view = (View) PluginServices.getMDIManager().getActiveWindow();
159
                                        if (CADExtension.getEditionManager().getActiveLayerEdited() == null)
160
                                                return false;
161
                                        FLyrVect lv = (FLyrVect) CADExtension.
162
                                                                                        getEditionManager().
163
                                                                                        getActiveLayerEdited().
164
                                                                                        getLayer();
165
                                        int geometryDimensions = FGeometryUtil.getDimensions(lv.getShapeType());
166
                                        if(geometryDimensions <= 0)
167
                                                return false;
168
                                        
169
                                        return lv.getRecordset().getSelection().cardinality() != 0;
170
                        }
171
                } catch (ReadDriverException e) {
172
                        NotificationManager.addError(e.getMessage(),e);
173
                        return false;
174
                }
175
                return true;
176
        }
177

    
178
        public boolean isVisible() {
179
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
180
                        return true;
181
                return false;
182
        }
183
}