Revision 436 org.gvsig.projection.jcrs/trunk/org.gvsig.projection.jcrs/org.gvsig.projection.jcrs.lib/src/main/java/org/gvsig/crs/COperation.java

View differences:

COperation.java
43 43
import java.awt.geom.Point2D;
44 44
import java.awt.geom.Rectangle2D;
45 45

  
46
import org.cresques.cts.CoordTransRuntimeException;
46 47
import org.cresques.cts.ICoordTrans;
47 48
import org.cresques.cts.IProjection;
48 49
import org.gdal.gdal.gdal;
......
167 168

  
168 169
        double[] transformed = { 0, 0, 0 };
169 170
        initializeCoordinateTransformation();
170
        if (coordinateTransformation != null) {
171
            transformed = coordinateTransformation.TransformPoint(x, y);
171
        gdal.PushErrorHandler( "CPLQuietErrorHandler" );
172
        gdal.ErrorReset();
173
        if (coordinateTransformation != null) { // && inverseCoordinateTransformation != null) {
174
            coordinateTransformation.TransformPoint(transformed, x, y, 0);
172 175
        }
173 176

  
177
        gdal.PopErrorHandler();
174 178
        errno = gdal.GetLastErrorNo();
175 179
        msg = gdal.GetLastErrorMsg();
176 180
        errtype = gdal.GetLastErrorType();
177 181

  
178 182
        Point2D.Double result = null;
179
        if (errno != 0 && errtype != 0) {
180
            //FIXME:
183
        if (errno != 0 || errtype != 0 || !msg.isEmpty()) {
184
            // FIXME:
181 185
            // gdal no devuelve m?s de 20 errores por transformaci?n,
182 186
            // si queremos de los devuelva todos tenemos que anular la
183 187
            // transformaci?n cada vez que se produce un error
184
//            coordinateTransformation = null;
185
            // Adem?s, no podemos hacer nada por los puntos que devuelve
186
            // la transformaci?n cuando ha tenido un error.
187
            // Lo suyo ser?a elevar una excepci?n y quien la recoja que decida qu? dabe hacer con el punto,
188
            // por ejemplo, si se est? dibujando una geometr?a, tal vez, no habr?a que dibujarla o saltarse los
189
            // puntos que no se pueden transformar.
188
            if (msg
189
                .equalsIgnoreCase("Reprojection failed, err = -14, further errors will be supressed on the transform object.")) {
190
                resetCoordinateTransformation();
191
            }
190 192

  
191
            gdal.ErrorReset();
192

  
193
            throw new CoordTransRuntimeException(sourceCrs, targetCrs, x, y);
193 194
        }
194 195
        result = new Point2D.Double(transformed[0], transformed[1]);
195 196
        return result;
......
197 198

  
198 199
    private void initializeCoordinateTransformation() {
199 200
        if (coordinateTransformation == null) {
201

  
202
            int errno = 0;
203
            String msg = "";
204
            int errtype = 0;
205

  
206
            gdal.ErrorReset();
207
            gdal.PushErrorHandler( "CPLQuietErrorHandler" );
200 208
            coordinateTransformation =
201 209
                CoordinateTransformation.CreateCoordinateTransformation(source.getSpatialReference(),
202 210
                    target.getSpatialReference());
211
            gdal.PopErrorHandler();
212
            errno = gdal.GetLastErrorNo();
213
            msg = gdal.GetLastErrorMsg();
214
            errtype = gdal.GetLastErrorType();
215
            if (errno != 0 || errtype != 0 || !msg.isEmpty()) {
216
                logger.warn("Can't initialize coordinate transformation from "+sourceCrs.getAbrev()+" to "+targetCrs.getAbrev()+" "+errno + " "+errtype+" "+msg+".");
217
            }
203 218
        }
204 219
    }
205 220

  
221
    private void resetCoordinateTransformation() {
222
        coordinateTransformation = null;
223
        initializeCoordinateTransformation();
224
    }
225

  
206 226
    public IProjection getPOrig() {
207 227
        return (IProjection) sourceCrs;
208 228
    }
......
211 231
        return (IProjection) targetCrs;
212 232
    }
213 233

  
214
    public Point2D convert(Point2D ptOrig, Point2D ptDest) {
234
    public Point2D convert(Point2D ptOrig, Point2D ptDest) throws CoordTransRuntimeException {
215 235
        try {
216 236
            ptDest = operate(ptOrig);
217 237
        } catch (CrsException e) {
......
224 244
        return ptDest;
225 245
    }
226 246

  
227
    public Rectangle2D convert(Rectangle2D rect) {
247
    public Rectangle2D convert(Rectangle2D rect) throws CoordTransRuntimeException {
228 248
        Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY());
229 249
        Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY());
230 250
        Point2D pt3 = new Point2D.Double(rect.getMinX(), rect.getMaxY());

Also available in: Unified diff