Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-gdal / src / main / native / jgdal / rasterband_interfaz.c @ 21213

History | View | Annotate | Download (26.3 KB)

1
/**********************************************************************
2
 * $Id: rasterband_interfaz.c 15691 2007-10-31 10:49:53Z nbrodin $
3
 *
4
 * Name:     rasterband_interfaz.c
5
 * Project:  JGDAL. Interface java to gdal (Frank Warmerdam).
6
 * Purpose:  Raster band'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
        
51
#include <jni.h>
52
#include "gdal.h"
53
#include "cpl_string.h"
54
  
55
/******************************************************************************/
56
//                                                        GetRasterBandXSize
57
/******************************************************************************/
58

    
59

    
60
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterBandXSizeNat
61
  (JNIEnv *env, jobject obj, jlong cPtr){
62
          
63
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
64
          int res = -1;
65
                    
66
    rb = *(GDALRasterBandH **)&cPtr;
67
    
68
    if(rb!=NULL)
69
            res = GDALGetRasterBandXSize(rb);        
70

    
71

    
72
    return res;
73
  }
74
  
75
  
76
/******************************************************************************/ 
77
//                                                        GetRasterBandYSize
78
/******************************************************************************/
79

    
80

    
81
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterBandYSizeNat
82
  (JNIEnv *env, jobject obj, jlong cPtr){
83
          
84
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
85
          int res = -1;
86
                    
87
    rb = *(GDALRasterBandH **)&cPtr;
88
    
89
    if(rb!=NULL)
90
            res = GDALGetRasterBandYSize(rb);        
91

    
92
    return res;
93
          
94
  }
95
  
96
 /****************************************************************************/
97
 //                                                         GetOverviewCount
98
 /******************************************************************************/
99
  
100
  
101
  JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getOverviewCountNat
102
  (JNIEnv *env, jobject obj, jlong cPtr){
103
          
104
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
105
          int res = -1;
106
                    
107
    rb = *(GDALRasterBandH **)&cPtr;
108
    
109
    if(rb!=NULL)
110
                res = GDALGetOverviewCount(rb);        
111

    
112
    return res;
113
    
114
  }
115
  
116
/******************************************************************************/   
117
//                                                                GetOverview
118
/******************************************************************************/
119

    
120

    
121
JNIEXPORT jlong JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_getOverviewNat
122
  (JNIEnv *env, jobject obj, jlong cPtr, jint noverview){
123
          
124
        GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
125
        GDALRasterBandH *res = NULL;
126
        jlong jresult = 0;
127
          
128
        rb = *(GDALRasterBandH **)&cPtr;
129
    
130
    if(rb!=NULL)
131
            res = GDALGetOverview(rb,(int)noverview);
132
    
133
    *(GDALDatasetH **)&jresult = res;
134
        
135
    if(res==NULL)return -1;
136
    else return jresult;
137
                  
138
  }
139
  
140
/******************************************************************************/     
141
//                                                                min
142
/******************************************************************************/    
143
  
144
/*int min(int x, int y){
145
        if(x < y)
146
                return x;
147
        else
148
                return y;        
149
}*/
150
  
151
/******************************************************************************/     
152
//                                                                ReadRaster
153
/******************************************************************************/  
154
  
155
  
