Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_903 / libraries / libjni-gdal / src / gdal_interfaz.c @ 44123

History | View | Annotate | Download (13.4 KB)

1
 /**********************************************************************
2
 * $Id: gdal_interfaz.c 7764 2006-10-03 07:04:43Z nacho $
3
 *
4
 * Name:     gdal_interfaz.c
5
 * Project:  JGDAL. Interface java to gdal (Frank Warmerdam).
6
 * Purpose:  dataset's Basic Funcions.
7
 * Author:   Nacho Brodin, brodin_ign@gva.es
8
 *
9
 **********************************************************************/
10
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
11
*
12
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
13
*
14
* This program is free software; you can redistribute it and/or
15
* modify it under the terms of the GNU General Public License
16
* as published by the Free Software Foundation; either version 2
17
* of the License, or (at your option) any later version.
18
*
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
* GNU General Public License for more details.
23
*
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
27
*
28
* For more information, contact:
29
*
30
*  Generalitat Valenciana
31
*   Conselleria d'Infraestructures i Transport
32
*   Av. Blasco Ib??ez, 50
33
*   46010 VALENCIA
34
*   SPAIN
35
*
36
*      +34 963862235
37
*   gvsig@gva.es
38
*      www.gvsig.gva.es
39
*
40
*    or
41
*
42
*   IVER T.I. S.A
43
*   Salamanca 50
44
*   46005 Valencia
45
*   Spain
46
*
47
*   +34 963163400
48
*   dac@iver.es
49
* 
50
* [01] 01-Oct-2005 nbt OpenArray function to support name parameter in char array
51
*/
52

    
53

    
54
#include <jni.h>
55
#include "es_gva_cit_jgdal_Gdal.h"
56
#include "es_gva_cit_jgdal_JNIBase.h"
57
#include "gdal.h"
58
#include "cpl_string.h"
59

    
60

    
61

    
62
/******************************************************************************/
63
//                                                                Open
64
/******************************************************************************/
65

    
66

    
67
JNIEXPORT jlong JNICALL Java_es_gva_cit_jgdal_Gdal_openNat
68
  (JNIEnv *env, jobject obj, jstring pszF, jint acc){
69
          
70
          const char *pszFilename;
71
          GDALDatasetH *dataset;
72
          jlong jresult = 0 ;
73
          FILE *fich;
74
          
75
          pszFilename = (*env)->GetStringUTFChars(env, pszF, 0);
76

    
77
        fich=fopen( pszFilename, "r" );
78
        if( fich )
79
                fclose(fich);
80
        else
81
           {
82
              fclose(fich);
83
              return -1;
84
           }
85
          
86
          GDALAllRegister();
87
          dataset = GDALOpen((char *)pszFilename,(int)acc);
88
          
89
          *(GDALDatasetH **)&jresult = dataset;
90
          
91
    (*env)->ReleaseStringUTFChars(env, pszF, pszFilename);
92
          
93
  
94
          if(dataset==NULL)return -1; 
95
          else return jresult; 
96
                  
97
  }
98
  
99
/******************************************************************************/
100
//                                                                OpenArray
101
/******************************************************************************/
102

    
103

    
104
JNIEXPORT jlong JNICALL Java_es_gva_cit_jgdal_Gdal_openArrayNat
105
  (JNIEnv *env, jobject obj, jbyteArray pszF, jint acc){
106
          
107
          jbyte *pszFilename = NULL;
108
          jbyte *aux = NULL;
109
          GDALDatasetH *dataset;
110
          jlong jresult = 0 ;
111
          FILE *fich;
112
          jsize longitud = 0, i;
113
          char *p;
114
                    
115
          longitud = (*env)->GetArrayLength(env, pszF); 
116
          pszFilename = (*env)->GetByteArrayElements(env, pszF, 0);
117
          
118
          #ifdef __linux__
119
                pszFilename = (jbyte *)realloc(pszFilename, longitud + 1);
120
        #else
121
                aux = (jbyte *)malloc(sizeof(jbyte) * (longitud + 1));
122
                  memcpy(aux, pszFilename, longitud);
123
                  free(pszFilename);
124
                  pszFilename = aux;
125
        #endif
126
                  
127
        pszFilename[longitud] = '\0';
128
        
129
        fich = fopen( pszFilename, "r" );
130
        if( fich )
131
                fclose(fich);
132
        else                
133
              return -1;
134
           
135
           GDALAllRegister();
136
          dataset = GDALOpen((char *)pszFilename,(int)acc);
137
          
138
          *(GDALDatasetH **)&jresult = dataset;
139
          
140
    (*env)->ReleaseByteArrayElements(env, pszF, pszFilename, 0);
141
          
142
          if(dataset == NULL)
143
                  return -1; 
144
          else 
145
                  return jresult; 
146
                  
147
  }
