Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-gdal / src / main / java / org / gvsig / jogr / OGRSpatialReference.java @ 20367

History | View | Annotate | Download (13.4 KB)

1
/**********************************************************************
2
 * $Id: OGRSpatialReference.java 15690 2007-10-31 10:28:41Z nbrodin $
3
 *
4
 * Name:     OGRSpatialReference.java
5
 * Project:  JGDAL. Interface java to gdal (Frank Warmerdam).
6
 * Purpose:   
7
 * Author:   Nacho Brodin, brodin_ign@gva.es
8
 *
9
 **********************************************************************/
10
/*Copyright (C) 2004  Nacho Brodin <brodin_ign@gva.es>
11

12
 This program is free software; you can redistribute it and/or
13
 modify it under the terms of the GNU General Public License
14
 as published by the Free Software Foundation; either version 2
15
 of the License, or (at your option) any later version.
16

17
 This program is distributed in the hope that it will be useful,
18
 but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 GNU General Public License for more details.
21

22
 You should have received a copy of the GNU General Public License
23
 along with this program; if not, write to the Free Software
24
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
 */
26

    
27
package org.gvsig.jogr;
28

    
29

    
30

    
31
//import es.gva.cit.wrappergdal.OGRException;
32

    
33
/** 
34
 * Representa un sistema de referencia espacial OpenGIS.
35
 * 
36
 * @author Nacho Brodin <brodin_ign@gva.es>.<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
37
 * @version 0.0
38
 * @link http://www.gvsig.gva.es
39
 */