156
JNIEXPORT jobject JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_readRasterNat
157
  (JNIEnv *env, jobject obj, jlong cPtr, jint nXOff, jint nYOff, jint nXSize, jint nYSize, jint BufXSize, jint BufYSize, jint eBufType){
158

    
159
                  jclass class_gdalbuffer;
160
                  jmethodID metodo;
161
                  jobject obj_gdalbuffer = NULL;
162
                  jfieldID id_campo;
163
                  
164
                  GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
165
                  
166
                  jbyteArray bytearray;
167
                  jshortArray shortarray;
168
                  jintArray intarray;
169
                  jfloatArray floatarray;
170
                  jdoubleArray doublearray;
171
                  
172
                  unsigned char *pabyScanLineBuf;
173
                  int *buffInt=NULL;
174
                  unsigned int *buffUInt=NULL;
175
                  long *buffLong=NULL;
176
                  unsigned long *buffULong=NULL;
177
                  float *buffFloat=NULL;
178
                  double *buffDouble=NULL;
179
                                   
180
            rb = *(GDALRasterBandH **)&cPtr;
181
                  
182
                  if(rb!=NULL){
183
                          
184
                  //Creamos el objeto java que contendr� la l�nea
185
                  
186
                          class_gdalbuffer = (*env)->FindClass (env, "es/gva/cit/jgdal/GdalBuffer");
187
                          
188
                          if(eBufType==1){
189
                                  
190
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaByte", "(I)V");
191
                                          
192
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(BufXSize*BufYSize));
193
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffByte", "[B");
194
                                                    
195
                                //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
196
                                                    
197
                                  pabyScanLineBuf = (unsigned char*) CPLMalloc(sizeof(unsigned char) *(int)BufXSize * (int)BufYSize);
198
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, pabyScanLineBuf, (int)BufXSize, (int)BufYSize, GDT_Byte, 0, 0);          
199
                                bytearray = (*env)->NewByteArray(env,(int)(BufXSize*BufYSize));
200
                                if(bytearray!=NULL){
201
                                          (*env)->SetByteArrayRegion(env, bytearray, 0, (int)(BufXSize*BufYSize),(jbyte *)pabyScanLineBuf); 
202
                                          (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, bytearray);
203
                                }
204
                                if(pabyScanLineBuf != NULL)CPLFree(pabyScanLineBuf);
205
                                
206
                          }else
207
                          
208
                          //------------------------------------------------------------------
209
                          
210
                          if(eBufType==2 || eBufType==3 || eBufType==8){
211
                                  
212
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaShort", "(I)V");
213
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(BufXSize*BufYSize));
214
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffShort", "[S");
215
                                            
216
                           //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
217
                                                                              
218
                                  if(eBufType==2){
219
                                          buffUInt = (unsigned int*) CPLMalloc(sizeof(unsigned int) *(int)BufXSize * (int)BufYSize);
220
                                          GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffUInt, (int)BufXSize, (int)BufYSize, GDT_UInt16, 0, 0);
221
                                            shortarray = (*env)->NewShortArray(env,(int)(BufXSize*BufYSize));
222
                                    if(shortarray!=NULL){
223
                                                  (*env)->SetShortArrayRegion(env, shortarray, 0, (int)(BufXSize*BufYSize),(jshort *)buffUInt); 
224
                                                  (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, shortarray);
225
                                        }
226
                                        if(buffUInt != NULL)CPLFree(buffUInt);
227
                                        return obj_gdalbuffer;                
228
                                  }
229
                                  
230
                                  buffInt = (int*) CPLMalloc(sizeof(int) *(int)BufXSize * (int)BufYSize);
231
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffInt, (int)BufXSize, (int)BufYSize, (int)eBufType, 0, 0);
232
                                  shortarray = (*env)->NewShortArray(env,(int)(BufXSize*BufYSize));
233
                            if(shortarray!=NULL){
234
                                          (*env)->SetShortArrayRegion(env, shortarray, 0, (int)(BufXSize*BufYSize),(jshort *)buffInt); 
235
                                         (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, shortarray);
236
                                }
237

    
238
                                if(buffInt != NULL)CPLFree(buffInt);
239
                                
240
                          }else
241
                          
242
                          //------------------------------------------------------------------
243
                          