148

    
149

    
150
/******************************************************************************/  
151
//                                                                GetRasterXSize
152
/******************************************************************************/
153

    
154
  
155
  JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterXSizeNat
156
  (JNIEnv *env, jobject obj, jlong cPtr){
157
          
158
          int res=-1;
159
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
160
  
161
          dt = *(GDALDatasetH **)&cPtr;
162
          if(dt!=NULL)
163
                  res = GDALGetRasterXSize(dt);
164
  
165
                    
166
          return res;
167
                          
168
  }
169

    
170
  
171
/******************************************************************************/  
172
//                                                                GetRasterYSize
173
/******************************************************************************/  
174
  
175
  
176
  JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterYSizeNat
177
  (JNIEnv *env, jobject obj, jlong cPtr){
178
          
179
          int res=-1;
180
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
181
          
182
          dt = *(GDALDatasetH **)&cPtr;
183
          if(dt!=NULL)
184
                  res = GDALGetRasterYSize(dt);
185
  
186
                    
187
          return res;
188
          
189
  }
190

    
191
/******************************************************************************/   
192
//                                                                GetRasterCount
193
/******************************************************************************/
194
 
195
 
196
 JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterCountNat
197
  (JNIEnv *env, jobject obj, jlong cPtr){
198
          
199
          int res=-1;
200
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
201
          
202
          dt = *(GDALDatasetH **)&cPtr;
203
          if(dt!=NULL)
204
                  res = GDALGetRasterCount(dt);
205
  
206
                    
207
          return res;
208
  }
209
  
210
  
211
/******************************************************************************/ 
212
//                                                                 GetMetadata
213
/******************************************************************************/
214

    
215

    
216
JNIEXPORT jobjectArray JNICALL Java_es_gva_cit_jgdal_Gdal_getMetadataNat
217
  (JNIEnv *env, jobject obj, jlong cPtr, jstring pszDomain){
218
          
219
           char                        **papszMetadata = NULL;
220
           GDALDatasetH         *dt  = (GDALDatasetH *) 0 ;
221
           int                         i = 0;
222
           int                        nmetadatos = 0;
223
           jclass                 class_string;
224
           jobjectArray         vector_str;
225
                       
226
           dt = *(GDALDatasetH **)&cPtr;
227
                               
228
           if(dt != NULL){
229
                      //Obtenemos los metadatos sobre papszMetadata
230
                 papszMetadata = GDALGetMetadata( dt, NULL );
231
                 
232
                 //Si hay metadatos devolvemos creamos el vector de String de java y los devolvemos
233
                 
234
                 nmetadatos = CSLCount(papszMetadata);
235
             if( nmetadatos > 0 ){
236
                class_string = (*env)->FindClass (env, "java/lang/String");
237
                         vector_str = (*env)->NewObjectArray(env, nmetadatos, class_string, (*env)->NewStringUTF(env,""));
238
                         
239
                         //Cargamos los metadatos
240
                         
241
                for( i = 0; papszMetadata[i] != NULL; i++ )
242
                        (*env)->SetObjectArrayElement(env,vector_str,i,(*env)->NewStringUTF(env,papszMetadata[i]));
243
                                               
244
                           return vector_str;            
245
              }
246
           }
247
     
248
           return NULL;
249
  }
250

    
251

    
252

    
253

    
254

    
255
/******************************************************************************/   
256
//                                                                GetRasterBand  
257
/******************************************************************************/
258

    
259

    
260
  JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_Gdal_getRasterBandNat
