Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-gdal-macosx / src / gdal_interfaz.c @ 25845

History | View | Annotate | Download (13.6 KB)

1 8219 nacho
 /**********************************************************************
2
 * $Id$
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
        printf("==>OpenArray jresult:%ld  dataset:%ld\n", jresult, dataset);
141
142
    (*env)->ReleaseByteArrayElements(env, pszF, pszFilename, 0);
143
144
          if(dataset == NULL)
145
                  return -1;
146
          else
147
                  return jresult;
148
149
  }
150
151
152
/******************************************************************************/
153
//                                                                GetRasterXSize
154
/******************************************************************************/
155
156
157
  JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterXSizeNat
158
  (JNIEnv *env, jobject obj, jlong cPtr){
159
160
          int res=-1;
161
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
162
163
          dt = *(GDALDatasetH **)&cPtr;
164
          if(dt!=NULL)
165
                  res = GDALGetRasterXSize(dt);
166
167
168
          return res;
169
170
  }
171
172
173
/******************************************************************************/
174
//                                                                GetRasterYSize
175
/******************************************************************************/
176
177
178
  JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterYSizeNat
179
  (JNIEnv *env, jobject obj, jlong cPtr){
180
181
          int res=-1;
182
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
183
184
          dt = *(GDALDatasetH **)&cPtr;
185
          if(dt!=NULL)
186
                  res = GDALGetRasterYSize(dt);
187
188
189
          return res;
190
191
  }
192
193
/******************************************************************************/
194
//                                                                GetRasterCount
195
/******************************************************************************/
196
197
198
 JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterCountNat
199
  (JNIEnv *env, jobject obj, jlong cPtr){
200
201
          int res=-1;
202
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
203
204
          dt = *(GDALDatasetH **)&cPtr;
205
          if(dt!=NULL)
206
                  res = GDALGetRasterCount(dt);
207
208
209
          return res;
210
  }
211
212
213
/******************************************************************************/
214
//                                                                 GetMetadata
215
/******************************************************************************/
216
217
218
JNIEXPORT jobjectArray JNICALL Java_es_gva_cit_jgdal_Gdal_getMetadataNat
219
  (JNIEnv *env, jobject obj, jlong cPtr, jstring pszDomain){
220
221
           char                        **papszMetadata = NULL;
222
           GDALDatasetH         *dt  = (GDALDatasetH *) 0 ;
223
           int                         i = 0;
224
           int                        nmetadatos = 0;
225
           jclass                 class_string;
226
           jobjectArray         vector_str;
227
228
           dt = *(GDALDatasetH **)&cPtr;
229
230
           if(dt != NULL){
231
                      //Obtenemos los metadatos sobre papszMetadata
232
                 papszMetadata = GDALGetMetadata( dt, NULL );
233
234
                 //Si hay metadatos devolvemos creamos el vector de String de java y los devolvemos
235
236
                 nmetadatos = CSLCount(papszMetadata);
237
             if( nmetadatos > 0 ){
238
                class_string = (*env)->FindClass (env, "java/lang/String");
239
                         vector_str = (*env)->NewObjectArray(env, nmetadatos, class_string, (*env)->NewStringUTF(env,""));
240
241
                         //Cargamos los metadatos
242
243
                for( i = 0; papszMetadata[i] != NULL; i++ )
244
                        (*env)->SetObjectArrayElement(env,vector_str,i,(*env)->NewStringUTF(env,papszMetadata[i]));
245
246
                           return vector_str;
247
              }
248
           }
249
250
           return NULL;
251
  }
252
253
254
255
256
257
/******************************************************************************/
258
//                                                                GetRasterBand
259
/******************************************************************************/
260
261
262
  JNIEXPORT jlong JNICALL Java_es_gva_cit_jgdal_Gdal_getRasterBandNat
263
  (JNIEnv *env, jobject obj, jlong cPtr, jint hBand){
264
265
266
          GDALDatasetH *dt  = (GDALDatasetH *) 0 ;
267
          GDALRasterBandH *rasterband=NULL;
268
          jlong jresult = 0;
269
270
        dt = *(GDALDatasetH **)&cPtr;
271
272
    if(dt!=NULL){
273
                rasterband = GDALGetRasterBand(dt,(int)hBand);
274
                *(GDALDatasetH **)&jresult = rasterband;
275
                printf("==>GetRasterBand cPtr:%ld  dt:%ld jresult:%ld  rasterband:%ld", cPtr, dt, 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 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