root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / org / gvsig / crs / CrsFactory.java @ 320
History | View | Annotate | Download (9.25 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.crs; |
20 |
|
21 |
import java.io.File; |
22 |
import java.util.TreeMap; |
23 |
|
24 |
import org.cresques.cts.ICRSFactory; |
25 |
import org.cresques.cts.IProjection; |
26 |
import org.gvsig.crs.repository.EpsgRepository; |
27 |
import org.gvsig.crs.repository.EpsgRepositoryGT; |
28 |
import org.gvsig.crs.repository.EsriRepository; |
29 |
import org.gvsig.crs.repository.EsriRepositoryGT; |
30 |
import org.gvsig.crs.repository.ICrsRepository; |
31 |
import org.gvsig.crs.repository.Iau2000Repository; |
32 |
import org.gvsig.crs.repository.Iau2000RepositoryGT; |
33 |
import org.gvsig.crs.repository.NoAuthRepository; |
34 |
import org.gvsig.crs.repository.NoAuthRepositoryGT; |
35 |
import org.gvsig.crs.repository.UsrRepository; |
36 |
import org.gvsig.crs.repository.UsrRepositoryGT; |
37 |
import org.gvsig.tools.dispose.Disposable; |
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
/**
|
42 |
* Clase que consigue el CRS a trav?s del c?digo de la EPSG o de
|
43 |
* la cadena WKT
|
44 |
*
|
45 |
* @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
46 |
*
|
47 |
*/
|
48 |
public class CrsFactory implements ICRSFactory { |
49 |
|
50 |
private static Logger logger = LoggerFactory.getLogger(CrsFactory.class); |
51 |
static TreeMap data = new TreeMap(); |
52 |
|
53 |
private static File databaseFolder = null; |
54 |
private static File projLibFolder = null; |
55 |
private static boolean enableMemoryCache = true; |
56 |
private static File epsgDatabaseFile = null; |
57 |
|
58 |
public CrsFactory() {
|
59 |
} |
60 |
|
61 |
/**
|
62 |
* Obtiene un CRS a partir de su c?digo (p.e. EPSG:23030).
|
63 |
*
|
64 |
* @param code
|
65 |
* @return
|
66 |
* @throws CrsException
|
67 |
*/
|
68 |
public ICrs getCRS(String code) throws CrsException { |
69 |
return getCRS(code,null,null); |
70 |
} |
71 |
|
72 |
public ICrs getCRS(String code, String theSourceParams, String theTargetParams) throws CrsException { |
73 |
|
74 |
String keyOfCache = getKeyOfCache(code,theSourceParams,theTargetParams);
|
75 |
if (enableMemoryCache && data.containsKey(keyOfCache)) {
|
76 |
return (ICrs) data.get(keyOfCache);
|
77 |
} |
78 |
String sourceParams = theSourceParams;
|
79 |
String targetParams = theTargetParams;
|
80 |
String crsCode = code;
|
81 |
|
82 |
ICrs crs = null;
|
83 |
|
84 |
if ( code.indexOf(":", code.indexOf(":") + 1) >= 0 ) { |
85 |
crsCode = code.substring(0, code.indexOf(":", code.indexOf(":") + 1)); |
86 |
if ( code.indexOf("@") == -1 ) { |
87 |
crsCode = crsCode.substring(0, crsCode.indexOf(",")); |
88 |
} else {
|
89 |
sourceParams = code.substring(code.indexOf("@") + 1, code.lastIndexOf("@")); |
90 |
targetParams = code.substring(code.lastIndexOf("@") + 1); |
91 |
|
92 |
if ( sourceParams.equals("") ) { |
93 |
sourceParams = null;
|
94 |
} else if ( targetParams.equals("1") ) { // Compativilidad con versiones de libJCrs sin soporte para transf. compuestas. |
95 |
targetParams = sourceParams; |
96 |
sourceParams = "";
|
97 |
} |
98 |
if ( targetParams.equals("") || targetParams.equals("0") ) // Compativilidad con versiones de libJCrs sin soporte para transf. compuestas. |
99 |
{ |
100 |
targetParams = null;
|
101 |
} |
102 |
} |
103 |
} |
104 |
crs = getSimpleCRS(crsCode); |
105 |
if( sourceParams!=null || targetParams!=null ) { |
106 |
crs.setTransformationParams(sourceParams, targetParams); |
107 |
} |
108 |
|
109 |
if( enableMemoryCache ) {
|
110 |
keyOfCache = getKeyOfCache(crsCode,sourceParams,targetParams); |
111 |
data.put(keyOfCache, crs); |
112 |
} |
113 |
|
114 |
|
115 |
return crs;
|
116 |
} |
117 |
|
118 |
private String getKeyOfCache(String code, String theSourceParams, String theTargetParams) { |
119 |
String keyOfCache = code;
|
120 |
if( theSourceParams!=null ) { |
121 |
keyOfCache += ":proj@" + theSourceParams + "@"; |
122 |
if( theTargetParams!=null ) { |
123 |
keyOfCache += theTargetParams; |
124 |
} |
125 |
} |
126 |
return keyOfCache;
|
127 |
} |
128 |
|
129 |
private ICrs getSimpleCRS(String code) throws CrsException { |
130 |
|
131 |
String repoId = ""; |
132 |
String crsCode = ""; |
133 |
ICrs crs = null;
|
134 |
ICrsRepository repo = null;
|
135 |
|
136 |
try {
|
137 |
repoId = code.substring(0, code.indexOf(":")); |
138 |
crsCode = code.substring(code.indexOf(":") + 1); |
139 |
|
140 |
if ( repoId.equals("EPSG") ) { |
141 |
repo = new EpsgRepositoryGT();
|
142 |
crs = repo.getCrs(crsCode); |
143 |
if ( crs == null ) { |
144 |
repo = new EpsgRepository();
|
145 |
crs = repo.getCrs(crsCode); |
146 |
} |
147 |
} else if ( repoId.equals("IAU2000") ) { |
148 |
repo = new Iau2000RepositoryGT();
|
149 |
crs = repo.getCrs(crsCode); |
150 |
if ( crs == null ) { |
151 |
repo = new Iau2000Repository();
|
152 |
crs = repo.getCrs(crsCode); |
153 |
} |
154 |
} else if ( repoId.equals("CRS") ) { |
155 |
repo = new NoAuthRepositoryGT();
|
156 |
crs = repo.getCrs(crsCode); |
157 |
if ( crs == null ) { |
158 |
repo = new NoAuthRepository();
|
159 |
crs = repo.getCrs(crsCode); |
160 |
} |
161 |
} else if ( repoId.equals("ESRI") ) { |
162 |
repo = new EsriRepositoryGT();
|
163 |
crs = repo.getCrs(crsCode); |
164 |
if ( crs == null ) { |
165 |
repo = new EsriRepository();
|
166 |
crs = repo.getCrs(crsCode); |
167 |
} |
168 |
} else if ( repoId.equals("USR") ) { |
169 |
repo = new UsrRepositoryGT();
|
170 |
crs = repo.getCrs(crsCode); |
171 |
if ( crs == null ) { |
172 |
repo = new UsrRepository();
|
173 |
crs = repo.getCrs(crsCode); |
174 |
} |
175 |
} |
176 |
if ( crs == null ) { |
177 |
logger.debug("Can't find CRS '" + code + "' in available repositories."); |
178 |
} else {
|
179 |
logger.debug("Found CRS '" + code + "' in repository '" + |
180 |
(repo == null ? "unknow" : (repo.getClass().getSimpleName())) + "'."); |
181 |
} |
182 |
return crs;
|
183 |
} finally {
|
184 |
if( repo instanceof Disposable ) { |
185 |
((Disposable)repo).dispose(); |
186 |
} |
187 |
} |
188 |
} |
189 |
|
190 |
|
191 |
/**
|
192 |
*
|
193 |
* @param epsg_code
|
194 |
* @param code
|
195 |
* @return
|
196 |
* @throws CrsException
|
197 |
*/
|
198 |
public ICrs getCRS(int epsg_code, String code) throws CrsException { |
199 |
Crs crs = new Crs(epsg_code, code);
|
200 |
return crs;
|
201 |
} |
202 |
|
203 |
/**
|
204 |
*
|
205 |
* @param epsg_code
|
206 |
* @param code
|
207 |
* @param params
|
208 |
* @return
|
209 |
* @throws CrsException
|
210 |
*/
|
211 |
public ICrs getCRS(int epsg_code, String code, String params) throws CrsException { |
212 |
Crs crs = new Crs(epsg_code, code, params);
|
213 |
return crs;
|
214 |
} |
215 |
|
216 |
/**
|
217 |
*
|
218 |
*/
|
219 |
public IProjection get(String name) { |
220 |
try {
|
221 |
return getCRS(name,null,null); |
222 |
} catch (CrsException e) {
|
223 |
logger.error("Can't cget CRS name '" + name + "'.", e); |
224 |
} |
225 |
return null; |
226 |
} |
227 |
|
228 |
/**
|
229 |
*
|
230 |
*/
|
231 |
public boolean doesRigurousTransformations() { |
232 |
return true; |
233 |
} |
234 |
|
235 |
public static File getDataBaseFolder() { |
236 |
return databaseFolder;
|
237 |
} |
238 |
|
239 |
public static void setDataBaseFolder(File folder) { |
240 |
databaseFolder = folder; |
241 |
} |
242 |
|
243 |
public static File getProjLibFolder() { |
244 |
return projLibFolder;
|
245 |
} |
246 |
|
247 |
public static void setProjLibFolder(File folder) { |
248 |
projLibFolder = folder; |
249 |
} |
250 |
|
251 |
public static void setEnableMemoryCacheOfCRS(boolean enableMemoryCache) { |
252 |
CrsFactory.enableMemoryCache = enableMemoryCache; |
253 |
} |
254 |
|
255 |
public static boolean isEnableMemoryCacheOfCRS() { |
256 |
return enableMemoryCache;
|
257 |
} |
258 |
|
259 |
public static File getEpsgDatabaseFile() { |
260 |
if ( epsgDatabaseFile == null ) { |
261 |
epsgDatabaseFile = new File(getDataBaseFolder(), "EPSG.sql"); |
262 |
} |
263 |
return epsgDatabaseFile;
|
264 |
|
265 |
} |
266 |
|
267 |
public static void setEpsgDatabaseFile(File databaseFile) { |
268 |
epsgDatabaseFile = databaseFile; |
269 |
if ( !epsgDatabaseFile.isAbsolute() ) {
|
270 |
epsgDatabaseFile = new File(getDataBaseFolder(), epsgDatabaseFile.getPath()); |
271 |
} |
272 |
} |
273 |
} |