244
                          if(eBufType==4 || eBufType==5 || eBufType==9){
245
                                  
246
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaInt", "(I)V");
247
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(BufXSize*BufYSize));
248
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffInt", "[I");
249
                                            
250
                           //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
251
                                                                              
252
                                  if(eBufType==4){
253
                                          buffULong = (unsigned long*) CPLMalloc(sizeof(unsigned long) *(int)BufXSize * (int)BufYSize);
254
                                          GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffULong, (int)BufXSize, (int)BufYSize, GDT_UInt32, 0, 0);
255
                                          intarray = (*env)->NewIntArray(env,(int)(BufXSize*BufYSize));
256
                                    if(intarray!=NULL){
257
                                                  (*env)->SetIntArrayRegion(env, intarray, 0, (int)(BufXSize*BufYSize),(jint *)buffULong); 
258
                                                  (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, intarray);
259
                                        }
260
                                        if(buffULong != NULL)CPLFree(buffULong);
261
                                        return obj_gdalbuffer;
262
                                  }
263
                                  
264
                                  buffLong = (long*) CPLMalloc(sizeof(long) *(int)BufXSize * (int)BufYSize);
265
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffLong, (int)BufXSize, (int)BufYSize, (int)eBufType, 0, 0);
266
                                  intarray = (*env)->NewIntArray(env,(int)(BufXSize*BufYSize));
267
                            if(intarray!=NULL){
268
                                          (*env)->SetIntArrayRegion(env, intarray, 0, (int)(BufXSize*BufYSize),(jint *)buffLong); 
269
                                         (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, intarray);
270
                                }
271

    
272
                                if(buffLong != NULL)CPLFree(buffLong);
273
                                
274
                          }else
275
                          
276
                          //------------------------------------------------------------------
277
                          
278
                          if(eBufType==6 || eBufType==10){
279
                                  
280
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaFloat", "(I)V");
281
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(BufXSize*BufYSize));
282
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffFloat", "[F");
283
                                            
284
                                   //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
285
                           
286
                                   buffFloat = (float*) CPLMalloc(sizeof(float) *(int)BufXSize * (int)BufYSize);
287
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffFloat, (int)BufXSize, (int)BufYSize, (int)eBufType, 0, 0);
288
                                  floatarray = (*env)->NewFloatArray(env,(int)(BufXSize*BufYSize));
289
                            if(floatarray!=NULL){
290
                                          (*env)->SetFloatArrayRegion(env, floatarray, 0, (int)(BufXSize*BufYSize),(jfloat *)buffFloat); 
291
                                         (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, floatarray);
292
                                }
293

    
294
                                if(buffFloat != NULL)CPLFree(buffFloat);
295
                          }else
296
                          
297
                          //------------------------------------------------------------------
298
                          
299
                          if(eBufType==7 || eBufType==11){
300
                                  
301
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaDouble", "(I)V");
302
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(BufXSize*BufYSize));
303
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffDouble", "[D");
304
                                            
305
                                   //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
306
                           
307
                                   buffDouble = (double*) CPLMalloc(sizeof(double) *(int)BufXSize * (int)BufYSize);
308
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffDouble, (int)BufXSize, (int)BufYSize, (int)eBufType, 0, 0);
309
                                  doublearray = (*env)->NewDoubleArray(env,(int)(BufXSize*BufYSize));
310
                            if(doublearray!=NULL){
311
                                          (*env)->SetDoubleArrayRegion(env, doublearray, 0, (int)(BufXSize*BufYSize),(jdouble *)buffDouble); 
312
                                         (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, doublearray);
313
                                }
314

    
315
                                if(buffDouble != NULL)CPLFree(buffDouble);
316
                          }
317
                          
318
                  }//if(rb!=NULL)
319
                                    
320
                  return obj_gdalbuffer;
321
                  
322
  } 
323
  
324
/******************************************************************************/     
325
//                                                                ReadRaster
326
/******************************************************************************/  
327
  
328
  
