Statistics
| Revision:

gvsig-raster / org.gvsig.raster.gdal / trunk / org.gvsig.raster.gdal / org.gvsig.raster.gdal.io / src / main / java / org / gvsig / raster / gdal / util / DefaultCRSUtils.java @ 4182

History | View | Annotate | Download (3.74 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22
package org.gvsig.raster.gdal.util;
23

    
24
import org.cresques.cts.ICRSFactory;
25
import org.cresques.cts.IProjection;
26
import org.gdal.osr.SpatialReference;
27

    
28
import org.gvsig.fmap.crs.CRSFactory;
29
import org.gvsig.fmap.dal.coverage.util.CRSUtils;
30

    
31
/**
32
 * Esta clase se encarga de hacer la conversion entre Wkt e IProjection.
33
 *
34
 * El uso se hace mediante dos llamadas estaticas que son:
35
 * convertIProjectionToWkt y
36
 * convertWktToIProjection.
37
 *
38
 * Antes de usarlos, hay que saber si tenemos acceso a gvSIG e intentar coger el
39
 * factory desde all?.
40
 * Ya que su uso consume tiempo y espacio de memoria y es preferible
41
 * reaprovechar ese objeto ya
42
 * creado. Esto se hace con setCRSFactory.
43
 *
44
 * En caso de no asignarse el ya existente, el crear? uno interno y lo dejara en
45
 * una variable estatica.
46
 *
47
 * @version 11/07/2008
48
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
49
 */
50
public class DefaultCRSUtils implements CRSUtils {
51

    
52
    private ICRSFactory factory = null;
53

    
54
    public void setCRSFactory(ICRSFactory factory) {
55
        this.factory = factory;
56
    }
57

    
58
    /**
59
     * Devuelve el CRSFactory asignado desde fuera o creado desde dentro, todo
60
     * depende de como se haya
61
     * usado.
62
     *
63
     * @param code
64
     * @return
65
     */
66
    private IProjection getCRS(String code) {
67
        if (factory == null)
68
            factory = CRSFactory.getCRSFactory(); // new ProjectionPool();
69
        return factory.get(code);
70
    }
71

    
72
    public IProjection convertWktToIProjection(String wkt) {
73
        if (wkt == null || wkt.equals(""))
74
            return null;
75

    
76
        String code = null;
77
        String name = null;
78

    
79
        SpatialReference oSRSSource = new SpatialReference();
80

    
81
        oSRSSource.ImportFromWkt(wkt);
82

    
83
        code = oSRSSource.GetAuthorityCode("PROJCS");
84
        if (code == null)
85
            code = oSRSSource.GetAuthorityCode("GEOGCS");
86
        name = oSRSSource.GetAuthorityName("PROJCS");
87
        if (name == null)
88
            name = oSRSSource.GetAuthorityName("GEOGCS");
89
        try {
90
            if (name != null && code != null)
91
                return getCRS(name + ":" + code);
92
        } catch (NumberFormatException ex) {
93
            return null;
94
        }
95

    
96
        return null;
97
    }
98

    
99
    public String convertIProjectionToWkt(IProjection projection) {
100
        if (projection == null)
101
            return null;
102
        SpatialReference oSRSSource = new SpatialReference();
103

    
104
        String code = projection.getAbrev();
105
        String name = code.substring(0, code.indexOf(":"));
106
        code = code.substring(code.indexOf(":") + 1);
107

    
108
        do
109
            if (name.equals("EPSG")) {
110
                oSRSSource.ImportFromEPSG(Integer.parseInt(code));
111
                break;
112
            }
113
        while (false);
114
        return oSRSSource.ExportToWkt();
115
    }
116
}