1 /******************************************************************************
2  * $Id: gdal_csharp.i 3e3d90133f1a88ec9ba7f889e3f962fe719d40d5 2020-07-23 10:17:03 +0200 Tamas Szekeres $
3  *
4  * Name:     gdal_csharp.i
5  * Project:  GDAL CSharp Interface
6  * Purpose:  GDAL CSharp SWIG Interface declarations.
7  * Author:   Tamas Szekeres, szekerest@gmail.com
8  *
9  ******************************************************************************
10  * Copyright (c) 2007, Tamas Szekeres
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  *****************************************************************************/
30 
31 %include cpl_exceptions.i
32 
33 %rename (GetMetadata) GetMetadata_List;
34 %ignore GetMetadata_Dict;
35 
36 %include typemaps_csharp.i
37 
38 DEFINE_EXTERNAL_CLASS(OSRSpatialReferenceShadow, OSGeo.OSR.SpatialReference)
39 
40 %apply (int *pList) {int *band_list, int *panHistogram_in};
41 %apply (double *OUTPUT) {double *min_ret, double *max_ret};
42 %apply (int *nLen) {int *buckets_ret};
43 %apply (double *pList) {double *burn_values_list, double *fixedLevels};
44 %apply (int **pList) {int **ppanHistogram};
45 %apply (void *buffer_ptr) {void *pfnTransformer, void *pTransformArg};
46 
47 %apply (void *buffer_ptr) {GDAL_GCP const *pGCPs};
48 %csmethodmodifiers __SetGCPs "private";
49 %csmethodmodifiers __GetGCPs "private";
50 %csmethodmodifiers GDALGCPsToGeoTransform "private";
51 
52 %apply (void *buffer_ptr) {GDALDatasetShadow** poObjects};
53 %csmethodmodifiers wrapper_GDALWarpDestDS "private";
54 %csmethodmodifiers wrapper_GDALWarpDestName "private";
55 
56 %apply (GDALProgressFunc callback) {GDALProgressFunc pfnProgress};
57 %apply (void *buffer_ptr) {void *pProgressData};
58 
59 %rename (RasterIOExtraArg) GDALRasterIOExtraArg;
60 typedef struct
61 {
62     /*! Version of structure (to allow future extensions of the structure) */
63     int                    nVersion;
64 
65     /*! Resampling algorithm */
66     GDALRIOResampleAlg     eResampleAlg;
67 
68     /*! Progress callback */
69     GDALProgressFunc pfnProgress;
70     /*! Progress callback user data */
71     void *pProgressData;
72 
73     /*! Indicate if dfXOff, dfYOff, dfXSize and dfYSize are set.
74         Mostly reserved from the VRT driver to communicate a more precise
75         source window. Must be such that dfXOff - nXOff < 1.0 and
76         dfYOff - nYOff < 1.0 and nXSize - dfXSize < 1.0 and nYSize - dfYSize < 1.0 */
77     int bFloatingPointWindowValidity;
78     /*! Pixel offset to the top left corner. Only valid if bFloatingPointWindowValidity = TRUE */
79     double dfXOff;
80     /*! Line offset to the top left corner. Only valid if bFloatingPointWindowValidity = TRUE */
81     double dfYOff;
82     /*! Width in pixels of the area of interest. Only valid if bFloatingPointWindowValidity = TRUE */
83     double dfXSize;
84     /*! Height in pixels of the area of interest. Only valid if bFloatingPointWindowValidity = TRUE */
85     double dfYSize;
86 } GDALRasterIOExtraArg;
87 
88 DEFINE_EXTERNAL_CLASS(OGRLayerShadow, OSGeo.OGR.Layer)
89 
90 %define %rasterio_functions(GDALTYPE,CSTYPE)
91  public CPLErr ReadRaster(int xOff, int yOff, int xSize, int ySize, CSTYPE[] buffer, int buf_xSize, int buf_ySize, int pixelSpace, int lineSpace) {
92       CPLErr retval;
93       GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
94       try {
95           retval = ReadRaster(xOff, yOff, xSize, ySize, handle.AddrOfPinnedObject(), buf_xSize, buf_ySize, GDALTYPE, pixelSpace, lineSpace);
96       } finally {
97           handle.Free();
98       }
99       GC.KeepAlive(this);
100       return retval;
101   }
WriteRaster(int xOff,int yOff,int xSize,int ySize,CSTYPE[]buffer,int buf_xSize,int buf_ySize,int pixelSpace,int lineSpace)102   public CPLErr WriteRaster(int xOff, int yOff, int xSize, int ySize, CSTYPE[] buffer, int buf_xSize, int buf_ySize, int pixelSpace, int lineSpace) {
103       CPLErr retval;
104       GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
105       try {
106           retval = WriteRaster(xOff, yOff, xSize, ySize, handle.AddrOfPinnedObject(), buf_xSize, buf_ySize, GDALTYPE, pixelSpace, lineSpace);
107       } finally {
108           handle.Free();
109       }
110       GC.KeepAlive(this);
111       return retval;
112   }
ReadRaster(int xOff,int yOff,int xSize,int ySize,CSTYPE[]buffer,int buf_xSize,int buf_ySize,int pixelSpace,int lineSpace,RasterIOExtraArg extraArg)113   public CPLErr ReadRaster(int xOff, int yOff, int xSize, int ySize, CSTYPE[] buffer, int buf_xSize, int buf_ySize, int pixelSpace, int lineSpace, RasterIOExtraArg extraArg) {
114       CPLErr retval;
115       GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
116       try {
117           retval = ReadRaster(xOff, yOff, xSize, ySize, handle.AddrOfPinnedObject(), buf_xSize, buf_ySize, GDALTYPE, pixelSpace, lineSpace, extraArg);
118       } finally {
119           handle.Free();
120       }
121       GC.KeepAlive(this);
122       return retval;
123   }
WriteRaster(int xOff,int yOff,int xSize,int ySize,CSTYPE[]buffer,int buf_xSize,int buf_ySize,int pixelSpace,int lineSpace,RasterIOExtraArg extraArg)124   public CPLErr WriteRaster(int xOff, int yOff, int xSize, int ySize, CSTYPE[] buffer, int buf_xSize, int buf_ySize, int pixelSpace, int lineSpace, RasterIOExtraArg extraArg) {
125       CPLErr retval;
126       GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
127       try {
128           retval = WriteRaster(xOff, yOff, xSize, ySize, handle.AddrOfPinnedObject(), buf_xSize, buf_ySize, GDALTYPE, pixelSpace, lineSpace, extraArg);
129       } finally {
130           handle.Free();
131       }
132       GC.KeepAlive(this);
133       return retval;
134   }
135 
136 %enddef
137 
138 %typemap(cscode, noblock="1") GDALRasterBandShadow {
139 /*! Eight bit unsigned integer */ %rasterio_functions(DataType.GDT_Byte,byte)
140 /*! Sixteen bit signed integer */ %rasterio_functions(DataType.GDT_Int16,short)
141 /*! Thirty two bit signed integer */ %rasterio_functions(DataType.GDT_Int32,int)
142 /*! Thirty two bit floating point */ %rasterio_functions(DataType.GDT_Float32,float)
143 /*! Sixty four bit floating point */ %rasterio_functions(DataType.GDT_Float64,double)
144 }
145 
146 /*! Sixteen bit unsigned integer */ //%rasterio_functions(DataType.GDT_UInt16,ushort)
147 /*! Thirty two bit unsigned integer */ //%rasterio_functions(DataType.GDT_UInt32,uint)
148 /*! Complex Int16 */ //%rasterio_functions(DataType.GDT_CInt16,int)
149 /*! Complex Int32 */ //%rasterio_functions(DataType.GDT_CInt32,int)
150 /*! Complex Float32 */ //%rasterio_functions(DataType.GDT_CFloat32,int)
151 /*! Complex Float64 */ //%rasterio_functions(DataType.GDT_CFloat64,int)
152 
153 %define %ds_rasterio_functions(GDALTYPE,CSTYPE)
154  public CPLErr ReadRaster(int xOff, int yOff, int xSize, int ySize, CSTYPE[] buffer, int buf_xSize, int buf_ySize,
155      int bandCount, int[] bandMap, int pixelSpace, int lineSpace, int bandSpace) {
156       CPLErr retval;
157       GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
158       try {
159           retval = ReadRaster(xOff, yOff, xSize, ySize, handle.AddrOfPinnedObject(), buf_xSize, buf_ySize, GDALTYPE,
160                                bandCount, bandMap, pixelSpace, lineSpace, bandSpace);
161       } finally {
162           handle.Free();
163       }
164       GC.KeepAlive(this);
165       return retval;
166   }
WriteRaster(int xOff,int yOff,int xSize,int ySize,CSTYPE[]buffer,int buf_xSize,int buf_ySize,int bandCount,int[]bandMap,int pixelSpace,int lineSpace,int bandSpace)167   public CPLErr WriteRaster(int xOff, int yOff, int xSize, int ySize, CSTYPE[] buffer, int buf_xSize, int buf_ySize,
168      int bandCount, int[] bandMap, int pixelSpace, int lineSpace, int bandSpace) {
169       CPLErr retval;
170       GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
171       try {
172           retval = WriteRaster(xOff, yOff, xSize, ySize, handle.AddrOfPinnedObject(), buf_xSize, buf_ySize, GDALTYPE,
173                                bandCount, bandMap, pixelSpace, lineSpace, bandSpace);
174       } finally {
175           handle.Free();
176       }
177       GC.KeepAlive(this);
178       return retval;
179   }
ReadRaster(int xOff,int yOff,int xSize,int ySize,CSTYPE[]buffer,int buf_xSize,int buf_ySize,int bandCount,int[]bandMap,int pixelSpace,int lineSpace,int bandSpace,RasterIOExtraArg extraArg)180   public CPLErr ReadRaster(int xOff, int yOff, int xSize, int ySize, CSTYPE[] buffer, int buf_xSize, int buf_ySize,
181      int bandCount, int[] bandMap, int pixelSpace, int lineSpace, int bandSpace, RasterIOExtraArg extraArg) {
182       CPLErr retval;
183       GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
184       try {
185           retval = ReadRaster(xOff, yOff, xSize, ySize, handle.AddrOfPinnedObject(), buf_xSize, buf_ySize, GDALTYPE,
186                                bandCount, bandMap, pixelSpace, lineSpace, bandSpace, extraArg);
187       } finally {
188           handle.Free();
189       }
190       GC.KeepAlive(this);
191       return retval;
192   }
WriteRaster(int xOff,int yOff,int xSize,int ySize,CSTYPE[]buffer,int buf_xSize,int buf_ySize,int bandCount,int[]bandMap,int pixelSpace,int lineSpace,int bandSpace,RasterIOExtraArg extraArg)193   public CPLErr WriteRaster(int xOff, int yOff, int xSize, int ySize, CSTYPE[] buffer, int buf_xSize, int buf_ySize,
194      int bandCount, int[] bandMap, int pixelSpace, int lineSpace, int bandSpace, RasterIOExtraArg extraArg) {
195       CPLErr retval;
196       GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
197       try {
198           retval = WriteRaster(xOff, yOff, xSize, ySize, handle.AddrOfPinnedObject(), buf_xSize, buf_ySize, GDALTYPE,
199                                bandCount, bandMap, pixelSpace, lineSpace, bandSpace, extraArg);
200       } finally {
201           handle.Free();
202       }
203       GC.KeepAlive(this);
204       return retval;
205   }
206 
207 %enddef
208 
209 %typemap(cscode, noblock="1") GDALDatasetShadow {
210 /*! Eight bit unsigned integer */ %ds_rasterio_functions(DataType.GDT_Byte,byte)
211 /*! Sixteen bit signed integer */ %ds_rasterio_functions(DataType.GDT_Int16,short)
212 /*! Thirty two bit signed integer */ %ds_rasterio_functions(DataType.GDT_Int32,int)
213 /*! Thirty two bit floating point */ %ds_rasterio_functions(DataType.GDT_Float32,float)
214 /*! Sixty four bit floating point */ %ds_rasterio_functions(DataType.GDT_Float64,double)
215 
216 public int BuildOverviews( string resampling, int[] overviewlist, $module.GDALProgressFuncDelegate callback, string callback_data) {
217       int retval;
218       if (overviewlist.Length <= 0)
219         throw new ArgumentException("overviewlist size is small (BuildOverviews)");
220 
221       IntPtr ptr = Marshal.AllocHGlobal(overviewlist.Length * Marshal.SizeOf(overviewlist[0]));
222       try {
223           Marshal.Copy(overviewlist, 0, ptr, overviewlist.Length);
224           retval = BuildOverviews(resampling, overviewlist.Length, ptr, callback, callback_data);
225       } finally {
226           Marshal.FreeHGlobal(ptr);
227       }
228       GC.KeepAlive(this);
229       return retval;
230   }
BuildOverviews(string resampling,int[]overviewlist)231 public int BuildOverviews( string resampling, int[] overviewlist) {
232       return BuildOverviews( resampling, overviewlist, null, null);
233   }
234 
GetGCPs()235 public GCP[] GetGCPs() {
236       /*hello*/
237       IntPtr cPtr = __GetGCPs();
238       int length = GetGCPCount();
239       GCP[] ret = null;
240       if (cPtr != IntPtr.Zero && length > 0)
241       {
242           ret = new GCP[length];
243           for (int i=0; i < length; i++)
244               ret[i] = __ReadCArrayItem_GDAL_GCP(cPtr, i);
245       }
246       GC.KeepAlive(this);
247       return ret;
248   }
249 
SetGCPs(GCP[]pGCPs,string pszGCPProjection)250 public CPLErr SetGCPs(GCP[] pGCPs, string pszGCPProjection) {
251      CPLErr ret = 0;
252      if (pGCPs != null && pGCPs.Length > 0)
253      {
254          IntPtr cPtr = __AllocCArray_GDAL_GCP(pGCPs.Length);
255          if (cPtr == IntPtr.Zero)
256             throw new ApplicationException("Error allocating CArray with __AllocCArray_GDAL_GCP");
257 
258          try {
259              for (int i=0; i < pGCPs.Length; i++)
260                 __WriteCArrayItem_GDAL_GCP(cPtr, i, pGCPs[i]);
261 
262              ret = __SetGCPs(pGCPs.Length, cPtr, pszGCPProjection);
263          }
264          finally
265          {
266             __FreeCArray_GDAL_GCP(cPtr);
267          }
268      }
269      GC.KeepAlive(this);
270      return ret;
271   }
272 }
273 
274 /*! Sixteen bit unsigned integer */ //%ds_rasterio_functions(DataType.GDT_UInt16,ushort)
275 /*! Thirty two bit unsigned integer */ //%ds_rasterio_functions(DataType.GDT_UInt32,uint)
276 /*! Complex Int16 */ //%ds_rasterio_functions(DataType.GDT_CInt16,int)
277 /*! Complex Int32 */ //%ds_rasterio_functions(DataType.GDT_CInt32,int)
278 /*! Complex Float32 */ //%ds_rasterio_functions(DataType.GDT_CFloat32,int)
279 /*! Complex Float64 */ //%ds_rasterio_functions(DataType.GDT_CFloat64,int)
280 
281 %pragma(csharp) modulecode=%{
GCPsToGeoTransform(GCP[]pGCPs,double[]argout,int bApproxOK)282   public static int GCPsToGeoTransform(GCP[] pGCPs, double[] argout, int bApproxOK) {
283     int ret = 0;
284     if (pGCPs != null && pGCPs.Length > 0)
285      {
286          IntPtr cPtr = __AllocCArray_GDAL_GCP(pGCPs.Length);
287          if (cPtr == IntPtr.Zero)
288             throw new ApplicationException("Error allocating CArray with __AllocCArray_GDAL_GCP");
289 
290          try {
291              for (int i=0; i < pGCPs.Length; i++)
292                 __WriteCArrayItem_GDAL_GCP(cPtr, i, pGCPs[i]);
293 
294              ret = GCPsToGeoTransform(pGCPs.Length, cPtr, argout, bApproxOK);
295          }
296          finally
297          {
298             __FreeCArray_GDAL_GCP(cPtr);
299          }
300      }
301      return ret;
302    }
303 
FileFromMemBuffer(string utf8_path,byte[]bytes)304  public static void FileFromMemBuffer(string utf8_path, byte[] bytes) {
305      GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
306      try {
307           FileFromMemBuffer(utf8_path, bytes.Length, handle.AddrOfPinnedObject());
308      } finally {
309         handle.Free();
310      }
311   }
312 
313   public static int Warp(Dataset dstDS, Dataset[] poObjects, GDALWarpAppOptions warpAppOptions, $module.GDALProgressFuncDelegate callback, string callback_data) {
314       int retval = 0;
315       if (poObjects.Length <= 0)
316         throw new ArgumentException("poObjects size is small (GDALWarpDestDS)");
317 
318       int intPtrSize = Marshal.SizeOf(typeof(IntPtr));
319       IntPtr nativeArray = Marshal.AllocHGlobal(poObjects.Length * intPtrSize);
320       try {
321           for (int i=0; i < poObjects.Length; i++)
322             Marshal.WriteIntPtr(nativeArray, i * intPtrSize, Dataset.getCPtr(poObjects[i]).Handle);
323 
324           retval  = wrapper_GDALWarpDestDS(dstDS, poObjects.Length, nativeArray, warpAppOptions, callback, callback_data);
325       } finally {
326           Marshal.FreeHGlobal(nativeArray);
327       }
328       return retval;
329    }
330 
331    public static Dataset Warp(string dest, Dataset[] poObjects, GDALWarpAppOptions warpAppOptions, $module.GDALProgressFuncDelegate callback, string callback_data) {
332       Dataset retval = null;
333       if (poObjects.Length <= 0)
334         throw new ArgumentException("poObjects size is small (GDALWarpDestDS)");
335 
336       int intPtrSize = Marshal.SizeOf(typeof(IntPtr));
337       IntPtr nativeArray = Marshal.AllocHGlobal(poObjects.Length * intPtrSize);
338       try {
339           for (int i=0; i < poObjects.Length; i++)
340             Marshal.WriteIntPtr(nativeArray, i * intPtrSize, Dataset.getCPtr(poObjects[i]).Handle);
341 
342           retval  = wrapper_GDALWarpDestName(dest, poObjects.Length, nativeArray, warpAppOptions, callback, callback_data);
343       } finally {
344           Marshal.FreeHGlobal(nativeArray);
345       }
346       return retval;
347    }
348 
349 %}
350