329
JNIEXPORT jobject JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_readRasterWithPaletteNat
330
  (JNIEnv *env, jobject obj, jlong cPtr, jint nXOff, jint nYOff, jint nXSize, jint nYSize, jint BufXSize, jint BufYSize, jint eBufType){
331

    
332
                  jclass class_gdalbuffer;
333
                  jmethodID metodo;
334
                  jobject obj_gdalbuffer = NULL;
335
                  jfieldID id_campoR, id_campoG, id_campoB, id_campoA;
336
                  int dfNoData, bGotNodata;
337
                  
338
                  GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
339
                  
340
                  jbyteArray bytearrayR, bytearrayG, bytearrayB, bytearrayA;
341
                  
342
                  unsigned char *scanLineBufR, *scanLineBufG, *scanLineBufB, *scanLineBufA;
343
                  
344
                  GDALColorTableH hTable;
345
        GByte      abyPCT[256][4];
346
                int i = 0;                  
347
                                   
348
            rb = *(GDALRasterBandH **)&cPtr;
349
                  
350
                  if(rb!=NULL){
351
                          
352
                  //Creamos el objeto java que contendr� la l�nea
353
                  
354
                          class_gdalbuffer = (*env)->FindClass (env, "es/gva/cit/jgdal/GdalBuffer");
355
                          
356
                          if(eBufType==1){
357
                                  
358
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaPalette", "(I)V");
359
                                          
360
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(BufXSize*BufYSize));
361
                                  id_campoR = (*env)->GetFieldID(env, class_gdalbuffer, "buffRPalette", "[B");
362
                                  id_campoG = (*env)->GetFieldID(env, class_gdalbuffer, "buffGPalette", "[B");
363
                                  id_campoB = (*env)->GetFieldID(env, class_gdalbuffer, "buffBPalette", "[B");
364
                                  id_campoA = (*env)->GetFieldID(env, class_gdalbuffer, "buffAPalette", "[B");
365
                                                    
366
                                //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
367
                                                    
368
                                  scanLineBufR = (unsigned char*) CPLMalloc(sizeof(unsigned char) *(int)BufXSize * (int)BufYSize);
369
                                  scanLineBufG = (unsigned char*) CPLMalloc(sizeof(unsigned char) *(int)BufXSize * (int)BufYSize);
370
                                  scanLineBufB = (unsigned char*) CPLMalloc(sizeof(unsigned char) *(int)BufXSize * (int)BufYSize);
371
                                  scanLineBufA = (unsigned char*) CPLMalloc(sizeof(unsigned char) *(int)BufXSize * (int)BufYSize);
372
                                  
373
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, scanLineBufR, (int)BufXSize, (int)BufYSize, GDT_Byte, 0, 0);
374
                                                                    
375
                                    hTable = GDALGetRasterColorTable( rb );          
376
                                     
377
                                  if( hTable != NULL ){
378
                                  int min = GDALGetColorEntryCount( hTable );
379
                                  dfNoData = GDALGetRasterNoDataValue( rb, &bGotNodata );
380
                                  if(256 < min)
381
                                   min = 256;
382
                                         for( i = 0; i < min; i++ ){
383
                                    GDALColorEntry sEntry;
384

    
385
                                    GDALGetColorEntryAsRGB( hTable, i, &sEntry );
386
                                    abyPCT[i][0] = sEntry.c1;
387
                                    abyPCT[i][1] = sEntry.c2;
388
                                    abyPCT[i][2] = sEntry.c3;
389
                                    abyPCT[i][3] = sEntry.c4;
390
                                    if(sEntry.c4 == 0){
391
                                            abyPCT[i][0] = 255;
392
                                            abyPCT[i][1] = 255;
393
                                            abyPCT[i][2] = 255;
394
                                            abyPCT[i][3] = 0;
395
                                    }         
396
                                    //printf("%d %d %d %d\n", sEntry.c1,sEntry.c2, sEntry.c3,sEntry.c4);
397
                                }
398
                                
399
                                for( i = GDALGetColorEntryCount( hTable ); i < 256; i++ ){
400
                                    abyPCT[i][0] = 0;
401
                                    abyPCT[i][1] = 0;
402
                                    abyPCT[i][2] = 0;
403
                                    abyPCT[i][3] = 255;
404
                                }
405
                                
406
                                for( i = nXSize * nYSize - 1; i >= 0; i-- ){
407
                                    scanLineBufG[i] = abyPCT[scanLineBufR[i]][1];
408
                                    scanLineBufB[i] = abyPCT[scanLineBufR[i]][2];
409
                                           scanLineBufA[i] = abyPCT[scanLineBufR[i]][3];
410
                                           scanLineBufR[i] = abyPCT[scanLineBufR[i]][0];
411
                                }
412
                                //printf("-%d %d %d %d\n", scanLineBufR[0], scanLineBufG[0], scanLineBufB[0],scanLineBufA[0]);
413
                                  }
414
                                                            
415
                                bytearrayR = (*env)->NewByteArray(env,(int)(BufXSize*BufYSize));
416
                                bytearrayG = (*env)->NewByteArray(env,(int)(BufXSize*BufYSize));
417
                                bytearrayB = (*env)->NewByteArray(env,(int)(BufXSize*BufYSize));
418
                                bytearrayA = (*env)->NewByteArray(env,(int)(BufXSize*BufYSize));
419
                                if(bytearrayR!=NULL){
420
                                          (*env)->SetByteArrayRegion(env, bytearrayR, 0, (int)(BufXSize*BufYSize),(jbyte *)scanLineBufR); 
421
                                          (*env)->SetObjectField(env, obj_gdalbuffer, id_campoR, bytearrayR);
422
                                }
423
                                if(scanLineBufR != NULL)
424
                                        CPLFree(scanLineBufR);
425
                                        
426
                                if(bytearrayG!=NULL){
427
                                          (*env)->SetByteArrayRegion(env, bytearrayG, 0, (int)(BufXSize*BufYSize),(jbyte *)scanLineBufG); 
428
                                          (*env)->SetObjectField(env, obj_gdalbuffer, id_campoG, bytearrayG);
429
                                }
430
                                if(scanLineBufG != NULL)
431
                                        CPLFree(scanLineBufG);
432
                                        
433
                                if(bytearrayB!=NULL){
434
                                          (*env)->SetByteArrayRegion(env, bytearrayB, 0, (int)(BufXSize*BufYSize),(jbyte *)scanLineBufB); 
435
                                          (*env)->SetObjectField(env, obj_gdalbuffer, id_campoB, bytearrayB);
436
                                }
437
                                if(scanLineBufB != NULL)
438
                                        CPLFree(scanLineBufB);
439
                                        
440
                                if(bytearrayA!=NULL){
441
                                          (*env)->SetByteArrayRegion(env, bytearrayA, 0, (int)(BufXSize*BufYSize),(jbyte *)scanLineBufA); 
442
                                          (*env)->SetObjectField(env, obj_gdalbuffer, id_campoA, bytearrayA);
443
                                }
444
                                if(scanLineBufA != NULL)
445
                                        CPLFree(scanLineBufA);
446
                                
447
                          }
