Statistics
| Revision:

root / branches / v10 / libraries / libjni-gdal / src / gdal_interfaz.c @ 10988

History | View | Annotate | Download (13.5 KB)

1
 /**********************************************************************
2
 * $Id: gdal_interfaz.c 10988 2007-03-30 11:54:34Z 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
#include "signal.h"
60

    
61

    
62

    
63
/******************************************************************************/
64
//                                                                Open
65
/******************************************************************************/
66

    
67

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

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

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

    
152

    
153
/******************************************************************************/  
154
//                                                                GetRasterXSize
155
/******************************************************************************/
156

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

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

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

    
218

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

    
254

    
255

    
256

    
257

    
258
/******************************************************************************/   
259
//                                                                GetRasterBand  
260
/******************************************************************************/
261

    
262

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

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

    
283
/******************************************************************************/
284
//                                                                GetGeoTransform
285
/******************************************************************************/
286

    
287

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

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

    
308

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

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

    
361

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

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

    
435

    
436
JNIEXPORT void JNICALL Java_es_gva_cit_jgdal_Gdal_closeNat
437
  (JNIEnv *env, jobject obj, jlong cPtr){
438

    
439
        GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
440
          
441
          dt = *(GDALDatasetH **)&cPtr;
442
        GDALClose(dt);        
443

    
444
  }
445
  
446

    
447
/************************************************************************/
448
/*                        GDALGetDriverByName()                         */
449
/************************************************************************/
450

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

    
467
          (*env)->ReleaseStringUTFChars(env, name, namedrv);
468
         
469
           return (long)jresult;
470
  }
471
  
472

    
473
/************************************************************************/
474
/*                        getGCPCountNat()                              */
475
/************************************************************************/
476

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

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