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 @ 2314
History | View | Annotate | Download (4.66 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 |
/*
|
51 |
* (non-Javadoc)
|
52 |
* @see org.gvsig.fmap.dal.coverage.util.CRSUtils#setCRSFactory(org.cresques.cts.ICRSFactory)
|
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 depende de como se haya
|
60 |
* usado.
|
61 |
*
|
62 |
* @param code
|
63 |
* @return
|
64 |
*/
|
65 |
private IProjection getCRS(String code) { |
66 |
if (factory == null) |
67 |
factory = CRSFactory.cp;//new ProjectionPool();
|
68 |
return factory.get(code);
|
69 |
} |
70 |
|
71 |
/*
|
72 |
* (non-Javadoc)
|
73 |
* @see org.gvsig.fmap.dal.coverage.util.CRSUtils#convertWktToIProjection(java.lang.String)
|
74 |
*/
|
75 |
public IProjection convertWktToIProjection(String wkt) { |
76 |
if (wkt == null || wkt.equals("")) |
77 |
return null; |
78 |
|
79 |
String code = null; |
80 |
String name = null; |
81 |
|
82 |
OGRSpatialReference oSRSSource = new OGRSpatialReference();
|
83 |
try {
|
84 |
OGRSpatialReference.importFromWkt(oSRSSource, wkt); |
85 |
|
86 |
code = oSRSSource.getAuthorityCode("PROJCS");
|
87 |
if (code == null) |
88 |
code = oSRSSource.getAuthorityCode("GEOGCS");
|
89 |
name = oSRSSource.getAuthorityName("PROJCS");
|
90 |
if (name == null) |
91 |
name = oSRSSource.getAuthorityName("GEOGCS");
|
92 |
try {
|
93 |
if(name != null && code != null) |
94 |
return getCRS(name + ":" + code); |
95 |
} catch (NumberFormatException ex) { |
96 |
return null; |
97 |
} |
98 |
} catch (OGRException e) {
|
99 |
LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo EPSG", e);
|
100 |
} catch (CrsGdalException e) {
|
101 |
LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo EPSG", e);
|
102 |
} |
103 |
|
104 |
return null; |
105 |
} |
106 |
|
107 |
/*
|
108 |
* (non-Javadoc)
|
109 |
* @see org.gvsig.fmap.dal.coverage.util.CRSUtils#convertIProjectionToWkt(org.cresques.cts.IProjection)
|
110 |
*/
|
111 |
public String convertIProjectionToWkt(IProjection projection) { |
112 |
if(projection == null) |
113 |
return null; |
114 |
OGRSpatialReference oSRSSource = new OGRSpatialReference();
|
115 |
|
116 |
String code = projection.getAbrev();
|
117 |
String name = code.substring(0, code.indexOf(":")); |
118 |
code = code.substring(code.indexOf(":") + 1); |
119 |
|
120 |
try {
|
121 |
do
|
122 |
if (name.equals("EPSG")) { |
123 |
OGRSpatialReference.importFromEPSG(oSRSSource, Integer.parseInt(code));
|
124 |
break;
|
125 |
} |
126 |
while (false); |
127 |
return OGRSpatialReference.exportToWkt(oSRSSource);
|
128 |
} catch (CrsGdalException e) {
|
129 |
LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo WKT", e);
|
130 |
} catch (NumberFormatException e) { |
131 |
LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo WKT", e);
|
132 |
} catch (OGRException e) {
|
133 |
LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo WKT", e);
|
134 |
} |
135 |
return null; |
136 |
} |
137 |
} |