448
                  }
449
                  //CPLFree(hTable);
450

    
451
                  return obj_gdalbuffer;
452
                  
453
  } 
454
  
455
/******************************************************************************/
456
//                                                                GetRasterColorTable
457
/******************************************************************************/
458

    
459
JNIEXPORT jlong JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_getRasterColorTableNat
460
  (JNIEnv *env, jobject obj, jlong cPtr){
461

    
462
        GDALColorTableH hTable = NULL;
463
        jlong jresult = 0;
464
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
465
          
466
          rb = *(GDALRasterBandH **)&cPtr;
467
        hTable = GDALGetRasterColorTable( rb );
468
    
469
    *(GDALColorTableH **)&jresult = hTable;
470
        
471
    if(hTable == NULL)
472
            return -1;
473
    else 
474
            return jresult;
475
  }
476
  
477
/******************************************************************************/
478
//                                                                GetBlockXSize
479
/******************************************************************************/
480

    
481

    
482
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getBlockXSizeNat
483
  (JNIEnv *env, jobject obj, jlong cPtr){
484
          
485
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
486
          int pnXSize=-1,pnYSize=-1;
487
                    
488
    rb = *(GDALRasterBandH **)&cPtr;
489
    
490
    if(rb!=NULL)
491
            GDALGetBlockSize( rb,&pnXSize, &pnYSize );
492

    
493
    return pnXSize;
494
  }
