Revision 351

View differences:

org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.autopolygon/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.vectorediting.lib.prov.autopolygon.AutopolygonEditingLibrary
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.autopolygon/src/main/java/org/gvsig/vectorediting/lib/prov/autopolygon/AutopolygonEditingLibrary.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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.vectorediting.lib.prov.autopolygon;
25

  
26

  
27
import org.gvsig.tools.ToolsLocator;
28
import org.gvsig.tools.i18n.I18nManager;
29
import org.gvsig.tools.library.AbstractLibrary;
30
import org.gvsig.tools.library.LibraryException;
31
import org.gvsig.vectorediting.lib.api.EditingLibrary;
32
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator;
33
import org.gvsig.vectorediting.lib.spi.EditingProviderManager;
34

  
35
/**
36
 * Library for default implementation initialization and configuration.
37
 *
38
 * @author gvSIG team
39
 * @version $Id$
40
 */
41
public class AutopolygonEditingLibrary extends AbstractLibrary {
42

  
43
    @Override
44
    public void doRegistration() {
45
        registerAsServiceOf(EditingLibrary.class);
46
    }
47

  
48
    @Override
49
    protected void doInitialize() throws LibraryException {
50
    }
51

  
52
    @Override
53
    protected void doPostInitialize() throws LibraryException {
54
        EditingProviderManager manager =
55
            EditingProviderLocator.getProviderManager();
56

  
57
        manager.addProviderFactory(new AutopolygonEditingProviderFactory());
58

  
59
        manager.registerIcon("vectorediting-tools", "insert-autopolygon",
60
            this.getClass().getClassLoader(), this.getClass().getName());
61

  
62
        registerTranslations();
63
    }
64

  
65
    private void registerTranslations() {
66
        I18nManager manager = ToolsLocator.getI18nManager();
67
        manager.addResourceFamily("i18n/text",
68
            this.getClass().getClassLoader(), "insert-autopolygon");
69
    }
70

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

  
26

  
27
import java.util.ArrayList;
28
import java.util.HashMap;
29
import java.util.List;
30
import java.util.Map;
31

  
32
import org.cresques.cts.IProjection;
33

  
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.EditableFeature;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureQuery;
38
import org.gvsig.fmap.dal.feature.FeatureSelection;
39
import org.gvsig.fmap.dal.feature.FeatureSet;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
import org.gvsig.fmap.dal.feature.FeatureType;
42
import org.gvsig.fmap.geom.Geometry;
43
import org.gvsig.fmap.geom.GeometryLocator;
44
import org.gvsig.fmap.geom.GeometryManager;
45
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
46
import org.gvsig.fmap.geom.Geometry.TYPES;
47
import org.gvsig.fmap.geom.aggregate.MultiSurface;
48
import org.gvsig.fmap.geom.exception.CreateGeometryException;
49
import org.gvsig.fmap.geom.primitive.Circle;
50
import org.gvsig.fmap.geom.primitive.Ellipse;
51
import org.gvsig.fmap.geom.primitive.Point;
52
import org.gvsig.fmap.geom.primitive.Surface;
53
import org.gvsig.fmap.geom.type.GeometryType;
54
import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsGeometryEvaluator;
55
import org.gvsig.tools.dispose.DisposableIterator;
56
import org.gvsig.tools.dynobject.DynObject;
57
import org.gvsig.tools.exception.BaseException;
58
import org.gvsig.tools.service.spi.ProviderServices;
59
import org.gvsig.tools.visitor.Visitor;
60
import org.gvsig.vectorediting.lib.api.DrawingStatus;
61
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
62
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
63
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
64
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
65
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
66
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
67
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
68
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
69
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
70
import org.gvsig.vectorediting.lib.spi.EditingProvider;
71
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
72
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
73

  
74
public class AutopolygonEditingProvider extends AbstractEditingProvider
75
    implements EditingProvider {
76

  
77
    private EditingServiceParameter geometryParameter;
78

  
79
    private FeatureStore featureStore;
80

  
81
    private Geometry geometryValue;
82

  
83
    public AutopolygonEditingProvider(ProviderServices services,
84
        DynObject parameters) {
85
        super(services);
86
        this.featureStore =
87
            (FeatureStore) parameters
88
                .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
89

  
90
        this.geometryParameter =
91
            new DefaultEditingServiceParameter("Draw geometry",
92
                "draw_geometry_to_autopolygon", Geometry.TYPES.MULTISURFACE, TYPE.GEOMETRY);
93
    }
94

  
95
    public EditingServiceParameter next() {
96
        if (geometryValue == null) {
97
            return geometryParameter;
98
        }
99

  
100
        return null;
101
    }
102

  
103
    public DrawingStatus getDrawingStatus(Point mousePosition)
104
        throws DrawServiceException {
105
        return null;
106
    }
107

  
108
    public void stop() throws StopServiceException {
109
        geometryValue = null;
110
    }
111

  
112
    public List<EditingServiceParameter> getParameters() {
113
        List<EditingServiceParameter> list =
114
            new ArrayList<EditingServiceParameter>();
115
        list.add(geometryParameter);
116
        return list;
117
    }
118

  
119
    public void setValue(Object value) throws InvalidEntryException {
120
        EditingServiceParameter param = next();
121
        validateAndInsertValue(param, value);
122
    }
123

  
124
    private void validateAndInsertValue(EditingServiceParameter param,
125
        Object value) throws InvalidEntryException {
126

  
127
        if (param == geometryParameter) {
128
            if (value instanceof Geometry && value != null) {
129
                geometryValue = (Geometry)value;
130
                return;
131
            }
132
        }
133
        throw new InvalidEntryException(null);
134
    }
135

  
136
    public void finishAndStore() throws FinishServiceException {
137
        EditingProviderServices editingProviderServices =
138
            (EditingProviderServices) getProviderServices();
139
        Geometry geometry = finish();
140
        editingProviderServices.insertGeometryIntoFeatureStore(geometry,
141
            featureStore);
142
    }
143

  
144
    public Geometry finish() throws FinishServiceException {
145
        EditingProviderServices editingProviderServices =
146
            (EditingProviderServices) getProviderServices();
147

  
148
        Geometry geometry = geometryValue.cloneGeometry();
149
        FeatureQuery query = null;
150
        try {
151
            query = queryByGeometry(geometry);
152
            if (query != null) {
153
                FeatureSet features = featureStore.getFeatureSet(query);
154
                DisposableIterator featuresIterator = features.fastIterator();
155
                while (featuresIterator.hasNext()) {
156
                    Feature feature = (Feature) featuresIterator.next();
157
                    geometry =
158
                        geometry.difference(feature.getDefaultGeometry());
159
                }
160
                featuresIterator.dispose();
161
                features.dispose();
162
            }
163
            GeometryType storeGeomType =
164
                editingProviderServices.getGeomType(featureStore);
165

  
166
            if (storeGeomType.isTypeOf(MULTISURFACE)) {
167
                MultiSurface multisurface;
168
                multisurface =
169
                    GeometryLocator.getGeometryManager().createMultiSurface(
170
                        storeGeomType.getSubType());
171
                multisurface.addSurface((Surface)geometry);
172
                return multisurface;
173
            }
174

  
175
        } catch (Exception e) {
176
            new FinishServiceException(e);
177
        }
178
        return geometry;
179
    }
180

  
181
    public void start() throws StartServiceException, InvalidEntryException {
182
        geometryValue = null;
183
    }
184

  
185
    public String getName() {
186
        return AutopolygonEditingProviderFactory.PROVIDER_NAME;
187
    }
188

  
189
    private FeatureQuery queryByGeometry(Geometry geometry) throws DataException {
190
        FeatureQuery featureQuery = featureStore.createFeatureQuery();
191
        FeatureType featureType = featureStore.getDefaultFeatureType();
192
        String geomName = featureType.getDefaultGeometryAttributeName();
193
        featureQuery.setFeatureType(featureType);
194

  
195
        IProjection query_proj = featureType.getDefaultSRS();
196
        IntersectsGeometryEvaluator iee =
197
            new IntersectsGeometryEvaluator(geometry, query_proj,
198
                featureType, geomName);
199
        featureQuery.setFilter(iee);
200
        featureQuery.setAttributeNames(null);
201
        return featureQuery;
202
    }
203

  
204

  
205

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

  
26

  
27
import org.gvsig.fmap.geom.Geometry;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dynobject.DynClass;
30
import org.gvsig.tools.dynobject.DynObject;
31
import org.gvsig.tools.dynobject.DynObjectManager;
32
import org.gvsig.tools.service.spi.AbstractProviderFactory;
33
import org.gvsig.tools.service.spi.Provider;
34
import org.gvsig.tools.service.spi.ProviderServices;
35
import org.gvsig.vectorediting.lib.api.EditingServiceInfo;
36
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceinfo;
37
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
38

  
39
public class AutopolygonEditingProviderFactory extends
40
AbstractProviderFactory implements EditingProviderFactory {
41

  
42
    public final static String PROVIDER_NAME = "insert-autopolygon";
43

  
44
    public final static String PROVIDER_DESCRIPTION =
45
        "Creates autopolygons";
46

  
47
    public void initialize() {
48
    }
49

  
50
    public EditingServiceInfo getServiceInfo() {
51
        EditingServiceInfo serviceInfo =
52
            new DefaultEditingServiceinfo(
53
                PROVIDER_NAME,
54
                "",
55
                false,
56
                null,
57
                new int[] { Geometry.TYPES.SURFACE, Geometry.TYPES.MULTISURFACE });
58

  
59
        return serviceInfo;
60
    }
61

  
62
    @Override
63
    protected Provider doCreate(DynObject parameters, ProviderServices services) {
64
        return new AutopolygonEditingProvider(services, parameters);
65
    }
66

  
67
    @Override
68
    protected DynClass createParametersDynClass() {
69
        DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager();
70
        DynClass dynclass =
71
            dynObjectManager
72
            .createDynClass(PROVIDER_NAME, PROVIDER_DESCRIPTION);
73

  
74
        dynclass.addDynFieldString(PROVIDER_NAME_FIELD);
75
        dynclass.addDynFieldObject(FEATURE_STORE_FIELD);
76
        dynclass.addDynFieldObject(MAPCONTEXT_FIELD);
77

  
78
        return dynclass;
79
    }
80

  
81
    @Override
82
    public DynObject createParameters() {
83
        DynObject parameters = super.createParameters();
84
        parameters.setDynValue(PROVIDER_NAME_FIELD, PROVIDER_NAME);
85
        return parameters;
86
    }
87

  
88
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.autopolygon/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
  <modelVersion>4.0.0</modelVersion>
3
  <parent>
4
    <groupId>org.gvsig</groupId>
5
    <artifactId>org.gvsig.vectorediting.lib.prov</artifactId>
6
    <version>1.0.1</version>
7
  </parent>
8
  <artifactId>org.gvsig.vectorediting.lib.prov.autopolygon</artifactId>
9
  <name>org.gvsig.vectorediting.lib.prov.autopoligon</name>
10
</project>
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.vectorediting.lib.prov.smoothline.SmoothLineEditingLibrary
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/ControlCurve.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3
import org.gvsig.fmap.geom.exception.CreateGeometryException;
4
import org.gvsig.fmap.geom.primitive.Line;
5

  
6
public abstract class ControlCurve {
7

  
8
    protected double[] m_X;
9
    protected double[] m_Y;
10
    protected Line line;
11

  
12
    public ControlCurve(final Line line) {
13

  
14
       m_X = new double[line.getNumVertices()];
15
       m_Y = new double[line.getNumVertices()];
16
       for (int i = 0; i < line.getNumVertices(); i++) {
17
          m_X[i] = line.getVertex(i).getX();
18
          m_Y[i] = line.getVertex(i).getY();
19
       }
20
       this.line = line;
21

  
22
    }
23

  
24
    public abstract Line getSmoothedLine(int iSteps, int subtype) throws CreateGeometryException;
25

  
26
 }
27

  
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/Cubic.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3

  
4
public class Cubic {
5

  
6
    double a, b, c, d; /* a + b*u + c*u^2 +d*u^3 */
7

  
8

  
9
    public Cubic(final double a,
10
                 final double b,
11
                 final double c,
12
                 final double d) {
13
       this.a = a;
14
       this.b = b;
15
       this.c = c;
16
       this.d = d;
17
    }
18

  
19
    /** evaluate cubic */
20
    public double eval(final double u) {
21
       return (((d * u) + c) * u + b) * u + a;
22
    }
23
 }
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/NatCubic.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3
import org.gvsig.fmap.geom.GeometryLocator;
4
import org.gvsig.fmap.geom.GeometryManager;
5
import org.gvsig.fmap.geom.exception.CreateGeometryException;
6
import org.gvsig.fmap.geom.primitive.Line;
7

  
8
public class NatCubic extends ControlCurve {
9

  
10
    /*
11
     * calculates the natural cubic spline that interpolates
12
     * y[0], y[1], ... y[n]
13
     * The first segment is returned as
14
     * C[0].a + C[0].b*u + C[0].c*u^2 + C[0].d*u^3 0<=u <1
15
     * the other segments are in C[1], C[2], ... C[n-1]
16
     */
17

  
18
    public NatCubic(final Line line) {
19
        super(line);
20
    }
21

  
22
    Cubic[] calcNaturalCubic(final int n, final double[] x) {
23
        final double[] gamma = new double[n + 1];
24
        final double[] delta = new double[n + 1];
25
        final double[] D = new double[n + 1];
26
        int i;
27
        /*
28
         * We solve the equation
29
         * [2 1 ] [D[0]] [3(x[1] - x[0]) ]
30
         * |1 4 1 | |D[1]| |3(x[2] - x[0]) |
31
         * | 1 4 1 | | . | = | . |
32
         * | ..... | | . | | . |
33
         * | 1 4 1| | . | |3(x[n] - x[n-2])|
34
         * [ 1 2] [D[n]] [3(x[n] - x[n-1])]
35
         *
36
         * by using row operations to convert the matrix to upper triangular
37
         * and then back sustitution. The D[i] are the derivatives at the knots.
38
         */
39

  
40
        gamma[0] = 1.0f / 2.0f;
41
        for (i = 1; i < n; i++) {
42
            gamma[i] = 1 / (4 - gamma[i - 1]);
43
        }
44
        gamma[n] = 1 / (2 - gamma[n - 1]);
45

  
46
        delta[0] = 3 * (x[1] - x[0]) * gamma[0];
47
        for (i = 1; i < n; i++) {
48
            delta[i] = (3 * (x[i + 1] - x[i - 1]) - delta[i - 1]) * gamma[i];
49
        }
50
        delta[n] = (3 * (x[n] - x[n - 1]) - delta[n - 1]) * gamma[n];
51

  
52
        D[n] = delta[n];
53
        for (i = n - 1; i >= 0; i--) {
54
            D[i] = delta[i] - gamma[i] * D[i + 1];
55
        }
56

  
57
        /* now compute the coefficients of the cubics */
58
        final Cubic[] C = new Cubic[n];
59
        for (i = 0; i < n; i++) {
60
            C[i] =
61
                new Cubic(x[i], D[i], 3 * (x[i + 1] - x[i]) - 2 * D[i]
62
                    - D[i + 1], 2 * (x[i] - x[i + 1]) + D[i] + D[i + 1]);
63
        }
64
        return C;
65
    }
66

  
67
    @Override
68
    public Line getSmoothedLine(final int iSteps, int subtype) throws CreateGeometryException {
69
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
70

  
71
        if (m_X.length >= 2) {
72
            final Cubic[] X = calcNaturalCubic(m_X.length - 1, m_X);
73
            final Cubic[] Y = calcNaturalCubic(m_Y.length - 1, m_Y);
74

  
75
            Line line = geomManager.createLine(subtype);
76
            /*
77
             * very crude technique - just break each segment up into steps
78
             * lines
79
             */
80
            line.addVertex((int) Math.round(X[0].eval(0)),
81
                (int) Math.round(Y[0].eval(0)));
82

  
83
            for (int i = 0; i < X.length; i++) {
84
                for (int j = 1; j <= iSteps; j++) {
85
                    final float u = j / (float) iSteps;
86
                    line.addVertex(X[i].eval(u), Y[i].eval(u));
87
                }
88
            }
89

  
90
            return line;
91

  
92
        } else {
93
            return this.line;
94
        }
95
    }
96
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/SmoothLineEditingProviderFactory.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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.vectorediting.lib.prov.smoothline;
25

  
26
import org.gvsig.fmap.geom.Geometry;
27
import org.gvsig.tools.ToolsLocator;
28
import org.gvsig.tools.dynobject.DynClass;
29
import org.gvsig.tools.dynobject.DynObject;
30
import org.gvsig.tools.service.spi.AbstractProviderFactory;
31
import org.gvsig.tools.service.spi.Provider;
32
import org.gvsig.tools.service.spi.ProviderServices;
33
import org.gvsig.vectorediting.lib.api.EditingServiceInfo;
34
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceinfo;
35
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
36

  
37
/**
38
 * @author fdiaz
39
 *
40
 */
41
public class SmoothLineEditingProviderFactory extends AbstractProviderFactory
42
    implements EditingProviderFactory {
43

  
44
    public static final String PROVIDER_NAME = "modify-smooth-line";
45

  
46
    private final static String PROVIDER_DESCRIPTION =
47
        "Smooth lines";
48

  
49
    public void initialize() {
50
    }
51

  
52
    public EditingServiceInfo getServiceInfo() {
53
        EditingServiceInfo serviceInfo =
54
            new DefaultEditingServiceinfo(PROVIDER_NAME, "", false, null,
55
                new int[] { Geometry.TYPES.CURVE,
56
                    Geometry.TYPES.MULTICURVE });
57

  
58
        return serviceInfo;
59
    }
60

  
61
    @Override
62
    protected Provider doCreate(DynObject parameters, ProviderServices services) {
63
        return new SmoothLineEditingProvider(parameters, services);
64
    }
65

  
66
    @Override
67
    public DynObject createParameters() {
68
        DynObject dynobject = super.createParameters();
69
        dynobject.setDynValue(PROVIDER_NAME_FIELD, PROVIDER_NAME);
70
        return dynobject;
71
    }
72

  
73
    @Override
74
    protected DynClass createParametersDynClass() {
75
        DynClass dynclass =
76
            ToolsLocator.getDynObjectManager().createDynClass(PROVIDER_NAME,
77
                PROVIDER_DESCRIPTION);
78

  
79
        dynclass.addDynFieldString(PROVIDER_NAME_FIELD);
80
        dynclass.addDynFieldObject(FEATURE_STORE_FIELD);
81
        dynclass.addDynFieldObject(MAPCONTEXT_FIELD);
82

  
83
        return dynclass;
84
    }
85

  
86
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/SmoothLineEditingLibrary.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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

  
25
package org.gvsig.vectorediting.lib.prov.smoothline;
26

  
27
import org.gvsig.tools.ToolsLocator;
28
import org.gvsig.tools.i18n.I18nManager;
29
import org.gvsig.tools.library.AbstractLibrary;
30
import org.gvsig.tools.library.Library;
31
import org.gvsig.tools.library.LibraryException;
32
import org.gvsig.vectorediting.lib.api.EditingLibrary;
33
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator;
34
import org.gvsig.vectorediting.lib.spi.EditingProviderManager;
35

  
36

  
37
/**
38
 * @author fdiaz
39
 *
40
 */
41
public class SmoothLineEditingLibrary extends AbstractLibrary implements Library {
42

  
43
    @Override
44
    public void doRegistration() {
45
        registerAsServiceOf(EditingLibrary.class);
46
    }
47

  
48
    @Override
49
    protected void doInitialize() throws LibraryException {
50
    }
51

  
52
    @Override
53
    protected void doPostInitialize() throws LibraryException {
54
        EditingProviderManager manager =
55
            EditingProviderLocator.getProviderManager();
56

  
57
        manager.addProviderFactory(new SmoothLineEditingProviderFactory());
58

  
59
        manager.registerIcon("vectorediting-tools", "modify-smooth-line", this
60
            .getClass().getClassLoader(), this.getClass().getName());
61

  
62
        registerTranslations();
63
    }
64

  
65
    private void registerTranslations() {
66
        I18nManager manager = ToolsLocator.getI18nManager();
67
        manager.addResourceFamily("i18n.text",
68
            this.getClass().getClassLoader(), "smooth-line-editing");
69
    }
70

  
71
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/BSpline.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3
import org.gvsig.fmap.geom.GeometryLocator;
4
import org.gvsig.fmap.geom.GeometryManager;
5
import org.gvsig.fmap.geom.exception.CreateGeometryException;
6
import org.gvsig.fmap.geom.primitive.Line;
7
import org.gvsig.fmap.geom.primitive.Point;
8

  
9

  
10
public class BSpline extends ControlCurve {
11

  
12
    public BSpline(final Line line) {
13
        super(line);
14
    }
15

  
16
    // the basis function for a cubic B spline
17
    float b(final int i, float t) {
18
        switch (i) {
19
        case -2:
20
            return (((-t + 3) * t - 3) * t + 1) / 6;
21
        case -1:
22
            return (((3 * t - 6) * t) * t + 4) / 6;
23
        case 0:
24
            return (((-3 * t + 3) * t + 3) * t + 1) / 6;
25
        case 1:
26
            return (t * t * t) / 6;
27
        }
28
        return 0; // we only get here if an invalid i is specified
29
    }
30

  
31
    // evaluate a point on the B spline
32
    private Point p(final int i, final float t, int subtype) throws CreateGeometryException {
33
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
34
        float px = 0;
35
        float py = 0;
36
        for (int j = -2; j <= 1; j++) {
37
            px += b(j, t) * m_X[i + j];
38
            py += b(j, t) * m_Y[i + j];
39
        }
40
        return geomManager.createPoint((int) Math.round(px), (int) Math.round(py), subtype);
41
    }
42

  
43
    @Override
44
    public Line getSmoothedLine(final int iSteps, int subtype) throws CreateGeometryException {
45
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
46
        Line line = geomManager.createLine(subtype);
47

  
48
        Point q = p(2, 0, subtype);
49
        line.addVertex(q.getX(), q.getY());
50
        for (int i = 2; i < m_X.length - 1; i++) {
51
            for (int j = 1; j <= iSteps; j++) {
52
                q = p(i, j / (float) iSteps, subtype);
53
                line.addVertex(q);
54
            }
55
        }
56
        return line;
57
    }
58
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/Bezier.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3
import org.gvsig.fmap.geom.GeometryLocator;
4
import org.gvsig.fmap.geom.GeometryManager;
5
import org.gvsig.fmap.geom.exception.CreateGeometryException;
6
import org.gvsig.fmap.geom.primitive.Line;
7
import org.gvsig.fmap.geom.primitive.Point;
8

  
9
public class Bezier extends ControlCurve {
10

  
11
    public Bezier(final Line line) {
12
        super(line);
13
    }
14

  
15
    // the basis function for a Bezier spline
16
    static float b(final int i, final float t) {
17
        switch (i) {
18
        case 0:
19
            return (1 - t) * (1 - t) * (1 - t);
20
        case 1:
21
            return 3 * t * (1 - t) * (1 - t);
22
        case 2:
23
            return 3 * t * t * (1 - t);
24
        case 3:
25
            return t * t * t;
26
        }
27
        return 0; // we only get here if an invalid i is specified
28
    }
29

  
30
    // evaluate a point on the B spline
31
    Point p(final int i, final float t, int subtype) throws CreateGeometryException {
32
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
33
        float px = 0;
34
        float py = 0;
35
        for (int j = 0; j <= 3; j++) {
36
            px += b(j, t) * m_X[i + j];
37
            py += b(j, t) * m_Y[i + j];
38
        }
39
        return geomManager.createPoint((int) Math.round(px), (int) Math.round(py),subtype);
40
    }
41

  
42
    @Override
43
    public Line getSmoothedLine(final int iSteps, int subtype) throws CreateGeometryException {
44
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
45
        Line line = geomManager.createLine(subtype);
46

  
47
        Point q = p(0, 0, subtype);
48
        line.addVertex(q);
49
        for (int i = 0; i < m_X.length - 3; i += 3) {
50
            for (int j = 1; j <= iSteps; j++) {
51
                q = p(i, j / (float) iSteps, subtype);
52
                line.addVertex(q);
53
            }
54
        }
55
        return line;
56
    }
57
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/SmoothLineEditingProvider.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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.vectorediting.lib.prov.smoothline;
25

  
26
import java.util.ArrayList;
27
import java.util.Enumeration;
28
import java.util.HashMap;
29
import java.util.LinkedHashMap;
30
import java.util.List;
31
import java.util.Map;
32

  
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.EditableFeature;
35
import org.gvsig.fmap.dal.feature.Feature;
36
import org.gvsig.fmap.dal.feature.FeatureSelection;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryException;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42
import org.gvsig.fmap.geom.aggregate.MultiCurve;
43
import org.gvsig.fmap.geom.aggregate.MultiSurface;
44
import org.gvsig.fmap.geom.exception.CreateGeometryException;
45
import org.gvsig.fmap.geom.operation.GeometryOperationException;
46
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
47
import org.gvsig.fmap.geom.primitive.Arc;
48
import org.gvsig.fmap.geom.primitive.Curve;
49
import org.gvsig.fmap.geom.primitive.Line;
50
import org.gvsig.fmap.geom.primitive.Point;
51
import org.gvsig.fmap.geom.primitive.Polygon;
52
import org.gvsig.fmap.geom.primitive.Spline;
53
import org.gvsig.fmap.geom.primitive.Surface;
54
import org.gvsig.fmap.geom.type.GeometryType;
55
import org.gvsig.tools.ToolsLocator;
56
import org.gvsig.tools.dynobject.DynObject;
57
import org.gvsig.tools.exception.BaseException;
58
import org.gvsig.tools.i18n.I18nManager;
59
import org.gvsig.tools.service.spi.ProviderServices;
60
import org.gvsig.tools.visitor.VisitCanceledException;
61
import org.gvsig.tools.visitor.Visitor;
62
import org.gvsig.vectorediting.lib.api.DrawingStatus;
63
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
64
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
65
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
66
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
67
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
68
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
69
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
70
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
71
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
72
import org.gvsig.vectorediting.lib.spi.EditingProvider;
73
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
74
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
75

  
76
/**
77
 * @author fdiaz
78
 *
79
 */
80
public class SmoothLineEditingProvider extends AbstractEditingProvider implements
81
    EditingProvider {
82

  
83
    private EditingServiceParameter selection;
84

  
85
    private EditingServiceParameter steps;
86

  
87
    private EditingServiceParameter algorithm;
88

  
89
    private FeatureStore featureStore;
90

  
91
    private Map<EditingServiceParameter, Object> values;
92

  
93
    protected Map<String, String> options;
94

  
95
    public static final String    NATURAL_CUBIC_SPLINES = "natural_cubic_splines";
96
    public static final String    BEZIER_CURVES         = "bezier_curves";
97
    public static final String    BSPLINES              = "b_splines";
98

  
99
    /**
100
     * Default constructor.
101
     *
102
     * @param providerServices
103
     *            available services for this provider
104
     * @param parameters
105
     *            of this provider
106
     */
107
    public SmoothLineEditingProvider(DynObject parameters,
108
        ProviderServices services) {
109
        super(services);
110

  
111
        this.featureStore =
112
            (FeatureStore) parameters
113
                .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
114

  
115
        this.selection =
116
            new DefaultEditingServiceParameter("selection", "selection",
117
                TYPE.SELECTION);
118

  
119
        this.steps =
120
            new DefaultEditingServiceParameter("steps", "intermediate_steps_1_9",
121
                TYPE.VALUE);
122

  
123
        I18nManager i18nManager = ToolsLocator.getI18nManager();
124
//        "Natural cubic splines","Bezier curves", "B splines"
125
        options = new LinkedHashMap<String, String>();
126
        options.put(i18nManager.getTranslation("key_natural_cubic_splines"), NATURAL_CUBIC_SPLINES);
127
        options.put(i18nManager.getTranslation("key_bezier_curves"),BEZIER_CURVES);
128
        options.put(i18nManager.getTranslation("key_b_splines"),BSPLINES);
129

  
130

  
131
        EditingProviderServices editingProviderServices =
132
            (EditingProviderServices) getProviderServices();
133

  
134
        String consoleMsg =
135
            editingProviderServices.makeConsoleMessage(null, options);
136

  
137
        this.algorithm =
138
            new DefaultEditingServiceParameter("algorithm", consoleMsg, options,
139
                EditingServiceParameter.TYPE.OPTION);
140
}
141

  
142
    public EditingServiceParameter next() {
143
        if (values.get(selection) == null) {
144
            return selection;
145
        } else if (values.get(steps) == null) {
146
            return steps;
147
        } else if (values.get(algorithm) == null) {
148
            return algorithm;
149
        }
150
        return null;
151
    }
152

  
153
    public DrawingStatus getDrawingStatus(Point mousePosition)
154
        throws DrawServiceException {
155
        return null;
156
    }
157

  
158
    public void stop() throws StopServiceException {
159
        if (values != null) {
160
            values.clear();
161
        }
162
    }
163

  
164
    public List<EditingServiceParameter> getParameters() {
165
        List<EditingServiceParameter> parameters =
166
            new ArrayList<EditingServiceParameter>();
167
        parameters.add(selection);
168
        parameters.add(steps);
169
        parameters.add(algorithm);
170
        return parameters;
171
    }
172

  
173
    public void setValue(Object value) throws InvalidEntryException {
174
        EditingServiceParameter parameter = next();
175
        validateAndInsertValue(parameter, value);
176
    }
177

  
178
    private void validateAndInsertValue(EditingServiceParameter parameter,
179
        Object value) throws InvalidEntryException {
180

  
181
        if (parameter == selection) {
182

  
183
            FeatureSelection featureSelection = (FeatureSelection) value;
184

  
185
            if (featureSelection.getSelectedCount() > 0) {
186
                try {
187
                    featureSelection.accept(new Visitor() {
188

  
189
                        public void visit(Object obj)
190
                            throws VisitCanceledException, BaseException {
191

  
192
                            Feature feature = (Feature) obj;
193
                            Geometry geometry = feature.getDefaultGeometry();
194
                            GeometryType geometryType =
195
                                geometry.getGeometryType();
196

  
197
                            if (!geometryType.isTypeOf(Geometry.TYPES.CURVE)
198
                                && !geometryType.isTypeOf(Geometry.TYPES.MULTICURVE)) {
199

  
200
                                throw new InvalidEntryException(null);
201
                            }
202
                        }
203
                    });
204

  
205
                    values.put(selection, value);
206
                    return;
207
                } catch (BaseException e) {
208
                    throw new InvalidEntryException(e);
209
                }
210
            }
211
        } else if (parameter == steps) {
212
            if (value instanceof Double) {
213
                Integer stepsValue = ((Double) value).intValue();
214
                if (stepsValue > 0 && stepsValue < 10 ) {
215
                    values.put(steps, stepsValue);
216
                    return;
217
                }
218
            }
219
        }  else if (parameter == algorithm) {
220

  
221
            if (value instanceof String) {
222

  
223
                I18nManager i18nManager = ToolsLocator.getI18nManager();
224
                String option = (String) value;
225

  
226
                if (option.equalsIgnoreCase(i18nManager.getTranslation("key_natural_cubic_splines"))){
227
                    values.put(parameter, "key_natural_cubic_splines");
228
                    return;
229
                }
230
                if (option.equalsIgnoreCase(i18nManager.getTranslation("key_bezier_curves"))){
231
                    values.put(parameter, "key_bezier_curves");
232
                    return;
233
                }
234
                if (option.equalsIgnoreCase(i18nManager.getTranslation("key_b_splines"))){
235
                    values.put(parameter, "key_b_splines");
236
                    return;
237
                }
238
            }
239
            throw new InvalidEntryException(null);
240

  
241
        }
242
        throw new InvalidEntryException(null);
243
    }
244

  
245
    public Geometry finish() throws FinishServiceException {
246
        return null;
247
    }
248

  
249
    public void finishAndStore() throws FinishServiceException {
250

  
251
        if (values != null) {
252

  
253
            FeatureSelection featureSelection =
254
                (FeatureSelection) values.get(selection);
255

  
256
            final Integer stepsValue = (Integer) values.get(steps);
257

  
258
            final String algorithmValue = (String)values.get(algorithm);
259

  
260
            final EditingProviderServices editingProviderServices =
261
                (EditingProviderServices) getProviderServices();
262

  
263
            try {
264
                final int subtype =
265
                    editingProviderServices.getSubType(featureStore);
266

  
267
                featureSelection.accept(new Visitor() {
268

  
269
                    public void visit(Object obj)
270
                        throws VisitCanceledException, BaseException {
271

  
272
                        Feature feature = (Feature) obj;
273
                        Geometry geometry = feature.getDefaultGeometry();
274
                        GeometryType geoType = geometry.getGeometryType();
275

  
276
                        if (geoType.isTypeOf(AGGREGATE)) {
277

  
278
                            MultiCurve multiCurveToSmooth =
279
                                (MultiCurve) geometry;
280

  
281
                            MultiCurve multiCurveSmoothed =
282
                                GeometryLocator.getGeometryManager()
283
                                    .createMultiCurve(subtype);
284

  
285
                            for (int i = 0; i < multiCurveToSmooth
286
                                .getPrimitivesNumber(); i++) {
287

  
288
                                Curve curveToSmooth =
289
                                    (Curve) multiCurveToSmooth
290
                                        .getPrimitiveAt(i);
291
                                curveToSmooth =
292
                                    smoothLine(curveToSmooth, stepsValue, algorithmValue);
293
                                multiCurveSmoothed.addCurve(curveToSmooth);
294
                                geometry = multiCurveSmoothed;
295
                            }
296
                        } else {
297
                            Curve curveToSmooth = (Curve) geometry;
298
                            curveToSmooth =
299
                                smoothLine(curveToSmooth, stepsValue, algorithmValue);
300
                            geometry = curveToSmooth;
301
                        }
302

  
303
                        EditableFeature eFeature = feature.getEditable();
304
                        eFeature.setDefaultGeometry(geometry);
305
                        editingProviderServices.updateFeatureInFeatureStore(
306
                            eFeature, featureStore);
307
                    }
308
                });
309

  
310
                featureStore.getFeatureSelection().deselectAll();
311
                featureSelection.dispose();
312

  
313
            } catch (BaseException e) {
314
                throw new FinishServiceException(e);
315
            }
316
        }
317
    }
318

  
319
    private Line smoothLine(Curve curveToSmooth, Integer steps, String algorithm) throws DataException, CreateGeometryException{
320

  
321
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
322
        EditingProviderServices editingProviderServices =
323
            (EditingProviderServices) getProviderServices();
324

  
325
        int subtype = editingProviderServices.getSubType(featureStore);
326
        Line lineToSmooth = null;
327

  
328
        if (curveToSmooth instanceof Arc || curveToSmooth instanceof Spline) {
329
            lineToSmooth = geometryManager.createLine(curveToSmooth.getGeometryType().getSubType());
330
            lineToSmooth.setGeneralPath(curveToSmooth.getGeneralPath());
331

  
332
        } else {
333
            lineToSmooth = (Line)curveToSmooth;
334
        }
335

  
336
        ControlCurve operation;
337
        if (algorithm.equalsIgnoreCase("key_natural_cubic_splines")){
338
            operation = new NatCubic(lineToSmooth);
339
        } else if (algorithm.equalsIgnoreCase("key_bezier_curves")){
340
            operation = new Bezier(lineToSmooth);
341
        } else if (algorithm.equalsIgnoreCase("key_b_splines")){
342
            operation = new BSpline(lineToSmooth);
343
        } else {
344
            return null;
345
        }
346
        return operation.getSmoothedLine(steps, subtype);
347

  
348
    }
349

  
350

  
351
    private Line simplifyLine(Curve curveToSimplify, Double toleranceValue)
352
        throws DataException, CreateGeometryException,
353
        GeometryOperationNotSupportedException, GeometryOperationException {
354

  
355
        EditingProviderServices editingProviderServices =
356
            (EditingProviderServices) getProviderServices();
357

  
358
        int subtype = editingProviderServices.getSubType(featureStore);
359

  
360
        Line simplifiedLine =
361
            (Line) GeometryLocator.getGeometryManager().createLine(subtype);
362

  
363
        // Add first vertex
364
        simplifiedLine.addVertex(curveToSimplify.getVertex(0));
365

  
366
        // Iterate over vertices
367
        for (int i = 0; i < curveToSimplify.getNumVertices() - 2; i++) {
368

  
369
            Point vertex = curveToSimplify.getVertex(i);
370
            Point nextVertex = curveToSimplify.getVertex(i + 1);
371
            Point nextNextVertex = curveToSimplify.getVertex(i + 2);
372

  
373
            // Creates line between i vertex and i+2 vertex
374
            Line tmpLine =
375
                editingProviderServices.createLine(vertex, nextNextVertex,
376
                    subtype);
377

  
378
            // If distance between line and i+1 vertex is bigger than tolerance
379
            // add point to simplifiedLine, else skip vertex
380
            if (tmpLine.distance(nextVertex) > toleranceValue) {
381
                simplifiedLine.addVertex(nextVertex);
382
            } else {
383
                i++;
384
            }
385
        }
386

  
387
        // Add last vertex
388
        simplifiedLine.addVertex(curveToSimplify.getVertex(curveToSimplify
389
            .getNumVertices() - 1));
390

  
391
        return simplifiedLine;
392
    }
393

  
394
    public void start() throws StartServiceException, InvalidEntryException {
395
        values = new HashMap<EditingServiceParameter, Object>();
396

  
397
        FeatureSelection selected = null;
398
        if (featureStore != null) {
399
            try {
400
                selected = featureStore.getFeatureSelection();
401
            } catch (DataException e) {
402
                throw new StartServiceException(e);
403
            }
404
            if (selected.getSelectedCount() > 0) {
405
                try {
406
                    setValue(selected);
407
                } catch (InvalidEntryException e) {
408
                    throw new InvalidEntryException(e);
409
                }
410
            }
411
        }
412
    }
413

  
414
    public String getName() {
415
        return SmoothLineEditingProviderFactory.PROVIDER_NAME;
416
    }
417

  
418
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
  <modelVersion>4.0.0</modelVersion>
3
  <parent>
4
    <groupId>org.gvsig</groupId>
5
    <artifactId>org.gvsig.vectorediting.lib.prov</artifactId>
6
    <version>1.0.1</version>
7
  </parent>
8
  <artifactId>org.gvsig.vectorediting.lib.prov.smoothline</artifactId>
9
  <name>org.gvsig.vectorediting.lib.prov.smoothline</name>
10
  <description>Editing provider to smooth lines</description>
11
</project>
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/java/org/gvsig/vectorediting/lib/prov/trimline/TrimLineEditingProviderFactory.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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

  
25
package org.gvsig.vectorediting.lib.prov.trimline;
26

  
27
import org.gvsig.fmap.geom.Geometry;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dynobject.DynClass;
30
import org.gvsig.tools.dynobject.DynObject;
31
import org.gvsig.tools.service.spi.AbstractProviderFactory;
32
import org.gvsig.tools.service.spi.Provider;
33
import org.gvsig.tools.service.spi.ProviderServices;
34
import org.gvsig.vectorediting.lib.api.EditingServiceInfo;
35
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceinfo;
36
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
37

  
38
/**
39
 * @author llmarques
40
 *
41
 */
42
public class TrimLineEditingProviderFactory extends AbstractProviderFactory
43
    implements EditingProviderFactory {
44

  
45
    public static final String PROVIDER_NAME = "modify-trim-line";
46

  
47
    private final static String PROVIDER_DESCRIPTION =
48
        "Trim lines by cutting edges";
49

  
50
    public void initialize() {
51
    }
52

  
53
    public EditingServiceInfo getServiceInfo() {
54
        EditingServiceInfo serviceInfo =
55
            new DefaultEditingServiceinfo(PROVIDER_NAME, PROVIDER_DESCRIPTION,
56
                false, null, new int[] { Geometry.TYPES.CURVE,
57
                    Geometry.TYPES.MULTICURVE });
58

  
59
        return serviceInfo;
60
    }
61

  
62
    @Override
63
    protected Provider doCreate(DynObject parameters, ProviderServices services) {
64
        return new TrimLineEditingProvider(services, parameters);
65
    }
66
    
67
    @Override
68
    public DynObject createParameters() {
69
        DynObject dynobject = super.createParameters();
70
        dynobject.setDynValue(PROVIDER_NAME_FIELD, PROVIDER_NAME);
71
        return dynobject;
72
    }
73

  
74
    @Override
75
    protected DynClass createParametersDynClass() {
76
        DynClass dynclass =
77
            ToolsLocator.getDynObjectManager().createDynClass(PROVIDER_NAME,
78
                PROVIDER_DESCRIPTION);
79

  
80
        dynclass.addDynFieldString(PROVIDER_NAME_FIELD);
81
        dynclass.addDynFieldObject(FEATURE_STORE_FIELD);
82
        dynclass.addDynFieldObject(MAPCONTEXT_FIELD);
83

  
84
        return dynclass;
85
    }
86

  
87
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/java/org/gvsig/vectorediting/lib/prov/trimline/TrimLineEditingLibrary.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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

  
25
package org.gvsig.vectorediting.lib.prov.trimline;
26

  
27
import org.gvsig.fmap.geom.Geometry.TYPES;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.i18n.I18nManager;
30
import org.gvsig.tools.library.AbstractLibrary;
31
import org.gvsig.tools.library.Library;
32
import org.gvsig.tools.library.LibraryException;
33
import org.gvsig.vectorediting.lib.prov.trimline.operation.ArcTrimLineOperation;
34
import org.gvsig.vectorediting.lib.prov.trimline.operation.CurveTrimLineOperation;
35
import org.gvsig.vectorediting.lib.prov.trimline.operation.TrimLineOperationUtils;
36
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator;
37
import org.gvsig.vectorediting.lib.spi.EditingProviderManager;
38

  
39
/**
40
 * @author llmarques
41
 *
42
 */
43
public class TrimLineEditingLibrary extends AbstractLibrary implements Library {
44

  
45
    @Override
46
    protected void doInitialize() throws LibraryException {
47
    }
48

  
49
    @Override
50
    protected void doPostInitialize() throws LibraryException {
51
        EditingProviderManager manager =
52
            EditingProviderLocator.getProviderManager();
53

  
54
        manager.addProviderFactory(new TrimLineEditingProviderFactory());
55

  
56
        manager.registerIcon("vectorediting-tools", "modify-trim-line", this
57
            .getClass().getClassLoader(), this.getClass().getName());
58
        
59
        TrimLineOperationUtils.register(new ArcTrimLineOperation(), TYPES.ARC);
60
        TrimLineOperationUtils.register(new CurveTrimLineOperation(), TYPES.SPLINE);
61
        TrimLineOperationUtils.register(new CurveTrimLineOperation(), TYPES.LINE);
62
        TrimLineOperationUtils.register(new CurveTrimLineOperation(), TYPES.CURVE);
63

  
64
        registerTranslations();
65
    }
66

  
67
    private void registerTranslations() {
68
        I18nManager manager = ToolsLocator.getI18nManager();
69
        manager.addResourceFamily("i18n/text",
70
            this.getClass().getClassLoader(), "modify-trim-line");
71
    }
72

  
73
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.1/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/java/org/gvsig/vectorediting/lib/prov/trimline/operation/TrimLineOperation.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff