Bonjour,

Je suis débutante dans la programmation java (j'utilise eclipse), ainsi dans GeoTools, mais je m'accroche pour les utiliser.

je cherche à utiliser la methode thin plate spline fournis dans la librairie geotools dans mon propre code pour faire un recalage de deux images

voici e code

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2007-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.referencing.operation.builder.algorithm;

import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.geotools.referencing.operation.matrix.GeneralMatrix;
import org.opengis.geometry.DirectPosition;
import org.opengis.geometry.Envelope;

/**
 * Implementation of TPS Interpolation based on thin plate spline (TPS) algorithm
 *
 * @see <A HREF="http://elonen.iki.fi/code/tpsdemo/index.html">Pages about TPS</A>
 *
 * @author jezekjan
 *
 */
public class TPSInterpolation extends AbstractInterpolation {
	
    /**Main matrix (according http://elonen.iki.fi/code/tpsdemo/index.html)*/
    private GeneralMatrix L;

    /**Matrix of target values (according http://elonen.iki.fi/code/tpsdemo/index.html)*/
    private GeneralMatrix V;

    /** Helper constant for generating matrix dimensions*/
    private final int number = super.getPositions().size();
    
    private final GeneralMatrix result;

    public TPSInterpolation(HashMap positions) {
    	super(positions);
    	  L = new GeneralMatrix(number + 3, number + 3);

          fillKsubMatrix();
          fillPsubMatrix();
          fillOsubMatrix();

          L.invert();

          GeneralMatrix V = fillVMatrix(0);
          result = new GeneralMatrix(number + 3, 1);
          result.mul(L, V);
        
    }

    /**
     *
     * @param positions HashMap containing {@link org.opengis.geometry.DirectPosition} as
     * key and value of general parameter as value
     * @param dx Value of step in x direction between generated cells
     * @param dy Value of step in y direction between generated cells
     * @param envelope Envelope that should be filled by generated grid
     */
  /*  public TPSInterpolation(HashMap positions, double dx, double dy, Envelope envelope) {
        super(positions, dx, dy, envelope);

        L = new GeneralMatrix(number + 3, number + 3);

        fillKsubMatrix();
        fillPsubMatrix();
        fillOsubMatrix();

        L.invert();

        GeneralMatrix V = fillVMatrix(0);
        result = new GeneralMatrix(number + 3, 1);
        result.mul(L, V);
    }*/ 
    
   public TPSInterpolation(Map<DirectPosition, Float> positions, int xNumOfCells, int yNumOfCells, Envelope envelope) {
       super(positions, xNumOfCells, yNumOfCells, envelope);

    L = new GeneralMatrix(number + 3, number + 3);

    fillKsubMatrix();
    fillPsubMatrix();
    fillOsubMatrix();

    L.invert();

    GeneralMatrix V = fillVMatrix(0);
    result = new GeneralMatrix(number + 3, 1);
    result.mul(L, V);
    }
    public float getValue(DirectPosition p) {
        // TODO Auto-generated method stub
        return calculateTPSFunction(result, p);
    }

    /**
     * Computes target point using TPS formula.
     * @param v matrix of useful coefficients
     * @param p position where we want the value
     * @return calculated shift
     */
    private float calculateTPSFunction(GeneralMatrix v, DirectPosition p) {
        double a1 = v.getElement(v.getNumRow() - 3, 0);
        double a2 = v.getElement(v.getNumRow() - 2, 0);
        double a3 = v.getElement(v.getNumRow() - 1, 0);

        float result;
        double sum = 0;

        Iterator iter = getPositions().keySet().iterator();

        for (int i = 0; i < (v.getNumRow() - 3); i++) {
            double dist = ((Point2D) p).distance((Point2D) iter.next());

            sum = sum + (v.getElement(i, 0) * functionU(dist));
        }

        result = (float) (a1 + (a2 * p.getOrdinate(0)) + (a3 * p.getOrdinate(1)) + sum);

        return result;
    }

    /**
     * Calculates U function for distance
     * @param distance distance
     * @return log(distance)*distance<sub>2</sub> or 0 if distance = 0
     */
    private double functionU(double distance) {
        if (distance == 0) {
            return 0;
        }

        return distance * distance * Math.log(distance);
    }

    /**
     * Calculates U function where distance = ||p_i, p_j|| (from source points)
     * @param p_i p_i
     * @param p_j p_j
     * @return log(distance)*distance<sub>2</sub> or 0 if distance = 0
     */
    private double calculateFunctionU(DirectPosition p_i, DirectPosition p_j) {
        double distance = ((Point2D) p_i).distance((Point2D) p_j);

        return functionU(distance);
    }

    /**
     * Fill K submatrix (<a href="http://elonen.iki.fi/code/tpsdemo/index.html"> see more here</a>)
     */
    private void fillKsubMatrix() {
        double alfa = 0;

        Object[] positions = getPositions().keySet().toArray();

        for (int i = 0; i < number; i++) {
            for (int j = i + 1; j < number; j++) {
                double u = calculateFunctionU((DirectPosition) positions[i],
                        (DirectPosition) positions[j]);
                L.setElement(i, j, u);
                L.setElement(j, i, u);
                alfa = alfa + (u * 2); // same for upper and lower part
            }
        }

        alfa = alfa / (number * number);
    }

    /**
     * Fill L submatrix (<a href="http://elonen.iki.fi/code/tpsdemo/index.html"> see more here</a>)
     */
    private void fillPsubMatrix() {
        Iterator iter = getPositions().keySet().iterator();

        for (int i = 0; i < number; i++) {
            L.setElement(i, i, 0);

            DirectPosition source = (DirectPosition) iter.next();

            L.setElement(i, number + 0, 1);
            L.setElement(i, number + 1, source.getCoordinate()[0]);
            L.setElement(i, number + 2, source.getCoordinate()[1]);

            L.setElement(number + 0, i, 1);
            L.setElement(number + 1, i, source.getCoordinate()[0]);
            L.setElement(number + 2, i, source.getCoordinate()[1]);
        }
    }

    /**
     * Fill O submatrix (<a href="http://elonen.iki.fi/code/tpsdemo/index.html"> see more here</a>)
     */
    private void fillOsubMatrix() {
        for (int i = number; i < (number + 3); i++) {
            for (int j = number; j < (number + 3); j++) {
                L.setElement(i, j, 0);
            }
        }
    }

    /**
     * Fill V matrix (matrix of target values)
     * @param dim 0 for dx, 1 for dy.
     * @return V Matrix
     */
    private GeneralMatrix fillVMatrix(int dim) {
        V = new GeneralMatrix(number + 3, 1);

        Iterator<DirectPosition> iter = getPositions().keySet().iterator();

        for (int i = 0; i < number; i++) {           
            V.setElement(i, 0, getPositions().get(iter.next()));        }

        V.setElement(V.getNumRow() - 3, 0, 0);
        V.setElement(V.getNumRow() - 2, 0, 0);
        V.setElement(V.getNumRow() - 1, 0, 0);

        return V;
    }
}
pour faire appel à cette class j'ai utilisé les lignes suivantes( je les trouve dans une forum de geotools lien)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
CoordinateReferenceSystem crs = DefaultEngineeringCRS.CARTESIAN_2D; 
				// Define the Envelope for our work; this will be the bounds of the final interpolation
				DirectPosition n = new DirectPosition2D(crs,   0.0,   0.0);
				DirectPosition x = new DirectPosition2D(crs, 100.0, 100.0);
				GeneralDirectPosition min = new GeneralDirectPosition( n );
				GeneralDirectPosition max = new GeneralDirectPosition( x );
				Envelope env = new GeneralEnvelope(min,max); 
				// Generate some known points to root the interpolation
				DirectPosition a = new DirectPosition2D(crs,13,85);
				DirectPosition b = new DirectPosition2D(crs,14,15);
				DirectPosition c = new DirectPosition2D(crs,45,78);
				DirectPosition d = new DirectPosition2D(crs,95,28); 
				// Define at each point the values to be interpolated; we do this in a HashMap
				HashMap pointsAndValues = new HashMap();
				pointsAndValues.put(a,  6);
				pointsAndValues.put(b,  1);
				pointsAndValues.put(c, -9);
				pointsAndValues.put(d,  7);  
				//now we can construct the Interpolation Object
				TPSInterpolation interp = new TPSInterpolation(pointsAndValues, 1, 1, env);   
				// We can create and show a coverage image of the interpolation within the Envelope
				GridCoverageFactory gcf = new GridCoverageFactory();
				gcf.create("Intepolated Coverage",  interp.get2DGrid(), env).show(); 
				// We can also generate an interpolated value at any DirectPosition
				float myValue = interp.getValue(new DirectPosition2D(crs,12.34,15.123));
j'ai eu l'erreur suivante :

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float
dans la ligne que je la met en couleur

Quelqu'un aurait-il une piste de réponde ???

Merci d'avance et bonne journée !