495

    
496

    
497
/******************************************************************************/     
498
//                                                                WriteRaster
499
/******************************************************************************/  
500
  
501
JNIEXPORT void JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_writeRasterNat
502
  (JNIEnv *env, jobject obj, jlong cPtr, jint nXOff, jint nYOff, jint nXSize, jint nYSize, jobject buffer, jint eBufType){
503
          
504
                  GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
505
                  jclass clase;
506
                  jfieldID id_campo;
507
                  jmethodID metodo;
508
                  jbyteArray bytearray;
509
                  jshortArray shortarray;
510
                  jintArray intarray;
511
                  jfloatArray floatarray;
512
                  jdoubleArray doublearray;
513
                  
514
                  int tam;
515
                  unsigned char *byteBuf;
516
                  unsigned short *shortBuf;
517
                  unsigned int *intBuf;
518
                  float *floatBuf;
519
                  double *doubleBuf;
520
                  
521
                char funcion[11];
522
                char signature[3];
523
                
524
                switch(eBufType){
525
                        case 1:        
526
                                        strncpy(funcion,"buffByte\0",9);
527
                                        strncpy(signature,"[B\0",3);
528
                                        break;
529
                        case 2:        
530
                        case 3:        
531
                        case 8:
532
                                        strncpy(funcion,"buffShort\0",10);
533
                                        strncpy(signature,"[S\0",3);
534
                                        break;
535
                        case 4:        
536
                        case 5:        
537
                        case 9:        
538
                                        strncpy(funcion,"buffInt\0",8);
539
                                        strncpy(signature,"[I\0",3);
540
                                        break;
541
                        case 6:        
542
                        case 10:
543
                                        strncpy(funcion,"buffFloat\0",10);
544
                                        strncpy(signature,"[F\0",3);
545
                                        break;        
546
                        case 7:        
547
                        case 11:
548
                                        strncpy(funcion,"buffDouble\0",11);
549
                                        strncpy(signature,"[D\0",3);
550
                                        break;        
551
                }
552
                  
553
                  
554
                  clase = (*env)->GetObjectClass(env, buffer);
555
                  id_campo = (*env)->GetFieldID(env, clase, funcion, signature);
556
                  metodo = (*env)->GetMethodID(env, clase, "getSize","()I");
557
                  tam = (*env)->CallIntMethod(env,buffer,metodo);
558
                  
559
                  rb = *(GDALRasterBandH **)&cPtr;
560
                  
561
                  switch(eBufType){
562
                        case 1:        
563
                                bytearray =(jbyteArray)(*env)->GetObjectField(env, buffer, id_campo); 
564
                                  byteBuf = (unsigned char *)malloc(sizeof(unsigned char)*tam);
565
                                  if(bytearray!=NULL)
566
                                      (*env)->GetByteArrayRegion(env, bytearray, 0, tam, (jbyte *)byteBuf); 
567
                              if(rb != NULL)
568
                                        GDALRasterIO( rb, GF_Write, nXOff, nYOff, nXSize, nYSize, byteBuf, nXSize, nYSize, GDT_Byte, 0, 0 );          
569
                        free(byteBuf);
570
                                break;
571
                        case 2:        
572
                        case 3:        
573
                        case 8:
574
                                shortarray = (jshortArray)(*env)->GetObjectField(env, buffer, id_campo); 
575
                                  shortBuf = (unsigned short *)malloc(sizeof(unsigned short) * tam);
576
                                  if(shortarray!=NULL)
577
                                      (*env)->GetShortArrayRegion(env, shortarray, 0, tam, (jshort *)shortBuf); 
578
                                 if(rb!=NULL)
579
                                        GDALRasterIO( rb, GF_Write, nXOff, nYOff, nXSize, nYSize, shortBuf, nXSize, nYSize, GDT_Int16, 0, 0 );          
580
                        free(shortBuf);         
581
                                break;
582
                        case 4:        
583
                        case 5:        
584
                        case 9:        
585
                                intarray = (jintArray)(*env)->GetObjectField(env, buffer, id_campo); 
586
                                  intBuf = (unsigned int *)malloc(sizeof(unsigned int) * tam);
587
                                  if(intarray!=NULL)
588
                                      (*env)->GetIntArrayRegion(env, intarray, 0, tam, (jint *)intBuf); 
589
                                  if(rb!=NULL)
590
                                        GDALRasterIO( rb, GF_Write, nXOff, nYOff, nXSize, nYSize, intBuf, nXSize, nYSize, GDT_Int32, 0, 0 );          
591
                        free(intBuf);
592
                                break;
593
                        case 6:        
594
                        case 10:
595
                                floatarray = (jfloatArray)(*env)->GetObjectField(env, buffer, id_campo); 
596
                                  floatBuf = (float *)malloc(sizeof(float) * tam);
597
                                  if(floatarray!=NULL)
598
                                      (*env)->GetFloatArrayRegion(env, floatarray, 0, tam, (jfloat *)floatBuf); 
599
                                  if(rb!=NULL)
600
                                        GDALRasterIO( rb, GF_Write, nXOff, nYOff, nXSize, nYSize, floatBuf, nXSize, nYSize, GDT_Float32, 0, 0 );          
601
                        free(floatBuf);
602
                                break;
603
                        case 7:        
604
                        case 11:
605
                                doublearray = (jdoubleArray)(*env)->GetObjectField(env, buffer, id_campo); 
606
                                  doubleBuf = (double *)malloc(sizeof(double) * tam);
607
                                  if(doublearray!=NULL)
608
                                      (*env)->GetDoubleArrayRegion(env, doublearray, 0, tam, (jdouble *)doubleBuf); 
609
                                  if(rb!=NULL)
610
                                        GDALRasterIO( rb, GF_Write, nXOff, nYOff, nXSize, nYSize, doubleBuf, nXSize, nYSize, GDT_Float64, 0, 0 );          
611
                        free(doubleBuf);
612
                                break;
613
                                
614
                }
615
                    
616
  }
