Statistics
| Revision:

gvsig-lrs / org.gvsig.lrs / trunk / org.gvsig.lrs / org.gvsig.lrs.lib / org.gvsig.lrs.lib.impl / src / main / java / org / gvsig / lrs / lib / impl / DefaultLrsAlgorithmsManager.java @ 9

History | View | Annotate | Download (8.48 KB)

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

    
25
import org.apache.commons.lang3.StringUtils;
26
import org.cresques.cts.IProjection;
27

    
28
import org.gvsig.fmap.dal.DALLocator;
29
import org.gvsig.fmap.dal.DataManager;
30
import org.gvsig.fmap.dal.DataServerExplorer;
31
import org.gvsig.fmap.dal.DataServerExplorerParameters;
32
import org.gvsig.fmap.dal.DataStoreParameters;
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.exception.InitializeException;
35
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
36
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
37
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.EditableFeatureType;
39
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
import org.gvsig.fmap.dal.feature.FeatureType;
42
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
43
import org.gvsig.fmap.geom.DataTypes;
44
import org.gvsig.fmap.geom.GeometryLocator;
45
import org.gvsig.fmap.geom.GeometryManager;
46
import org.gvsig.fmap.geom.type.GeometryType;
47
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
48
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
49
import org.gvsig.lrs.lib.api.LrsAlgorithm;
50
import org.gvsig.lrs.lib.api.LrsAlgorithmParams;
51
import org.gvsig.lrs.lib.api.LrsAlgorithmsManager;
52
import org.gvsig.lrs.lib.api.LrsCalibrateRouteAlgorithmParams;
53
import org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams;
54
import org.gvsig.lrs.lib.api.LrsEditRouteCalibrationAlgorithmParams;
55
import org.gvsig.lrs.lib.api.LrsGenerateDynamicSegmentationAlgorithmParams;
56
import org.gvsig.tools.dynobject.DynObject;
57
import org.gvsig.tools.service.Service;
58
import org.gvsig.tools.service.ServiceException;
59

    
60

    
61
/**
62
 * @author fdiaz
63
 *
64
 */
