Statistics
| Revision:

gvsig-raster / org.gvsig.raster.gdal / branches / org.gvsig.raster.gdal_dataaccess_refactoring / org.gvsig.raster.gdal.io / src / main / java / org / gvsig / raster / gdal / util / DefaultCRSUtils.java @ 2420

History | View | Annotate | Download (4.28 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.gvsig.fmap.crs.CRSFactory;
27
import org.gvsig.fmap.dal.coverage.util.CRSUtils;
28
import org.gvsig.jogr.CrsGdalException;
29
import org.gvsig.jogr.OGRException;
30
import org.gvsig.jogr.OGRSpatialReference;
31
import org.slf4j.LoggerFactory;
32
/**
33
 * Esta clase se encarga de hacer la conversion entre Wkt e IProjection.
34
 *
35
 * El uso se hace mediante dos llamadas estaticas que son: convertIProjectionToWkt y
36
 * convertWktToIProjection.
37
 *
38
 * Antes de usarlos, hay que saber si tenemos acceso a gvSIG e intentar coger el factory desde all?.
39
 * Ya que su uso consume tiempo y espacio de memoria y es preferible reaprovechar ese objeto ya
40
 * creado. Esto se hace con setCRSFactory.
41
 *
42
 * En caso de no asignarse el ya existente, el crear? uno interno y lo dejara en una variable estatica.
43
 *
44
 * @version 11/07/2008
45
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
46
 */
47
public class DefaultCRSUtils implements CRSUtils {
48
        public ICRSFactory factory = null;
49

    
50
        public void setCRSFactory(ICRSFactory factory) {
51
                this.factory = factory;
52
        }
53

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

    
67
        public IProjection convertWktToIProjection(String wkt) {
68
                if (wkt == null || wkt.equals(""))
69
                        return null;
70

    
71
                String code = null;
72
                String name = null;
73

    
74
                OGRSpatialReference oSRSSource = new OGRSpatialReference();
75
                try {
76
                        OGRSpatialReference.importFromWkt(oSRSSource, wkt);
77

    
78
                        code = oSRSSource.getAuthorityCode("PROJCS");
79
                        if (code == null)
80
                                code = oSRSSource.getAuthorityCode("GEOGCS");
81
                        name = oSRSSource.getAuthorityName("PROJCS");
82
                        if (name == null)
83
                                name = oSRSSource.getAuthorityName("GEOGCS");
84
                        try {
85
                                if(name != null && code != null)
86
                                        return getCRS(name + ":" + code);
87
                        } catch (NumberFormatException ex) {
88
                                return null;
89
                        }
90
                } catch (OGRException e) {
91
                        LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo EPSG", e);
92
                } catch (CrsGdalException e) {
93
                        LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo EPSG", e);
94
                }
95

    
96
                return null;
97
        }
98

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

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

    
108
                try {
109
                        do
110
                                if (name.equals("EPSG")) {
111
                                        OGRSpatialReference.importFromEPSG(oSRSSource, Integer.parseInt(code));
112
                                        break;
113
                                }
114
                        while (false);
115
                        return OGRSpatialReference.exportToWkt(oSRSSource);
116
                } catch (CrsGdalException e) {
117
                        LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo WKT", e);
118
                } catch (NumberFormatException e) {
119
                        LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo WKT", e);
120
                } catch (OGRException e) {
121
                        LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo WKT", e);
122
                }
123
                return null;
124
        }
125
}