617
    
618
/******************************************************************************/
619
//                                                                GetBlockYSize
620
/******************************************************************************/
621

    
622

    
623
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getBlockYSizeNat
624
  (JNIEnv *env, jobject obj, jlong cPtr){
625
          
626
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
627
          int pnXSize=-1,pnYSize=-1;
628
                    
629
    rb = *(GDALRasterBandH **)&cPtr;
630
    
631
    if(rb!=NULL)
632
            GDALGetBlockSize( rb,&pnXSize, &pnYSize );
633

    
634
    return pnYSize;
635
  }
636
  
637
/******************************************************************************/
638
//                                                                GetRasterDataType
639
/******************************************************************************/  
640
  
641
  JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterDataTypeNat
642
  (JNIEnv *env, jobject obj, jlong cPtr){
643
          
644
          int datatype = -1;
645
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
646
          
647
          rb = *(GDALRasterBandH **)&cPtr;
648
          
649
          if(rb!=NULL)
650
            datatype = GDALGetRasterDataType( rb );
651
 
652
    return datatype;
653
  }
654
  
655
/******************************************************************************/
656
//                                                                GetRasterNoDataValue
657
/******************************************************************************/  
658
  
659
JNIEXPORT jdouble JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_getRasterNoDataValueNat
660
  (JNIEnv *env, jobject obj, jlong cPtr){
661
          
662
          double nodata = -1;
663
          int bGotNodata = 0;
664
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
665
          
666
          rb = *(GDALRasterBandH **)&cPtr;
667
          
668
          if(rb!=NULL)
669
            nodata = GDALGetRasterNoDataValue( rb, &bGotNodata );
670
         
671
    return nodata;
672
  }