65
public class DefaultLrsAlgorithmsManager implements LrsAlgorithmsManager {
66

    
67
    /* (non-Javadoc)
68
     * @see org.gvsig.tools.service.Manager#createServiceParameters(java.lang.String)
69
     */
70
    public DynObject createServiceParameters(String serviceName) throws ServiceException {
71
        // TODO Auto-generated method stub
72
        return null;
73
    }
74

    
75
    /* (non-Javadoc)
76
     * @see org.gvsig.tools.service.Manager#getService(org.gvsig.tools.dynobject.DynObject)
77
     */
78
    public Service getService(DynObject parameters) throws ServiceException {
79
        // TODO Auto-generated method stub
80
        return null;
81
    }
82

    
83
    /* (non-Javadoc)
84
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmsManager#createLrsAlgorithm(org.gvsig.lrs.lib.api.LrsAlgorithmParams)
85
     */
86
    public LrsAlgorithm createLrsAlgorithm(LrsAlgorithmParams params) {
87
        if(params instanceof LrsCreateRouteAlgorithmParams){
88
            return new LrsCreateRouteAlgorithm((LrsCreateRouteAlgorithmParams)params);
89
        }
90
        return null;
91
    }
92

    
93
    /* (non-Javadoc)
94
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmsManager#createLrsCreateRouteAlgorithmParams()
95
     */
96
    public LrsCreateRouteAlgorithmParams createLrsCreateRouteAlgorithmParams() {
97
        return new DefaultLrsCreateRouteAlgorithmParams();
98
    }
99

    
100
    /* (non-Javadoc)
101
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmsManager#createLrsCalibrateRouteAlgorithmParams()
102
     */
103
    public LrsCalibrateRouteAlgorithmParams createLrsCalibrateRouteAlgorithmParams() {
104
        // TODO Auto-generated method stub
105
        return null;
106
    }
107

    
108
    /* (non-Javadoc)
109
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmsManager#createLrsEditRouteCalibrationAlgorithmParams()
110
     */
111
    public LrsEditRouteCalibrationAlgorithmParams createLrsEditRouteCalibrationAlgorithmParams() {
112
        // TODO Auto-generated method stub
113
        return null;
114
    }
115

    
116
    /* (non-Javadoc)
117
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmsManager#createLrsGenerateDynamicSegmentationAlgorithmParams()
118
     */
119
    public LrsGenerateDynamicSegmentationAlgorithmParams createLrsGenerateDynamicSegmentationAlgorithmParams() {
120
        // TODO Auto-generated method stub
121
        return null;
122
    }
123

    
124

    
125
    public void createNewShape(FeatureType sourceFeatureType,
126
        int outputLayerType, String outputLayerPath, IProjection projection) throws ValidateDataParametersException, DataException, GeometryTypeNotSupportedException, GeometryTypeNotValidException {
127

    
128
        DataManager dataManager = DALLocator.getDataManager();
129
        DataServerExplorerParameters eparams =
130
            dataManager.createServerExplorerParameters("FilesystemExplorer");
131
        eparams.setDynValue("initialpath", "/data");
132
        DataServerExplorer serverExplorer =
133
            dataManager.openServerExplorer(eparams.getExplorerName(), eparams);
134

    
135
        NewFeatureStoreParameters sparams =
136
            (NewFeatureStoreParameters) serverExplorer
137
                .getAddParameters("Shape");
138

    
139
        EditableFeatureType newEditableFeatureType =
140
            (EditableFeatureType) sparams.getDefaultFeatureType();
141

    
142
        // Iterate over feature type. Don't add geometry fields.
143
        for (int i = 0; i < sourceFeatureType.size(); i++) {
144
            String fieldName =
145
                sourceFeatureType.getAttributeDescriptor(i).getName();
146
            int fieldType =
147
                sourceFeatureType.getAttributeDescriptor(i).getType();
148
            if (fieldType != DataTypes.GEOMETRY) {
149
                newEditableFeatureType.add(fieldName, fieldType);
150
            }
151
        }
152

    
153
        // Add new geometry field with new geometry type
154
        FeatureAttributeDescriptor geometryAttribute =
155
            (FeatureAttributeDescriptor) sourceFeatureType
156
                .get(sourceFeatureType.getDefaultGeometryAttributeName());
157
        EditableFeatureAttributeDescriptor newGeometryAttribute =
158
            newEditableFeatureType.add(geometryAttribute.getName(),
159
                geometryAttribute.getType(), geometryAttribute.getSize());
160

    
161
        GeometryManager geoManager = GeometryLocator.getGeometryManager();
162
        GeometryType sourceLayerGeomType =
163
            sourceFeatureType.getDefaultGeometryAttribute().getGeomType();
164
        GeometryType outputLayerGeomType =
165
            geoManager.getGeometryType(outputLayerType,
166
                sourceLayerGeomType.getSubType());
167

    
168
        newGeometryAttribute.setGeometryType(outputLayerGeomType);
169
        newGeometryAttribute.setPrecision(geometryAttribute.getPrecision());
170
        newGeometryAttribute.setDefaultValue(geometryAttribute
171
            .getDefaultValue());
172

    
173
        newEditableFeatureType
174
            .setDefaultGeometryAttributeName(geometryAttribute.getName());
175

    
176
        if(!StringUtils.endsWithIgnoreCase(outputLayerPath, ".shp")){
177
            outputLayerPath = new StringBuilder().append(outputLayerPath).append(".shp").toString();
178
        }
179

    
180
        sparams.setDynValue("geometryType", null);
181
        sparams.setDynValue("shpfile", outputLayerPath);
182
        sparams.setDynValue("CRS", projection);
183
        sparams.setDynValue("useNullGeometry", false);
184
        sparams.setDefaultFeatureType(newEditableFeatureType);
185
        sparams.validate();
186

    
187
        serverExplorer.add("Shape", sparams, true);
188
    }
189

    
190
    public FeatureStore getFeatureStore(String outputLayerPath,
191
        IProjection projection) throws InitializeException, ProviderNotRegisteredException, ValidateDataParametersException{
192

    
193
        DataManager dataManager = DALLocator.getDataManager();
194
        DataStoreParameters dataStoreParams =
195
            dataManager.createStoreParameters("Shape");
196
        dataStoreParams.setDynValue("shpfile", outputLayerPath);
197
        dataStoreParams.setDynValue("CRS", projection);
198
        dataStoreParams.setDynValue("useNullGeometry", false);
199
        dataStoreParams.validate();
200

    
201
        return (FeatureStore) dataManager.openStore("Shape", dataStoreParams);
202
    }
203

    
204
}