261
  (JNIEnv *env, jobject obj, jlong cPtr, jint hBand){
262
          
263
                         
264
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
265
          GDALRasterBandH *rasterband=NULL;
266
          jlong jresult = 0;
267

    
268
        dt = *(GDALDatasetH **)&cPtr;
269
    
270
    if(dt!=NULL){
271
                rasterband = GDALGetRasterBand(dt,(int)hBand);
272
                *(GDALDatasetH **)&jresult = rasterband;
273
    }else return -1;
274
    
275
   return jresult;
276
    
277
  }
278
  
279

    
280
/******************************************************************************/
281
//                                                                GetGeoTransform
282
/******************************************************************************/
283

    
284

    
285
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_Gdal_getGeoTransformNat
286
  (JNIEnv *env, jobject obj, jlong cPtr, jobject gt){
287
          
288
          jclass class_geotransform;
289
          jfieldID id_campo;
290
          double adfgt[6];
291
          jdoubleArray doublearray;
292
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
293
          
294

    
295
        dt = *(GDALDatasetH **)&cPtr;
296
                  
297
          if(dt!=NULL){
298
                          
299
          //Obtenemos el objeto java que contendr? el vector geotransform
300
                
301
                          
302
                  class_geotransform = (*env)->GetObjectClass(env, gt);
303
                  id_campo = (*env)->GetFieldID(env, class_geotransform, "adfgeotransform", "[D");
304

    
305

    
306
           //Cargamos el buffer llamando a GDALRasterIO
307
                                   
308
                  
309
                  if(GDALGetGeoTransform(dt,adfgt)==CE_None){
310
                          doublearray = (*env)->NewDoubleArray(env,6);
311
                    if(doublearray!=NULL){
312
                                  (*env)->SetDoubleArrayRegion(env, doublearray, 0, 6,(jdouble *)adfgt); 
313
                                  (*env)->SetObjectField(env, gt, id_campo, doublearray);
314
                        }
315
                  }else return -1;                
316
                        
317
          }else return -1;
318
          
319
          return 0;
320
                                    
321
  }
322
  
323
/******************************************************************************/
324
//                                                                setGeoTransformNat
325
/******************************************************************************/
326

    
327
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_Gdal_setGeoTransformNat
328
  (JNIEnv *env, jobject obj, jlong cPtr, jobject gt){
329
          
330
          jclass class_geotransform;
331
          jfieldID id_campo;
332
          double adfgt[6];
333
          jdoubleArray doublearray;
334
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
335
          
336
          dt = *(GDALDatasetH **)&cPtr;
337
          
338
          if(dt!=NULL){
339
                  
340
                  //Obtenemos los valores pasados en el vector gt
341
                  
342
                  class_geotransform = (*env)->GetObjectClass(env, gt);
343
                  id_campo = (*env)->GetFieldID(env, class_geotransform, "adfgeotransform", "[D");
344
                  doublearray =(jdoubleArray)(*env)->GetObjectField(env, gt, id_campo);
345
                  (*env)->GetDoubleArrayRegion(env,doublearray,0,6,(jdouble *)adfgt);
346
                  
347
                  GDALSetGeoTransform( dt, adfgt );
348
                  return 0;
349
          }
350
          return -1;
351
          
352
  }  
353
  
354
/******************************************************************************/
355
//                                                                GetProjectionRef
356
/******************************************************************************/
357

    
358

    
359
JNIEXPORT jstring JNICALL Java_es_gva_cit_jgdal_Gdal_getProjectionRefNat
360
  (JNIEnv *env, jobject obj, jlong cPtr){
361
          
362
        char *pszProjection=NULL;
363
          jstring pszP=NULL;
364
        GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
365
          
366
                  
367
          dt = *(GDALDatasetH **)&cPtr;
368
                  
369
          //Llamamos a la funci?n y cargamos el jstring con el resultado         
370
                  
371
        if(dt!=NULL){          
372
                   pszProjection = (char *) GDALGetProjectionRef(dt);
373
                   if(pszProjection!=NULL)
374
                           pszP = (*env)->NewStringUTF(env, pszProjection); 
375
        }
376
           
377
           return pszP;
378
          
379
  }
380
  