40

    
41
public class OGRSpatialReference extends JNIBase {
42
        
43
        //private native int setUTMNat(long cPtr, int zona, int norte_sur);
44
        //private native int setWellKnownGeogCSNat(long cPtr, String cs);
45
        //private native long OGRSpatialReferenceNat();
46
        //private native String exportToWktNat(long cPtr);
47
        private native void OSRDestroySpatialReferenceNat(long cPtr);
48
        private native int getUTMZoneNat(long cPtr);
49
        private native int getHemisphereNat(long cPtr);
50
        private native int setFromUserInputNat(long cPtr, String a_srs);
51
        
52
        private native long OGRSpatialReferenceNat() throws CrsGdalException;
53
        private native void OGRDestroySpatialReferenceNat(long crs);
54
        
55
        protected static native String exportToProj4Nat(long cPtr);
56
        protected static native String exportToWktNat(long cPtr);
57
        protected static native int importFromWktNat(long cPtr, String cadenas);
58
        protected static native int setUTMNat(long cPtr, int zona, int norte_sur);
59
        protected static native int setWellKnownGeogCSNat(long cPtr, String cs);
60
        protected static native int importFromEPSGNat(long cPtr, int cod);
61
        protected static native int importFromProj4Nat(long cPtr, String cs);
62
        protected static native int importFromPCINat(long cPtr,String cod, String cs,double[] coord);
63
        protected static native int importFromUSGSNat(long cPtr, long code, long zone, double[] params, long datum);
64
        protected static native int importFromESRINat(long cPtr, String cadenas);
65
        protected static native boolean isGeographicNat(long cPtr);
66
        protected static native String getAuthorityCodeNat(long cPtr, String targetKey);
67
        protected static native String getAuthorityNameNat(long cPtr, String targetKey);
68
        
69
        /**
70
         *Constructor a partir de la direcci?n de memoria 
71
         */
72
        
73
        public OGRSpatialReference(long cPtr){
74
                this.cPtr=cPtr;
75
        }
76
        
77
        /**
78
         * Constructor generico 
79
         */
80
        
81
        public OGRSpatialReference(){
82
                try{
83
                        cPtr = OGRSpatialReferenceNat();
84
                }catch(CrsGdalException exp){
85
                        System.out.println(exp.getStrError());
86
                }
87
        }
88
        
89
        /**
90
         * Escribe la zona UTM pasada por par?metro
91
         * @param zona        Zona UTM
92
         * @param norte_sur        TRUE hemisferio norte y FALSE hemisferio sur
93
         * @throws OGRException
94
         */
95
        
96
        public void setUTM(int zona, boolean norte_sur)throws OGRException {
97
                int ns=-1;
98
                if(norte_sur)ns=1;
99
                else ns=0;
100
                
101
                if(cPtr == 0)
102
                        throw new OGRException("Fallo al acceder al dato.");
103
                
104
                if ((zona <= 0) || (zona > 60))
105
                        throw new OGRException("Zona incorrecta");
106
                
107
                int res = setUTMNat(cPtr, zona, ns);
108
                
109
                if(res < 0)
110
                        throw new OGRException("Error en setUTM(). No se ha podido asignar la zona especificada.");
111
        }
112
        
113
        /**
114
         * Lee la zona UTM 
115
         * @return Zona UTM
116
         * @throws OGRException
117
         */
118
        
119
        public int getUTMZone()throws OGRException {
120
                if(cPtr == 0)
121
                        throw new OGRException("Fallo al acceder al dato.");
122
                
123
                int res = getUTMZoneNat(cPtr);
124
                
125
                if(res < 0)
126
                        throw new OGRException("Error en getUTMZone(). Valor de zona devuelto erroneo.");
127
                
128
                return res;        
129
        }
130
        
131
        /**
132
         * Obtiene el hemisferio
133
         * @return TRUE hemisferio norte y FALSE hemisferio sur
134
         * @throws OGRException
135
         */
136
        
137
        public boolean getHemisphere()throws OGRException {
138
                if(cPtr == 0)
139
                        throw new OGRException("Fallo al acceder al dato.");
140
                
141
                int hemis = getHemisphereNat(cPtr);
142
                                         
143
                if(hemis < 0)
144
                        throw new OGRException("Error en getHemisphere(). Valor de hemisferio devuelto erroneo.");
145
                
146
                if(hemis == 1)return true;
147
                else return false;
148
        }
149
        
150
        /**
151
         *Asigna las coordenadas geogr?ficas
152
         *@param cs        Coordenadas geograficas soportadas:<BR>
153
         *<UL>
154
         *<LI>WGS84</LI>
155
         *<LI>WGS72</LI>
156
         *<LI>NAD27</LI>
157
         *<LI>NAD83</LI>
158
         *<LI>EPSG:n</LI>
159
         *</UL>
160
         *@throws OGRException
161
         */
162
        
163
        public void setWellKnownGeogCS(String cs)throws OGRException {
164
                if(cPtr == 0)
165
                        throw new OGRException("Fallo al acceder al dato.");
166
                
167
                if ((cs == null) || (cs.equals("")))
168
                        throw new OGRException("Parametro incorrecto en OGRSpatialReference.setWllKnownGeoCS");
169
                
170
                int res = setWellKnownGeogCSNat(cPtr, cs);
171
                
172
                if(res < 0)
173
                        throw new OGRException("Error en setWellKnownGeoCS(). No se ha podido asignar el sistema de coordenadas especificado.");
174
        }
175
        
176
        /**
177
         * Devuelve la codificaci?n WKT
178
         * @return Codificaci?n WKT
179
         * @throws OGRException
180
         */
181
        public String exportToWkt()throws OGRException {
182
                if(cPtr == 0)
183
                        throw new OGRException("Fallo al acceder al dato.");
184
                
185
                String wkt = exportToWktNat(cPtr);
186
                
187
                if(wkt == null)
188
                        throw new OGRException("Error en exportToWkt(). No se ha podido obtener la proyecci?n.");
189
                
190
                return wkt;
191
        }
192
        
193
        /**
194
         * @throws OGRException
195
         * @param a_srs        
196
         */
197
        public void setFromUserInput(String a_srs)throws OGRException {
198
                if(cPtr == 0)
199
                        throw new OGRException("Fallo al acceder al dato.");
200
                
201
                if ((a_srs == null) || (a_srs.equals("")))
202
                        throw new OGRException("Parametro incorrecto en OGRSpatialReference.setFromUserInput");
203
                
204
                int ogrerr = setFromUserInputNat(cPtr,a_srs);
205
                throwException(ogrerr,"Error en setFromUserInput().");
206
        }
207
        
208
        /**
209
         * Destructor 
210
         */
211
        protected void finalize() throws OGRFailureException{
212
                if(cPtr == 0)
213
                        throw new OGRFailureException("Fallo al acceder al dato.");
214
                
215
                OSRDestroySpatialReferenceNat(cPtr);
216
        }
217
        
218
        /**
219
         * Consulta si la fuente de datos est? en coordenas geograficas o no
220
         * @return true si est? en coordenadas geogr?ficas
221
         */
222
        public boolean isGeographic() throws CrsGdalException {
223
                if(cPtr == 0)
224
                        throw new CrsGdalException();
225
                return isGeographicNat(cPtr);
226
        }
227
        
228
        /**
229
         * Obtiene el c?digo EPSG
230
         * @param targetKey
231
         * @return "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL
232
         * @throws CrsGdalException
233
         */
234
        public String getAuthorityCode(String targetKey) throws CrsGdalException {
235
                if(cPtr == 0)
236
                        throw new CrsGdalException();
237
                
238
                if ((targetKey == null) || (targetKey.equals("")))
239
                        throw new CrsGdalException();
240
                
241
                return getAuthorityCodeNat(cPtr, targetKey);
242
        }
243
        
244
        /**
245
         * Obtiene el autority name. Tipicamente EPSG
246
         * @param targetKey
247
         * @return "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL
248
         * @throws CrsGdalException
249
         */
250
        public String getAuthorityName(String targetKey) throws CrsGdalException {
251
                if(cPtr == 0)
252
                        throw new CrsGdalException();
253
                
254
                if ((targetKey == null) || (targetKey.equals("")))
255
                        throw new CrsGdalException();
256
                
257
                return getAuthorityNameNat(cPtr, targetKey);
258
        }
259
        
260
        
261
        
262
        /**
263
         * 
264
         * @param ORGSpace
265
         * @return
266
         * @throws CrsGdalException
267
         */
268
        public static String exportToWkt(OGRSpatialReference ORGSpace) throws CrsGdalException{
269
                if (ORGSpace.cPtr == 0)
270
                        throw new CrsGdalException();
271
                
272
                return exportToWktNat(ORGSpace.cPtr);
273
        }
274
        
275
        
276
        /**
277
         * 
278
         * @param ORGSpace
279
         * @param cadenas
280
         * @return
281
         * @throws OGRException
282
         */
283
        public static int importFromWkt(OGRSpatialReference ORGSpace, String cadenas) throws OGRException {
284
                if ((cadenas == null) || (cadenas.equals("")))
285
                        throw new OGRException("Parametro incorrecto");
286
                
287
                if (ORGSpace.cPtr == 0)
288
                        throw new OGRException("Parametro incorrecto");
289
                
290
                int result = importFromWktNat(ORGSpace.cPtr, cadenas);
291
                if(result != 0) 
292
                        throw new OGRException(result, "Error en Wkt().La creacion del objeto no tuvo exito. ", ORGSpace);
293
                return result;
294
        }
295
        
296
        
297
        /**
298
         * 
299
         * @param ORGSpace
300
         * @param zona
301
         * @param norte_sur
302
         * @return
303
         * @throws OGRException
304
         */
305
        public static int setUTM(OGRSpatialReference ORGSpace ,int zona, boolean norte_sur)throws OGRException {
306
                if ((zona <= 0) || (zona > 60))
307
                        throw new OGRException("Zona incorrecta");
308
                
309
                if(ORGSpace.cPtr == 0)
310
                        throw new OGRException("Error en setUTM(). La llamada de creaci?n de objeto no tuvo exito.");
311
                
312
                int ns=-1;
313
                if(norte_sur)ns=1;
314
                else ns=0;
315
                
316
                int res = setUTMNat(ORGSpace.cPtr, zona, ns);
317
                
318
                if(res != 0)
319
                        throw new OGRException(res,"Error en setUTM(). No se ha podido asignar la zona especificada. ",ORGSpace);
320
                return res;
321
        }
322
        
323
        
324
        /**
325
         * 
326
         * @param ORGSpace
327
         * @param cs
328
         * @return
329
         * @throws OGRException
330
         */
331
        public static int setWellKnownGeogCS(OGRSpatialReference ORGSpace, String cs)throws OGRException {
332
                if ((cs == null) || (cs.equals("")))
333
                        throw new OGRException("Parametro incorrecto");
334
                
335
                if(ORGSpace.cPtr == 0)
336
                        throw new OGRException("Error en setWellKnownGeogCS(). La llamada de creaci?n de objeto no tuvo exito.");
337
                
338
                int res = setWellKnownGeogCSNat(ORGSpace.cPtr, cs);
339
                
340
                if(res != 0)
341
                        throw new OGRException(res,"Error en setWellKnownGeoCS(). No se ha podido asignar el sistema de coordenadas especificado. ",ORGSpace);
342
                return res;
343
        }
344
        
345
        
346
        /**
347
         * 
348
         * @param ORGSpace
349
         * @param cod
350
         * @return
351
         * @throws OGRException
352
         */
353
        public static int importFromEPSG(OGRSpatialReference ORGSpace, int cod) throws OGRException {
354
                if(ORGSpace.cPtr == 0)
355
                        throw new OGRException("Error importFromEPSG. La llamada de creaci?n de objeto no tuvo exito.");
356
                
357
                int result = importFromEPSGNat(ORGSpace.cPtr, cod);
358
                
359
                if(result != 0) 
360
                        throw new OGRException(result,"Error en EPSG().La creacion del objeto no tuvo exito. ",ORGSpace);
361

    
362
                return result;
363
        }
364
        
365
        
366
        /**
367
         * 
368
         * @param ORGSpace
369
         * @param cs
370
         * @return
371
         * @throws OGRException
372
         */
373
        public static int importFromProj4(OGRSpatialReference ORGSpace, String cs)throws OGRException {
374
                
375
                if ((cs == null) || (cs.equals("")))
376
                        throw new OGRException("Parametro incorrecto");
377
                
378
                if(ORGSpace.cPtr == 0)
379
                        throw new OGRException("Error en Proj4(). La llamada de creaci?n de objeto no tuvo exito.");
380
                
381
                int res = importFromProj4Nat(ORGSpace.cPtr, cs);
382
                
383
                if(res != 0)
384
                        throw new OGRException(res, "Error en Proj4(). No se ha podido asignar el sistema de coordenadas especificado. ", ORGSpace);
385
                
386
                return res;
387
                
388
        }
389
        
390
        
391
        /**
392
         * 
393
         * @param ORGSpace
394
         * @return
395
         * @throws CrsOgrException
396
         */
397
        public static String exportToProj4(OGRSpatialReference ORGSpace) throws OGRException {
398
                if(ORGSpace.cPtr == 0)
399
                        throw new OGRException("Error en exportToProj4(). La llamada de creaci?n de objeto no tuvo exito.");
400
                
401
                String result = exportToProj4Nat(ORGSpace.cPtr);
402
                
403
                if(result.length() == 0) 
404
                        throw new OGRException("No se ha podido hacer exportToProj4");
405
                return result;
406
        }
407
        
408
        
409
        /**
410
         * 
411
         * @param ORGSpace
412
         * @param cod
413
         * @param cs
414
         * @param coord
415
         * @return
416
         * @throws OGRException
417
         */
418
        public static int importFromPCI(OGRSpatialReference ORGSpace,String cod, String cs, double[] coord)throws OGRException {
419
                if ((cs == null) || (cs.equals("")))
420
                        throw new OGRException("Parametro incorrecto");
421
                
422
                if ((cod == null) || (cod.equals("")))
423
                        throw new OGRException("Parametro incorrecto");
424
                
425
                if(ORGSpace.cPtr == 0)
426
                        throw new OGRException("Error en importFromPCI. La llamada de creaci?n de objeto no tuvo exito.");
427
                
428
                if (coord == null)
429
                        throw new OGRException("Parametro incorrecto");
430
                
431
                int result = importFromPCINat(ORGSpace.cPtr,cod, cs, coord);
432
                
433
                if(result != 0) 
434
                        throw new OGRException(result, "Error en PCI().La creacion del objeto no tuvo exito. ", ORGSpace);
435
                
436
                return result;
437
        }
438
        
439
        
440
        /**
441
         * 
442
         * @param ORGSpace
443
         * @param code
444
         * @param zone
445
         * @param params
446
         * @param datum
447
         * @return
448
         * @throws OGRException
449
         */
450
        public static int importFromUSGS(OGRSpatialReference ORGSpace, long code, long zone, double[] params, long datum) throws OGRException {
451
                if(ORGSpace.cPtr == 0)
452
                        throw new OGRException("Error en importFromUSGS. La llamada de creaci?n de objeto no tuvo exito.");
453
                
454
                if ((zone <= 0) || (zone > 60))
455
                        throw new OGRException("Zona incorrecta");
456
                
457
                if (params == null)
458
                        throw new OGRException("Parametro incorrecto");
459
                
460
                int result = importFromUSGSNat(ORGSpace.cPtr,code, zone, params, datum);
461
                
462
                if(result != 0) 
463
                        throw new OGRException(result, "Error en USGS().La creacion del objeto no tuvo exito. ", ORGSpace);
464
                
465
                return result;        
466
                
467
        }
468
        
469
        
470
        /**
471
         * 
472
         * @param ORGSpace
473
         * @param cadenas
474
         * @return
475
         * @throws OGRException
476
         */
477
        public static int importFromESRI(OGRSpatialReference ORGSpace, String cadenas) throws OGRException {
478
                if (cadenas == null)
479
                        throw new OGRException("Parametro incorrecto");
480
                
481
                if(ORGSpace.cPtr == 0)
482
                        throw new OGRException("Error en importFromEsri. La llamada de creaci?n de objeto no tuvo exito.");
483
                
484
                
485
                int result = importFromESRINat(ORGSpace.cPtr, cadenas);
486
                if(result != 0) 
487
                        throw new OGRException(result, "Error en ESRI().La creacion del objeto no tuvo exito. ", ORGSpace);
488
                return result;
489
        }
490
}