673
  
674
/******************************************************************************/
675
//                                                                ExistsNoDataValue
676
/******************************************************************************/  
677
  
678
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_existsNoDataValueNat
679
  (JNIEnv *env, jobject obj, jlong cPtr){
680

    
681
          int bGotNodata = 0;
682
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
683
          
684
          rb = *(GDALRasterBandH **)&cPtr;
685
          
686
          if(rb!=NULL)
687
            GDALGetRasterNoDataValue( rb, &bGotNodata );
688
         
689
    return bGotNodata;
690
  }
691
  
692
/******************************************************************************/ 
693
//                                                                 GetMetadata
694
/******************************************************************************/
695

    
696
JNIEXPORT jobjectArray JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_getMetadataNat
697
  (JNIEnv *env, jobject obj, jlong cPtr, jstring pszDomain){
698
          
699
           char                **papszMetadata;
700
           GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
701
           int i, nmetadatos;
702
           
703
           jclass class_string;
704
           jobjectArray vector_str;
705
           
706
           //Obtenemos los metadatos sobre papszMetadata
707
            
708
           rb = *(GDALRasterBandH **)&cPtr;
709
                    
710
           
711
           if(rb!=NULL){
712
                 papszMetadata = GDALGetMetadata( rb, NULL );
713
                 
714
                 
715
                 //Si hay metadatos devolvemos creamos el vector de String de java y los devolvemos
716
                 
717
                 nmetadatos = CSLCount(papszMetadata);
718
             if( nmetadatos > 0 )
719
              {
720
                class_string = (*env)->FindClass (env, "java/lang/String");
721
                         vector_str=(*env)->NewObjectArray(env, nmetadatos, class_string, (*env)->NewStringUTF(env,""));
722
                         
723
                         //Cargamos los metadatos
724
                         
725
                for( i = 0; papszMetadata[i] != NULL; i++ )
726
                        (*env)->SetObjectArrayElement(env,vector_str,i,(*env)->NewStringUTF(env,papszMetadata[i]));
727
                                        
728
                           return vector_str;            
729
              }
730
           }
731
     
732
           return NULL;
733
  }
734
  
735
/******************************************************************************/ 
736
//                                                        GetRasterColorInterpretation
737
/******************************************************************************/
738

    
739
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_getRasterColorInterpretationNat
740
  (JNIEnv *env, jobject obj, jlong cPtr){
741
           GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
742
          int interp;
743
          
744
          rb = *(GDALRasterBandH **)&cPtr;
745
         interp = GDALGetRasterColorInterpretation(rb); 
746
         
747
         return (jint)interp;
748
  }
749

    
750

    
751
/******************************************************************************/ 
752
//                                                        SetRasterColorInterpretation
753
/******************************************************************************/
754

    
755
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_setRasterColorInterpretationNat
756
        (JNIEnv *env, jobject obj, jlong cPtr, jint bandType){
757
        GDALRasterBandH *rb = (GDALRasterBandH *) 0;
758
        jint err;
759
        GDALColorInterp interp = (GDALColorInterp)bandType;
760
        
761
        rb = *(GDALRasterBandH **)&cPtr;
762
        
763
        if (rb != NULL){
764
                err = GDALSetRasterColorInterpretation(rb, GCI_CyanBand);
765
                }
766
                
767
        return err;
768
        
769
        }
770