381
/************************************************************************/
382
/*                        getDriverShortNameNat()                       */
383
/************************************************************************/
384

    
385
JNIEXPORT jstring JNICALL Java_es_gva_cit_jgdal_Gdal_getDriverShortNameNat
386
  (JNIEnv *env, jobject obj, jlong cPtr){
387
          
388
          char                         *pszShortName=NULL;
389
          jstring                 pszSN=NULL;
390
        GDALDatasetH         *dt  = (GDALDatasetH *) 0 ;
391
        GDALDriverH                hDriver;
392
        
393
        dt = *(GDALDatasetH **)&cPtr;
394
        
395
        //Llamamos a la funci?n y cargamos el jstring con el resultado         
396
                  
397
        if(dt!=NULL){          
398
                hDriver = GDALGetDatasetDriver( dt );
399
                   pszShortName = (char *) GDALGetDriverShortName(hDriver);
400
                   if(pszShortName!=NULL)
401
                           pszSN = (*env)->NewStringUTF(env, pszShortName); 
402
        }
403
           
404
           return pszSN;
405
  }
406
  
407
/******************************************************************************/  
408
//                                                                setProjection
409
/******************************************************************************/  
410
  
411
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_Gdal_setProjectionNat
412
  (JNIEnv *env, jobject obj, jlong cPtr, jstring proj){
413
          
414
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
415
          
416
          dt = *(GDALDatasetH **)&cPtr;
417
          if(dt!=NULL){
418
                  const char *projeccion = (*env)->GetStringUTFChars(env, proj, 0);
419
                  GDALSetProjection( dt, projeccion );
420
                  (*env)->ReleaseStringUTFChars(env, proj, projeccion);
421
                  return 1;
422
          }
423
          
424
          return -1;
425
  }
426
  
427
  
428
/******************************************************************************/  
429
//                                                                        Close
430
/******************************************************************************/
431

    
432

    
433
JNIEXPORT void JNICALL Java_es_gva_cit_jgdal_Gdal_closeNat
434
  (JNIEnv *env, jobject obj, jlong cPtr){
435

    
436
        GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
437
          
438
          dt = *(GDALDatasetH **)&cPtr;
439
        GDALClose(dt);        
440

    
441
  }
442
  
443

    
444
/************************************************************************/
445
/*                        GDALGetDriverByName()                         */
446
/************************************************************************/
447

    
448
JNIEXPORT jlong JNICALL Java_es_gva_cit_jgdal_Gdal_getDriverByNameNat
449
  (JNIEnv *env, jclass clase, jstring name){
450
          
451
          const char *namedrv;
452
          GDALDriverH *drv;
453
 
454
          jlong jresult=-1;
455
  
456
    if(GDALGetDriverCount()<=0)GDALAllRegister();
457
            
458
          namedrv = (*env)->GetStringUTFChars(env, name, 0);
459
          
460
          drv=GDALGetDriverByName(namedrv);  
461
                  
462
          *(GDALDriverH **)&jresult = drv;
463

    
464
          (*env)->ReleaseStringUTFChars(env, name, namedrv);
465
         
466
           return (long)jresult;
467
  }
468
  
469

    
470
/************************************************************************/
471
/*                        getGCPCountNat()                              */
472
/************************************************************************/
473

    
474
 JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getGCPCountNat
475
  (JNIEnv *env, jobject obj, jlong cPtr){
476
          
477
          int res=-1;
478
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
479
          
480
          dt = *(GDALDatasetH **)&cPtr;
481
          if(dt!=NULL)
482
                  res = GDALGetGCPCount(dt);
483
  
484
                    
485
          return res;
486
  }
487
 
488
 /******************************************************************************/ 
489
//                                                        GetColorInterpretationName
490
/******************************************************************************/
491

    
492
JNIEXPORT jstring JNICALL Java_es_gva_cit_jgdal_Gdal_getColorInterpretationNameNat
493
  (JNIEnv *env, jobject obj, jlong cPtr, jint ci){
494
           GDALDatasetH                 *dt  = (GDALDatasetH *) 0 ;
495
            char                                 *name = NULL;
496
            jstring                         typeName = NULL;
497
         
498
         dt = *(GDALDatasetH **)&cPtr;
499
         name = GDALGetColorInterpretationName(ci);           
500
         typeName = (*env)->NewStringUTF(env, name); 
501
         return typeName;
502
  }
503
  
504