1 /******************************************************************************
2  * $Id: gdal_java.i 92caa0d0f67bdff72b5f272d1558b1909bdbeaea 2021-03-31 14:38:01 +0200 Even Rouault $
3  *
4  * Name:     gdal_java.i
5  * Project:  GDAL SWIG Interface
6  * Purpose:  Typemaps for Java bindings
7  * Author:   Benjamin Collins, The MITRE Corporation
8  *
9 */
10 
11 %include java_exceptions.i
12 
13 %pragma(java) jniclasscode=%{
14   private static boolean available = false;
15 
16   static {
17     try {
18       System.loadLibrary("gdalalljni");
19       available = true;
20 
21       if (gdal.HasThreadSupport() == 0)
22       {
23         System.err.println("WARNING : GDAL should be compiled with thread support for safe execution in Java.");
24       }
25 
catch(UnsatisfiedLinkError e)26     } catch (UnsatisfiedLinkError e) {
27       available = false;
28       System.err.println("Native library load failed.");
29       System.err.println(e);
30     }
31   }
32 
isAvailable()33   public static boolean isAvailable() {
34     return available;
35   }
36 %}
37 
38 /* This hacks turns the gdalJNI class into a package private class */
39 %pragma(java) jniclassimports=%{
40 import org.gdal.osr.SpatialReference;
41 import org.gdal.ogr.Geometry;
42 import org.gdal.ogr.StyleTable;
43 import org.gdal.ogr.Layer;
44 import org.gdal.ogr.Feature;
45 import org.gdal.ogr.FieldDomain;
46 %}
47 
48 %pragma(java) moduleimports=%{
49 import org.gdal.osr.SpatialReference;
50 import org.gdal.ogr.Geometry;
51 import org.gdal.ogr.StyleTable;
52 import org.gdal.ogr.Layer;
53 import org.gdal.ogr.Feature;
54 import org.gdal.ogr.FieldDomain;
55 %}
56 
57 %typemap(javaimports) GDALDatasetShadow %{
58 import org.gdal.osr.SpatialReference;
59 import org.gdal.ogr.Geometry;
60 import org.gdal.ogr.StyleTable;
61 import org.gdal.ogr.Layer;
62 import org.gdal.ogr.Feature;
63 import org.gdal.ogr.FieldDomain;
64 %}
65 
66 %typemap(javaimports) GDALMDArrayHS %{
67 import org.gdal.osr.SpatialReference;
68 %}
69 
70 
71 %pragma(java) modulecode=%{
72 
73     /* Uninstanciable class */
gdal()74     private gdal()
75     {
76     }
77 
GeneralCmdLineProcessor(String[]args,int nOptions)78     public static String[] GeneralCmdLineProcessor(String[] args, int nOptions)
79     {
80         java.util.Vector vArgs = new java.util.Vector();
81         int i;
82         for(i=0;i<args.length;i++)
83             vArgs.addElement(args[i]);
84 
85         vArgs = GeneralCmdLineProcessor(vArgs, nOptions);
86         java.util.Enumeration eArgs = vArgs.elements();
87         args = new String[vArgs.size()];
88         i = 0;
89         while(eArgs.hasMoreElements())
90         {
91             String arg = (String)eArgs.nextElement();
92             args[i++] = arg;
93         }
94 
95         return args;
96     }
97 
GeneralCmdLineProcessor(String[]args)98     public static String[] GeneralCmdLineProcessor(String[] args)
99     {
100         return GeneralCmdLineProcessor(args, 0);
101     }
102 
InvGeoTransform(double[]gt_in)103     public static double[] InvGeoTransform(double[] gt_in)
104     {
105       double gt_out[] = new double[6];
106       if (InvGeoTransform(gt_in, gt_out) == 1)
107         return gt_out;
108       else
109         return null;
110     }
111 %}
112 
113 %typemap(javacode) GDAL_GCP %{
GCP(double x,double y,double z,double pixel,double line)114   public GCP(double x, double y, double z, double pixel, double line)
115   {
116       this(x, y, z, pixel, line, "", "");
117   }
118 
GCP(double x,double y,double pixel,double line,String info,String id)119   public GCP(double x, double y, double pixel, double line, String info, String id)
120   {
121       this(x, y, 0.0, pixel, line, info, id);
122   }
123 
GCP(double x,double y,double pixel,double line)124   public GCP(double x, double y, double pixel, double line)
125   {
126       this(x, y, 0.0, pixel, line, "", "");
127   }
128 %}
129 
130 
131 %typemap(javaimports) GDALDriverShadow %{
132 import java.util.Vector;
133 import org.gdal.gdalconst.gdalconstConstants;
134 %}
135 
136 %typemap(javacode) GDALDriverShadow %{
137 
StringArrayToVector(String[]options)138   private static Vector StringArrayToVector(String[] options)
139   {
140       if (options == null)
141         return null;
142       Vector v = new Vector();
143       for(int i=0;i<options.length;i++)
144         v.addElement(options[i]);
145       return v;
146   }
147 
Create(String name,int xsize,int ysize,int bands,int eType,String[]options)148   public Dataset Create(String name, int xsize, int ysize, int bands, int eType, String[] options) {
149     return Create(name, xsize, ysize, bands, eType, StringArrayToVector(options));
150   }
151 
Create(String name,int xsize,int ysize,int bands,String[]options)152   public Dataset Create(String name, int xsize, int ysize, int bands, String[] options) {
153     return Create(name, xsize, ysize, bands, gdalconstConstants.GDT_Byte, StringArrayToVector(options));
154   }
155 
CreateCopy(String name,Dataset src,int strict,String[]options)156   public Dataset CreateCopy(String name, Dataset src, int strict, String[] options) {
157     return CreateCopy(name, src, strict, StringArrayToVector(options), null);
158   }
159 
CreateCopy(String name,Dataset src,Vector options)160   public Dataset CreateCopy(String name, Dataset src, Vector options) {
161     return CreateCopy(name, src, 1, options, null);
162   }
163 
CreateCopy(String name,Dataset src,String[]options)164   public Dataset CreateCopy(String name, Dataset src, String[] options) {
165     return CreateCopy(name, src, 1, StringArrayToVector(options), null);
166   }
167 
168 %}
169 
170 %typemap(javacode) GDALDatasetShadow %{
171 
172   // Preferred name to match C++ API
GetRasterXSize()173   public int GetRasterXSize() { return getRasterXSize(); }
174 
175   // Preferred name to match C++ API
GetRasterYSize()176   public int GetRasterYSize() { return getRasterYSize(); }
177 
178   // Preferred name to match C++ API
GetRasterCount()179   public int GetRasterCount() { return getRasterCount(); }
180 
BuildOverviews(int[]overviewlist,ProgressCallback callback)181   public int BuildOverviews(int[] overviewlist, ProgressCallback callback) {
182     return BuildOverviews(null, overviewlist, callback);
183   }
184 
BuildOverviews(int[]overviewlist)185   public int BuildOverviews(int[] overviewlist) {
186     return BuildOverviews(null, overviewlist, null);
187   }
188 
GetGCPs()189   public java.util.Vector GetGCPs() {
190       java.util.Vector gcps = new java.util.Vector();
191       GetGCPs(gcps);
192       return gcps;
193   }
194 
GetGeoTransform()195   public double[] GetGeoTransform() {
196       double adfGeoTransform[] = new double[6];
197       GetGeoTransform(adfGeoTransform);
198       return adfGeoTransform;
199   }
200 
GetLayer(int index)201   public Layer GetLayer(int index)
202   {
203       return GetLayerByIndex(index);
204   }
205 
GetLayer(String layerName)206   public Layer GetLayer(String layerName)
207   {
208       return GetLayerByName(layerName);
209   }
210 %}
211 
212 %{
BandBlockReadWrite_Validate(GDALRasterBandH self,void * nioBuffer,long nioBufferSize)213     static CPLErr BandBlockReadWrite_Validate(GDALRasterBandH self, void *nioBuffer, long nioBufferSize)
214     {
215         int nBlockXSize, nBlockYSize;
216         GDALDataType eDataType;
217         GDALGetBlockSize(self, &nBlockXSize, &nBlockYSize);
218         eDataType = GDALGetRasterDataType(self);
219         int nDataTypeSize = GDALGetDataTypeSize( eDataType ) / 8;
220         if (nBlockXSize > (INT_MAX / nDataTypeSize) / nBlockYSize)
221         {
222             CPLError(CE_Failure, CPLE_AppDefined, "Integer overflow");
223             return CE_Failure;
224         }
225         if (nioBufferSize < nBlockXSize * nBlockYSize * nDataTypeSize)
226         {
227             CPLError(CE_Failure, CPLE_AppDefined, "Buffer not big enough");
228             return CE_Failure;
229         }
230         return CE_None;
231     }
232 %}
233 
234 %{
235 
236 static
237 GIntBig ComputeDatasetRasterIOSize (int buf_xsize, int buf_ysize, int nPixelSize,
238                                 int nBands, int* bandMap, int nBandMapArrayLength,
239                                 GIntBig nPixelSpace, GIntBig nLineSpace, GIntBig nBandSpace,
240                                 int bSpacingShouldBeMultipleOfPixelSize );
241 
DatasetRasterIO(GDALDatasetH hDS,GDALRWFlag eRWFlag,int xoff,int yoff,int xsize,int ysize,int buf_xsize,int buf_ysize,GDALDataType buf_type,void * regularArray,long nRegularArraySize,int band_list,int * pband_list,int nPixelSpace,int nLineSpace,int nBandSpace,GDALDataType gdal_type,size_t sizeof_ctype)242 static CPLErr DatasetRasterIO( GDALDatasetH hDS, GDALRWFlag eRWFlag,
243                             int xoff, int yoff, int xsize, int ysize,
244                             int buf_xsize, int buf_ysize,
245                             GDALDataType buf_type,
246                             void *regularArray, long nRegularArraySize,
247                             int band_list, int *pband_list,
248                             int nPixelSpace, int nLineSpace, int nBandSpace,
249                             GDALDataType gdal_type, size_t sizeof_ctype)
250 {
251     if ((gdal_type == GDT_Int16 && buf_type != GDT_Int16 && buf_type != GDT_UInt16 && buf_type != GDT_CInt16) ||
252         (gdal_type == GDT_Int32 && buf_type != GDT_Int32 && buf_type != GDT_UInt32 && buf_type != GDT_CInt32) ||
253         (gdal_type == GDT_Float32 && buf_type != GDT_Float32 && buf_type != GDT_CFloat32) ||
254         (gdal_type == GDT_Float64 && buf_type != GDT_Float64 && buf_type != GDT_CFloat64))
255   {
256       CPLError(CE_Failure, CPLE_AppDefined,
257               "Java array type is not compatible with GDAL data type");
258       return CE_Failure;
259   }
260 
261   if (band_list == 0)
262   {
263       if (pband_list != NULL)
264           return CE_Failure;
265 
266       band_list = GDALGetRasterCount(hDS);
267   }
268 
269   GIntBig nMinBufferSizeInBytes = ComputeDatasetRasterIOSize (
270                          buf_xsize, buf_ysize, GDALGetDataTypeSize(buf_type) / 8,
271                          band_list, pband_list, band_list,
272                          nPixelSpace, nLineSpace, nBandSpace, sizeof_ctype > 1 );
273   if (nMinBufferSizeInBytes > 0x7fffffff)
274   {
275      CPLError(CE_Failure, CPLE_IllegalArg, "Integer overflow");
276      nMinBufferSizeInBytes = 0;
277   }
278   if (nMinBufferSizeInBytes == 0)
279       return CE_Failure;
280   if (nRegularArraySize < nMinBufferSizeInBytes)
281   {
282       CPLError(CE_Failure, CPLE_AppDefined,
283               "Buffer is too small");
284       return CE_Failure;
285   }
286   return  GDALDatasetRasterIO( hDS, eRWFlag, xoff, yoff, xsize, ysize,
287                                 regularArray, buf_xsize, buf_ysize,
288                                 buf_type, band_list, pband_list, nPixelSpace, nLineSpace, nBandSpace );
289 
290 }
291 
292 
293 %}
294 
295 
296 %extend GDALDatasetShadow {
297 %apply (int nList, int* pList) { (int band_list, int *pband_list) };
298 %apply (void* nioBuffer, long nioBufferSize) { (void* nioBuffer, long nioBufferSize) };
299 
300 %apply (char *regularArrayOut, long nRegularArraySizeOut) { (char *regularArrayOut, long nRegularArraySizeOut) };
301 %apply (short *regularArrayOut, long nRegularArraySizeOut) { (short *regularArrayOut, long nRegularArraySizeOut) };
302 %apply (int *regularArrayOut, long nRegularArraySizeOut) { (int *regularArrayOut, long nRegularArraySizeOut) };
303 %apply (float *regularArrayOut, long nRegularArraySizeOut) { (float *regularArrayOut, long nRegularArraySizeOut) };
304 %apply (double *regularArrayOut, long nRegularArraySizeOut) { (double *regularArrayOut, long nRegularArraySizeOut) };
305 
306 %apply (char *regularArrayIn, long nRegularArraySizeIn) { (char *regularArrayIn, long nRegularArraySizeIn) };
307 %apply (short *regularArrayIn, long nRegularArraySizeIn) { (short *regularArrayIn, long nRegularArraySizeIn) };
308 %apply (int *regularArrayIn, long nRegularArraySizeIn) { (int *regularArrayIn, long nRegularArraySizeIn) };
309 %apply (float *regularArrayIn, long nRegularArraySizeIn) { (float *regularArrayIn, long nRegularArraySizeIn) };
310 %apply (double *regularArrayIn, long nRegularArraySizeIn) { (double *regularArrayIn, long nRegularArraySizeIn) };
311 
312   CPLErr ReadRaster_Direct( int xoff, int yoff, int xsize, int ysize,
313                             int buf_xsize, int buf_ysize,
314                             GDALDataType buf_type,
315                             void *nioBuffer, long nioBufferSize,
316                             int band_list, int *pband_list,
317                             int nPixelSpace = 0, int nLineSpace = 0, int nBandSpace = 0)
318 {
319     return DatasetRasterIO( (GDALDatasetH)self, GF_Read,
320                               xoff, yoff, xsize, ysize,
321                               buf_xsize, buf_ysize,
322                               buf_type,
323                               nioBuffer, nioBufferSize,
324                               band_list, pband_list,
325                               nPixelSpace, nLineSpace, nBandSpace,
326                               GDT_Unknown, 0);
327 }
328 
329 
330   %define DEFINE_DS_READ_RASTER(ctype, gdal_type)
331 CPLErr ReadRaster( int xoff, int yoff, int xsize, int ysize,
332                             int buf_xsize, int buf_ysize,
333                             GDALDataType buf_type,
334                             ctype *regularArrayOut, long nRegularArraySizeOut,
335                             int band_list, int *pband_list,
336                             int nPixelSpace = 0, int nLineSpace = 0, int nBandSpace = 0)
337 {
338     return DatasetRasterIO( (GDALDatasetH)self, GF_Read,
339                               xoff, yoff, xsize, ysize,
340                               buf_xsize, buf_ysize,
341                               buf_type,
342                               regularArrayOut, nRegularArraySizeOut,
343                               band_list, pband_list,
344                               nPixelSpace, nLineSpace, nBandSpace,
345                               gdal_type, sizeof(ctype));
346 }
347   %enddef
348 
349   DEFINE_DS_READ_RASTER(char, GDT_Byte)
350   DEFINE_DS_READ_RASTER(short, GDT_Int16)
351   DEFINE_DS_READ_RASTER(int, GDT_Int32)
352   DEFINE_DS_READ_RASTER(float, GDT_Float32)
353   DEFINE_DS_READ_RASTER(double, GDT_Float64)
354 
355 
356   CPLErr WriteRaster_Direct( int xoff, int yoff, int xsize, int ysize,
357                             int buf_xsize, int buf_ysize,
358                             GDALDataType buf_type,
359                             void *nioBuffer, long nioBufferSize,
360                             int band_list, int *pband_list,
361                             int nPixelSpace = 0, int nLineSpace = 0, int nBandSpace = 0)
362 {
363     return DatasetRasterIO( (GDALDatasetH)self, GF_Write,
364                               xoff, yoff, xsize, ysize,
365                               buf_xsize, buf_ysize,
366                               buf_type,
367                               nioBuffer, nioBufferSize,
368                               band_list, pband_list,
369                               nPixelSpace, nLineSpace, nBandSpace,
370                               GDT_Unknown, 0);
371 }
372 
373   %define DEFINE_DS_WRITE_RASTER(ctype, gdal_type)
374   CPLErr WriteRaster( int xoff, int yoff, int xsize, int ysize,
375                             int buf_xsize, int buf_ysize,
376                             GDALDataType buf_type,
377                             ctype *regularArrayIn, long nRegularArraySizeIn,
378                             int band_list, int *pband_list,
379                             int nPixelSpace = 0, int nLineSpace = 0, int nBandSpace = 0)
380 {
381     return DatasetRasterIO( (GDALDatasetH)self, GF_Write,
382                               xoff, yoff, xsize, ysize,
383                               buf_xsize, buf_ysize,
384                               buf_type,
385                               regularArrayIn, nRegularArraySizeIn,
386                               band_list, pband_list,
387                               nPixelSpace, nLineSpace, nBandSpace,
388                               gdal_type, sizeof(ctype));
389 }
390  %enddef
391 
392   DEFINE_DS_WRITE_RASTER(char, GDT_Byte)
393   DEFINE_DS_WRITE_RASTER(short, GDT_Int16)
394   DEFINE_DS_WRITE_RASTER(int, GDT_Int32)
395   DEFINE_DS_WRITE_RASTER(float, GDT_Float32)
396   DEFINE_DS_WRITE_RASTER(double, GDT_Float64)
397 
398 %clear (int band_list, int *pband_list);
399 
400 } /* extend */
401 
402 
403 %{
404 static
405 GIntBig ComputeBandRasterIOSize (int buf_xsize, int buf_ysize, int nPixelSize,
406                                  GIntBig nPixelSpace, GIntBig nLineSpace,
407                                  int bSpacingShouldBeMultipleOfPixelSize );
408 
BandRasterIO(GDALRasterBandH hBand,GDALRWFlag eRWFlag,int xoff,int yoff,int xsize,int ysize,int buf_xsize,int buf_ysize,GDALDataType buf_type,void * regularArrayOut,long nRegularArraySizeOut,int nPixelSpace,int nLineSpace,GDALDataType gdal_type,size_t sizeof_ctype)409 static CPLErr BandRasterIO( GDALRasterBandH hBand, GDALRWFlag eRWFlag,
410                             int xoff, int yoff, int xsize, int ysize,
411                             int buf_xsize, int buf_ysize,
412                             GDALDataType buf_type,
413                             void *regularArrayOut, long nRegularArraySizeOut,
414                             int nPixelSpace, int nLineSpace,
415                             GDALDataType gdal_type, size_t sizeof_ctype)
416 {
417     if ((gdal_type == GDT_Int16 && buf_type != GDT_Int16 && buf_type != GDT_UInt16 && buf_type != GDT_CInt16) ||
418         (gdal_type == GDT_Int32 && buf_type != GDT_Int32 && buf_type != GDT_UInt32 && buf_type != GDT_CInt32) ||
419         (gdal_type == GDT_Float32 && buf_type != GDT_Float32 && buf_type != GDT_CFloat32) ||
420         (gdal_type == GDT_Float64 && buf_type != GDT_Float64 && buf_type != GDT_CFloat64))
421     {
422         CPLError(CE_Failure, CPLE_AppDefined,
423                 "Java array type is not compatible with GDAL data type");
424         return CE_Failure;
425     }
426 
427     GIntBig nMinBufferSizeInBytes = ComputeBandRasterIOSize (
428                             buf_xsize, buf_ysize, GDALGetDataTypeSize(buf_type) / 8,
429                             nPixelSpace, nLineSpace, sizeof_ctype > 1 );
430     if (nMinBufferSizeInBytes > 0x7fffffff)
431     {
432        CPLError(CE_Failure, CPLE_IllegalArg, "Integer overflow");
433        nMinBufferSizeInBytes = 0;
434     }
435     if (nMinBufferSizeInBytes == 0)
436         return CE_Failure;
437     if (nRegularArraySizeOut < nMinBufferSizeInBytes)
438     {
439         CPLError(CE_Failure, CPLE_AppDefined,
440                 "Buffer is too small");
441         return CE_Failure;
442     }
443 
444     return GDALRasterIO( hBand, eRWFlag, xoff, yoff, xsize, ysize,
445                                    regularArrayOut, buf_xsize, buf_ysize,
446                                    buf_type, nPixelSpace, nLineSpace );
447 }
448 
449 %}
450 
451 %extend GDALRasterBandShadow {
452   CPLErr ReadRaster_Direct( int xoff, int yoff, int xsize, int ysize,
453                             int buf_xsize, int buf_ysize,
454                             GDALDataType buf_type,
455                             void *nioBuffer, long nioBufferSize,
456                             int nPixelSpace = 0, int nLineSpace = 0)
457   {
458     return BandRasterIO( self, GF_Read,
459                          xoff, yoff, xsize, ysize,
460                          buf_xsize, buf_ysize,
461                          buf_type,
462                          nioBuffer, nioBufferSize,
463                          nPixelSpace, nLineSpace,
464                          GDT_Unknown, 0 );
465   }
466 
467   %define DEFINE_READ_RASTER(ctype, gdal_type)
468   CPLErr ReadRaster( int xoff, int yoff, int xsize, int ysize,
469                      int buf_xsize, int buf_ysize,
470                      GDALDataType buf_type,
471                      ctype *regularArrayOut, long nRegularArraySizeOut,
472                      int nPixelSpace = 0, int nLineSpace = 0)
473   {
474     return BandRasterIO( self, GF_Read,
475                          xoff, yoff, xsize, ysize,
476                          buf_xsize, buf_ysize,
477                          buf_type,
478                          regularArrayOut, nRegularArraySizeOut,
479                          nPixelSpace, nLineSpace,
480                          gdal_type, sizeof(ctype) );
481   }
482   %enddef
483 
484   DEFINE_READ_RASTER(char, GDT_Byte)
485   DEFINE_READ_RASTER(short, GDT_Int16)
486   DEFINE_READ_RASTER(int, GDT_Int32)
487   DEFINE_READ_RASTER(float, GDT_Float32)
488   DEFINE_READ_RASTER(double, GDT_Float64)
489 
490   CPLErr WriteRaster_Direct( int xoff, int yoff, int xsize, int ysize,
491                             int buf_xsize, int buf_ysize,
492                             GDALDataType buf_type,
493                             void *nioBuffer, long nioBufferSize,
494                             int nPixelSpace = 0, int nLineSpace = 0)
495   {
496     return BandRasterIO( self, GF_Write,
497                          xoff, yoff, xsize, ysize,
498                          buf_xsize, buf_ysize,
499                          buf_type,
500                          nioBuffer, nioBufferSize,
501                          nPixelSpace, nLineSpace,
502                          GDT_Unknown, 0 );
503   }
504 
505   %define DEFINE_WRITE_RASTER(ctype, gdal_type)
506   CPLErr WriteRaster( int xoff, int yoff, int xsize, int ysize,
507                             int buf_xsize, int buf_ysize,
508                             GDALDataType buf_type,
509                             ctype *regularArrayIn, long nRegularArraySizeIn,
510                             int nPixelSpace = 0, int nLineSpace = 0)
511   {
512     return BandRasterIO( self, GF_Write,
513                          xoff, yoff, xsize, ysize,
514                          buf_xsize, buf_ysize,
515                          buf_type,
516                          regularArrayIn, nRegularArraySizeIn,
517                          nPixelSpace, nLineSpace,
518                          gdal_type, sizeof(ctype) );
519   }
520   %enddef
521 
522   DEFINE_WRITE_RASTER(char, GDT_Byte)
523   DEFINE_WRITE_RASTER(short, GDT_Int16)
524   DEFINE_WRITE_RASTER(int, GDT_Int32)
525   DEFINE_WRITE_RASTER(float, GDT_Float32)
526   DEFINE_WRITE_RASTER(double, GDT_Float64)
527 
528   CPLErr ReadBlock_Direct( int nXBlockOff, int nYBlockOff, void *nioBuffer, long nioBufferSize )
529   {
530     if (BandBlockReadWrite_Validate((GDALRasterBandH)self, nioBuffer, nioBufferSize) != CE_None)
531       return CE_Failure;
532 
533     return GDALReadBlock(self, nXBlockOff, nYBlockOff, nioBuffer);
534   }
535 
WriteBlock_Direct(int nXBlockOff,int nYBlockOff,void * nioBuffer,long nioBufferSize)536   CPLErr WriteBlock_Direct( int nXBlockOff, int nYBlockOff, void *nioBuffer, long nioBufferSize )
537   {
538     if (BandBlockReadWrite_Validate((GDALRasterBandH)self, nioBuffer, nioBufferSize) != CE_None)
539       return CE_Failure;
540 
541     return GDALWriteBlock(self, nXBlockOff, nYBlockOff, nioBuffer);
542   }
543 /* %clear (void *nioBuffer, long nioBufferSize); */
544 
545 %clear (char *regularArrayOut, long nRegularArraySizeOut);
546 %clear (short *regularArrayOut, long nRegularArraySizeOut);
547 %clear (int *regularArrayOut, long nRegularArraySizeOut);
548 %clear (float *regularArrayOut, long nRegularArraySizeOut);
549 %clear (double *regularArrayOut, long nRegularArraySizeOut);
550 
551 %clear (char *regularArrayIn, long nRegularArraySizeIn);
552 %clear (short *regularArrayIn, long nRegularArraySizeIn);
553 %clear (int *regularArrayIn, long nRegularArraySizeIn);
554 %clear (float *regularArrayIn, long nRegularArraySizeIn);
555 %clear (double *regularArrayIn, long nRegularArraySizeIn);
556 
557 %apply (int nList, int* pListOut) {(int buckets, int *panHistogram)};
558 %apply Pointer NONNULL { int *panHistogram };
GetHistogram(double min,double max,int buckets,int * panHistogram,bool include_out_of_range,bool approx_ok,GDALProgressFunc callback,void * callback_data)559   CPLErr GetHistogram(double min,
560                      double max,
561                      int buckets,
562                      int *panHistogram,
563                      bool include_out_of_range,
564                      bool approx_ok,
565                      GDALProgressFunc callback,
566                      void* callback_data) {
567     CPLErrorReset();
568     CPLErr err = GDALGetRasterHistogram( self, min, max, buckets, panHistogram,
569                                          include_out_of_range, approx_ok,
570                                          callback, callback_data );
571     return err;
572   }
573 
GetHistogram(double min,double max,int buckets,int * panHistogram,bool include_out_of_range,bool approx_ok)574   CPLErr GetHistogram(double min,
575                       double max,
576                       int buckets,
577                       int *panHistogram,
578                       bool include_out_of_range,
579                       bool approx_ok) {
580     CPLErrorReset();
581     CPLErr err = GDALGetRasterHistogram( self, min, max, buckets, panHistogram,
582                                          include_out_of_range, approx_ok,
583                                          NULL, NULL);
584     return err;
585   }
586 
GetHistogram(double min,double max,int buckets,int * panHistogram)587   CPLErr GetHistogram(double min,
588                       double max,
589                       int buckets,
590                       int *panHistogram) {
591     CPLErrorReset();
592     CPLErr err = GDALGetRasterHistogram( self, min, max, buckets, panHistogram,
593                                          0, 1,
594                                          NULL, NULL);
595     return err;
596   }
597 
GetHistogram(int buckets,int * panHistogram)598   CPLErr GetHistogram(int buckets,
599                         int *panHistogram) {
600     CPLErrorReset();
601     CPLErr err = GDALGetRasterHistogram( self, -0.5, 255.5, buckets, panHistogram,
602                                          0, 1,
603                                          NULL, NULL);
604     return err;
605   }
606 %clear (int buckets, int *panHistogram);
607 
608 %apply (int* pnList, int** ppListOut) {(int* buckets_ret, int **ppanHistogram)};
609 %apply (double *OUTPUT){double *min_ret, double *max_ret}
610   CPLErr GetDefaultHistogram( double *min_ret, double *max_ret, int* buckets_ret,
611                               int **ppanHistogram, bool force = 1,
612                               GDALProgressFunc callback = NULL,
613                               void* callback_data=NULL ) {
614       return GDALGetDefaultHistogram( self, min_ret, max_ret, buckets_ret,
615                                       ppanHistogram, force,
616                                       callback, callback_data );
617   }
618 %clear (double *min_ret, double *max_ret);
619 %clear (int* buckets_ret, int **ppanHistogram);
620 
621 } /* extend */
622 
623 #ifdef SWIGANDROID
624 
625 %typemap(javacode) GDALColorTableShadow %{
626   private Object parentReference;
627 
628   /* Ensure that the GC doesn't collect any parent instance set from Java */
addReference(Object reference)629   protected void addReference(Object reference) {
630     parentReference = reference;
631   }
632 
clone()633   public Object clone()
634   {
635       return Clone();
636   }
637 
638 %}
639 
640 #else
641 
642 %typemap(javaimports) GDALColorTableShadow %{
643 /* imports for getIndexColorModel */
644 import java.awt.image.IndexColorModel;
645 import java.awt.Color;
646 %}
647 
648 %typemap(javacode) GDALColorTableShadow %{
649   private Object parentReference;
650 
651   /* Ensure that the GC doesn't collect any parent instance set from Java */
addReference(Object reference)652   protected void addReference(Object reference) {
653     parentReference = reference;
654   }
655 
clone()656   public Object clone()
657   {
658       return Clone();
659   }
660 
661   // Convenience method.
getIndexColorModel(int bits)662   public IndexColorModel getIndexColorModel(int bits) {
663     int size = GetCount();
664     byte[] reds = new byte[size];
665     byte[] greens = new byte[size];
666     byte[] blues = new byte[size];
667     byte[] alphas = new byte[size];
668     int noAlphas = 0;
669     int zeroAlphas = 0;
670     int lastAlphaIndex = -1;
671 
672     Color entry = null;
673     for(int i = 0; i < size; i++) {
674       entry = GetColorEntry(i);
675       reds[i] = (byte)(entry.getRed()&0xff);
676       greens[i] = (byte)(entry.getGreen()&0xff);
677       blues[i] = (byte)(entry.getBlue()&0xff);
678       byte alpha = (byte)(entry.getAlpha()&0xff);
679 
680       // The byte type is -128 to 127 so a normal 255 will be -1.
681       if (alpha == -1)
682           noAlphas ++;
683       else{
684         if (alpha == 0){
685            zeroAlphas++;
686            lastAlphaIndex = i;
687         }
688       }
689       alphas[i] = alpha;
690     }
691     if (noAlphas == size)
692         return new IndexColorModel(bits, size, reds, greens, blues);
693     else if (noAlphas == (size - 1) && zeroAlphas == 1)
694         return new IndexColorModel(bits, size, reds, greens, blues, lastAlphaIndex);
695     else
696         return new IndexColorModel(bits, size, reds, greens, blues, alphas);
697  }
698 %}
699 
700 #endif
701 
702 %typemap(javaimports) GDALRasterBandShadow %{
703 import org.gdal.gdalconst.gdalconstConstants;
704 %}
705 
706 
707 %typemap(javacode) GDALRasterBandShadow %{
708 
709   // Preferred name to match C++ API
GetXSize()710   public int GetXSize() { return getXSize(); }
711 
712   // Preferred name to match C++ API
GetYSize()713   public int GetYSize() { return getYSize(); }
714 
715   // Preferred name to match C++ API
GetRasterDataType()716   public int GetRasterDataType() { return getDataType(); }
717 
GetBlockXSize()718   public int GetBlockXSize()
719   {
720       int[] anBlockXSize = new int[1];
721       int[] anBlockYSize = new int[1];
722       GetBlockSize(anBlockXSize, anBlockYSize);
723       return anBlockXSize[0];
724   }
725 
GetBlockYSize()726   public int GetBlockYSize()
727   {
728       int[] anBlockXSize = new int[1];
729       int[] anBlockYSize = new int[1];
730       GetBlockSize(anBlockXSize, anBlockYSize);
731       return anBlockYSize[0];
732   }
733 
Checksum()734   public int Checksum() {
735     return Checksum(0, 0, getXSize(), getYSize());
736   }
737 
GetStatistics(boolean approx_ok,boolean force,double[]min,double[]max,double[]mean,double[]stddev)738   public int GetStatistics(boolean approx_ok, boolean force, double[] min, double[] max, double[] mean, double[] stddev) {
739     return GetStatistics((approx_ok) ? 1 : 0, (force) ? 1 : 0, min, max, mean, stddev);
740   }
741 
742    public int ReadRaster_Direct(int xoff, int yoff, int xsize, int ysize,
743                                 int buf_xsize, int buf_ysize, java.nio.ByteBuffer nioBuffer) {
744        return ReadRaster_Direct(xoff, yoff, xsize, ysize, buf_xsize, buf_ysize, gdalconstConstants.GDT_Byte, nioBuffer);
745    }
746 
747    public int ReadRaster_Direct(int xoff, int yoff, int xsize, int ysize,
748                                 java.nio.ByteBuffer nioBuffer) {
749        return ReadRaster_Direct(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Byte, nioBuffer);
750    }
751 
ReadRaster_Direct(int xoff,int yoff,int xsize,int ysize,int buf_xsize,int buf_ysize,int buf_type)752    public java.nio.ByteBuffer ReadRaster_Direct(int xoff, int yoff, int xsize, int ysize,
753                                                 int buf_xsize, int buf_ysize, int buf_type)
754    {
755        long buf_size = buf_xsize * buf_ysize * (gdal.GetDataTypeSize(buf_type) / 8);
756        if ((int)buf_size != buf_size)
757                throw new OutOfMemoryError();
758        java.nio.ByteBuffer nioBuffer = java.nio.ByteBuffer.allocateDirect((int)buf_size);
759        int ret = ReadRaster_Direct(xoff, yoff, xsize, ysize, buf_xsize, buf_ysize, buf_type, nioBuffer);
760        if (ret == gdalconstConstants.CE_None)
761                return nioBuffer;
762        else
763                return null;
764    }
765 
ReadRaster_Direct(int xoff,int yoff,int xsize,int ysize,int buf_type)766    public java.nio.ByteBuffer ReadRaster_Direct(int xoff, int yoff, int xsize, int ysize, int buf_type)
767    {
768        return ReadRaster_Direct(xoff, yoff, xsize, ysize, xsize, ysize, buf_type);
769    }
770 
ReadRaster_Direct(int xoff,int yoff,int xsize,int ysize)771    public java.nio.ByteBuffer ReadRaster_Direct(int xoff, int yoff, int xsize, int ysize)
772    {
773        return ReadRaster_Direct(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Byte);
774    }
775 
ReadRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,byte[]array)776    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, byte[] array) {
777        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
778    }
779 
ReadRaster(int xoff,int yoff,int xsize,int ysize,byte[]array)780    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, byte[] array) {
781        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Byte, array);
782    }
783 
ReadRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,short[]array)784    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, short[] array) {
785        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
786    }
787 
ReadRaster(int xoff,int yoff,int xsize,int ysize,short[]array)788    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, short[] array) {
789        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Int16, array);
790    }
791 
ReadRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,int[]array)792    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, int[] array) {
793        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
794    }
795 
ReadRaster(int xoff,int yoff,int xsize,int ysize,int[]array)796    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, int[] array) {
797        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Int32, array);
798    }
799 
ReadRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,float[]array)800    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, float[] array) {
801        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
802    }
803 
ReadRaster(int xoff,int yoff,int xsize,int ysize,float[]array)804    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, float[] array) {
805        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Float32, array);
806    }
807 
ReadRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,double[]array)808    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, double[] array) {
809        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
810    }
811 
ReadRaster(int xoff,int yoff,int xsize,int ysize,double[]array)812    public int ReadRaster(int xoff, int yoff, int xsize, int ysize, double[] array) {
813        return ReadRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Float64, array);
814    }
815 
816    public int WriteRaster_Direct(int xoff, int yoff, int xsize, int ysize,
817                                 int buf_xsize, int buf_ysize, java.nio.ByteBuffer nioBuffer) {
818        return WriteRaster_Direct(xoff, yoff, xsize, ysize, buf_xsize, buf_ysize, gdalconstConstants.GDT_Byte, nioBuffer);
819    }
820 
821    public int WriteRaster_Direct(int xoff, int yoff, int xsize, int ysize,
822                                  int buf_type, java.nio.ByteBuffer nioBuffer) {
823        return WriteRaster_Direct(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, nioBuffer);
824    }
825 
826    public int WriteRaster_Direct(int xoff, int yoff, int xsize, int ysize,
827                                  java.nio.ByteBuffer nioBuffer) {
828        return WriteRaster_Direct(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Byte, nioBuffer);
829    }
830 
WriteRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,byte[]array)831    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, byte[] array) {
832        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
833    }
834 
WriteRaster(int xoff,int yoff,int xsize,int ysize,byte[]array)835    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, byte[] array) {
836        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Byte, array);
837    }
838 
WriteRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,short[]array)839    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, short[] array) {
840        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
841    }
842 
WriteRaster(int xoff,int yoff,int xsize,int ysize,short[]array)843    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, short[] array) {
844        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Int16, array);
845    }
846 
WriteRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,int[]array)847    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, int[] array) {
848        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
849    }
850 
WriteRaster(int xoff,int yoff,int xsize,int ysize,int[]array)851    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, int[] array) {
852        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Int32, array);
853    }
854 
WriteRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,float[]array)855    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, float[] array) {
856        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
857    }
858 
WriteRaster(int xoff,int yoff,int xsize,int ysize,float[]array)859    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, float[] array) {
860        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Float32, array);
861    }
862 
WriteRaster(int xoff,int yoff,int xsize,int ysize,int buf_type,double[]array)863    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, double[] array) {
864        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, buf_type, array);
865    }
866 
WriteRaster(int xoff,int yoff,int xsize,int ysize,double[]array)867    public int WriteRaster(int xoff, int yoff, int xsize, int ysize, double[] array) {
868        return WriteRaster(xoff, yoff, xsize, ysize, xsize, ysize, gdalconstConstants.GDT_Float64, array);
869    }
870 %}
871 
872 /* Could be disabled as do nothing, but we keep them for backwards compatibility */
873 /*%typemap(javadestruct_derived, methodname="delete", methodmodifiers="public") GDALRasterBandShadow ""
874 %typemap(javadestruct_derived, methodname="delete", methodmodifiers="public") GDALDriverShadow ""
875 %typemap(javadestruct, methodname="delete", methodmodifiers="protected") GDALMajorObjectShadow %{
876   {
877     if(swigCPtr != 0 && swigCMemOwn) {
878       swigCMemOwn = false;
879       throw new UnsupportedOperationException("C++ destructor does not have public access");
880     }
881     swigCPtr = 0;
882   }
883 %}*/
884 
885 // Add a Java reference to prevent premature garbage collection and resulting use
886 // of dangling C++ pointer. Intended for methods that return pointers or
887 // references to a member variable.
888 %typemap(javaout) GDALRasterBandShadow* GetRasterBand,
889                   GDALRasterBandShadow* GetOverview,
890                   GDALRasterBandShadow* GetMaskBand,
891                   GDALColorTableShadow* GetColorTable,
892                   GDALColorTableShadow* GetRasterColorTable,
893                   OGRLayerShadow* CreateLayer,
894                   OGRLayerShadow* CopyLayer,
895                   OGRLayerShadow* GetLayerByIndex,
896                   OGRLayerShadow* GetLayerByName,
897                   OGRLayerShadow* ExecuteSQL,
898                   CPLXMLNode* getChild,
899                   CPLXMLNode* getNext,
900                   CPLXMLNode* GetXMLNode,
901                   CPLXMLNode* SearchXMLNode {
902     long cPtr = $jnicall;
903     $javaclassname ret = null;
904     if (cPtr != 0) {
905       ret = new $javaclassname(cPtr, $owner);
906       ret.addReference(this);
907     }
908     return ret;
909   }
910 
911 %typemap(javainterfaces) GDALColorTableShadow "Cloneable"
912 %typemap(javainterfaces) GDALRasterAttributeTableShadow "Cloneable"
913 
914 %typemap(javacode) GDALRasterAttributeTableShadow %{
915 
clone()916   public Object clone()
917   {
918       return Clone();
919   }
920 %}
921 
922 %typemap(javacode) GDALMajorObjectShadow %{
923   private Object parentReference;
924 
925   /* Ensure that the GC doesn't collect any parent instance set from Java */
addReference(Object reference)926   protected void addReference(Object reference) {
927     parentReference = reference;
928   }
929 
930   /* For backward compatibility */
931   public int SetMetadata(java.util.Hashtable metadata, String domain)
932   {
933       if (metadata == null)
934           return SetMetadata((java.util.Vector)null, domain);
935       java.util.Vector v = new java.util.Vector();
936       java.util.Enumeration values = metadata.elements();
937       java.util.Enumeration keys = metadata.keys();
938       while(keys.hasMoreElements())
939       {
940           v.add((String)keys.nextElement() + "=" + (String)values.nextElement());
941       }
942       return SetMetadata(v, domain);
943   }
944 
945   public int SetMetadata(java.util.Hashtable metadata)
946   {
947       return SetMetadata(metadata, null);
948   }
949 %}
950 
951 %include callback.i
952 
953 %include typemaps_java.i
954