Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.reproject / src / main / java / org / gvsig / geoprocess / algorithm / reproject / ReprojectAlgorithm.java @ 334

History | View | Annotate | Download (6.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.reproject;
25

    
26
import org.cresques.cts.IProjection;
27
import org.gvsig.fmap.crs.CRSFactory;
28
import org.gvsig.fmap.dal.exception.DataException;
29
import org.gvsig.fmap.dal.feature.FeatureSet;
30
import org.gvsig.fmap.dal.feature.FeatureStore;
31
import org.gvsig.fmap.dal.feature.FeatureType;
32
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
33
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
34

    
35
import es.unex.sextante.core.Sextante;
36
import es.unex.sextante.dataObjects.IVectorLayer;
37
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
38
import es.unex.sextante.exceptions.RepeatedParameterNameException;
39
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
40
import es.unex.sextante.outputs.OutputVectorLayer;
41

    
42
/**
43
 * Reproject algorithm
44
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
45
 */
46
public class ReprojectAlgorithm extends AbstractSextanteGeoProcess {
47
        public static final String        RESULT_POL        = "RESULT_POL";
48
        public static final String        RESULT_POINT      = "RESULT_POINT";
49
        public static final String        RESULT_LINE       = "RESULT_LINE";
50
        public static final String        LAYER             = "LAYER";
51
        public static final String        SELECTED_GEOM     = "SELECTED_GEOM";
52
        public static final String        DST_PROJECTION    = "DST_PROJECTION";
53
        
54
        /*
55
         * (non-Javadoc)
56
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
57
         */
58
        public void defineCharacteristics() {
59
        setName(getTranslation("Reproject"));
60
        setGroup(getTranslation("basic_vect_algorithms"));
61
        // setGeneratesUserDefinedRasterOutput(false);
62
                
63
                try {
64
                        m_Parameters.addInputVectorLayer(LAYER, 
65
                getTranslation("Input_layer"),
66
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
67
                                                                                                true);
68
                        m_Parameters.addBoolean(SELECTED_GEOM, 
69
                getTranslation("Selected_geometries"),
70
                                                                        false);
71
            m_Parameters
72
                .addString(DST_PROJECTION, getTranslation("Projection"));
73
            
74
            addOutputVectorLayer(RESULT_POL, getTranslation("Reproject_polygon"),
75
                                        OutputVectorLayer.SHAPE_TYPE_POLYGON);
76
                        addOutputVectorLayer(RESULT_LINE, getTranslation("Reproject_line"),
77
                                        OutputVectorLayer.SHAPE_TYPE_LINE);
78
                        addOutputVectorLayer(RESULT_POINT, getTranslation("Reproject_point"),
79
                                        OutputVectorLayer.SHAPE_TYPE_POINT);
80
                        
81
                } catch (RepeatedParameterNameException e) {
82
                        Sextante.addErrorToLog(e);
83
                }
84
        }
85
        
86
        /*
87
         * (non-Javadoc)
88
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
89
         */
90
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
91
                if(existsOutPutFile(RESULT_POL, 0) || 
92
                        existsOutPutFile(RESULT_LINE, 0) ||
93
                        existsOutPutFile(RESULT_POINT, 0)) {
94
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
95
            }
96
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
97
                boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
98
                String dstProj = m_Parameters.getParameterValueAsString(DST_PROJECTION);
99
                
100
                FeatureStore storeLayer = null;
101
                if(layer instanceof FlyrVectIVectorLayer)
102
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
103
                else
104
                        return false;
105
                
106
                try {
107
                        FeatureSet features = null;
108
                        features = storeLayer.getFeatureSet();
109
                        FeatureType featureType = features.getDefaultFeatureType();
110
            /*FeatureStore outFeatStore =
111
                buildOutPutStore(featureType, layer.getShapeType(),
112
                    getTranslation("Reproject"), RESULT);
113
                        IProjection projOutput = CRSFactory.getCRS(dstProj);
114
                        ReprojectOperation operation = new ReprojectOperation(((IProjection)layer.getCRS()), projOutput, this);
115
            operation.setTaskStatus(getStatus());
116
                        operation.computesFeatureOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);*/
117
                        
118
                        IProjection projOutput = CRSFactory.getCRS(dstProj);
119
                        ReprojectOperation operation = new ReprojectOperation(((IProjection)layer.getCRS()), projOutput, this);
120
            operation.setTaskStatus(getStatus());
121
            
122
            if (isPolygon(storeLayer) || isUndefined(storeLayer)) {
123
                                FeatureStore outFeatStore =
124
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON,
125
                                                        getTranslation("Reproject_polygon"), RESULT_POL);
126
                                operation.computesFeatureOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
127
                        } else {
128
                                buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON,
129
                                                getTranslation("Null_polygon"), RESULT_POL);
130
                        }
131
                        
132
                        if (isLine(storeLayer) || isUndefined(storeLayer)) {
133
                                FeatureStore outFeatStore =
134
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_LINE,
135
                                                        getTranslation("Reproject_line"), RESULT_LINE);
136
                                operation.computesFeatureOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
137
                        } else {
138
                                buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_LINE,
139
                                                getTranslation("Null_line"), RESULT_LINE);
140
                        }
141
                        
142
                        if (isPoint(storeLayer) || isUndefined(storeLayer)) {
143
                                FeatureStore outFeatStore =
144
                                        buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_POINT,
145
                                                        getTranslation("Reproject_point"), RESULT_POINT);
146
                                operation.computesFeatureOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
147
                        } else {
148
                                buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_POINT,
149
                                                getTranslation("Null_point"), RESULT_POINT);
150
                        }
151
                } catch (DataException e) {
152
                        Sextante.addErrorToLog(e);
153
                        return false;
154
                }
155
                
156
                if(getTaskMonitor().isCanceled())
157
                        return false;
158
                
159
                return true;
160
        }
161

    
162
    @Override
163
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
164
        return ReprojectParametersPanel.class;
165
    }
166
}