@dsh-gis/dsh-opengis-skill 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/.editorconfig +19 -0
  2. package/CHANGELOG.md +96 -0
  3. package/LICENSE +21 -21
  4. package/README.md +91 -30
  5. package/THIRD_PARTY_NOTICES.md +37 -10
  6. package/assets/gdal/reference/raster-tools.md +538 -538
  7. package/assets/gdal/reference/vector-tools.md +149 -149
  8. package/assets/gdal-api/reference/raster-api.md +424 -424
  9. package/assets/gdal-api/reference/vector-api.md +724 -724
  10. package/assets/geometry-api-net/reference/geometry-types.md +243 -243
  11. package/assets/geopandas/reference/core-api.md +221 -221
  12. package/assets/geotools/reference/raster-rendering.md +108 -108
  13. package/assets/geotools/reference/vector-io.md +149 -149
  14. package/assets/jts/reference/geometry-operations.md +303 -303
  15. package/assets/mapsui/SKILL.md +283 -283
  16. package/assets/opengis-all/reference/data-stages.md +608 -608
  17. package/assets/opengis-all/reference/workflow-cheatsheets.md +324 -324
  18. package/assets/opengis-utils-for-java/reference/common-tasks.md +384 -384
  19. package/assets/opengis-utils-for-net/reference/code-patterns.md +124 -124
  20. package/assets/opengis-utils-for-net/reference/util-classes.md +291 -291
  21. package/assets/pyqgis/reference/processing-rendering.md +292 -292
  22. package/assets/pyqgis/reference/vector-raster-layers.md +318 -318
  23. package/assets/qgis-process/SKILL.md +414 -414
  24. package/assets/qgis-process/reference/algorithm-cheatsheet.md +194 -194
  25. package/assets/shapely/reference/operations-reference.md +199 -199
  26. package/cordis.patch.yml +6 -3
  27. package/lib/catalog.d.ts +50 -0
  28. package/lib/catalog.js +238 -0
  29. package/lib/index.d.ts +47 -6
  30. package/lib/index.js +32 -44
  31. package/lib/markdown-links.d.ts +11 -0
  32. package/lib/markdown-links.js +103 -0
  33. package/lib/skill-file.js +104 -17
  34. package/locale/en.json +1 -1
  35. package/locale/zh.json +1 -1
  36. package/package.json +11 -5
  37. package/upstream.json +64 -2
@@ -1,424 +1,424 @@
1
- # GDAL Raster API Reference
2
-
3
- > 这是 [../SKILL.md](../SKILL.md) 的补充文件,收录 C++/Python/Java/C# 四种语言的栅格读写、格式转换与重投影完整示例。
4
-
5
- ---
6
-
7
- ## 栅格数据读取
8
-
9
- ### C++
10
-
11
- ```cpp
12
- #include "gdal_priv.h"
13
-
14
- GDALAllRegister();
15
-
16
- GDALDataset *ds = (GDALDataset *)GDALOpen("dem.tif", GA_ReadOnly);
17
- if (ds == nullptr) { /* 错误处理 */ }
18
-
19
- // 基本信息
20
- int width = ds->GetRasterXSize();
21
- int height = ds->GetRasterYSize();
22
- int bands = ds->GetRasterCount();
23
-
24
- // 仿射变换参数 [左上X, 像素宽度, 旋转, 左上Y, 旋转, 像素高度(负)]
25
- double gt[6];
26
- ds->GetGeoTransform(gt);
27
-
28
- // 投影信息
29
- const char *proj = ds->GetProjectionRef();
30
-
31
- // 读取第一个波段
32
- GDALRasterBand *band = ds->GetRasterBand(1);
33
- float *buf = new float[width * height];
34
- band->RasterIO(GF_Read, 0, 0, width, height, buf, width, height, GDT_Float32, 0, 0);
35
-
36
- // 获取 NoData 值
37
- int hasNoData;
38
- double nodata = band->GetNoDataValue(&hasNoData);
39
-
40
- delete[] buf;
41
- GDALClose(ds);
42
- ```
43
-
44
- ### Python
45
-
46
- ```python
47
- from osgeo import gdal
48
-
49
- ds = gdal.Open("dem.tif", gdal.GA_ReadOnly)
50
- if ds is None:
51
- raise RuntimeError("无法打开文件")
52
-
53
- # 基本信息
54
- width = ds.RasterXSize
55
- height = ds.RasterYSize
56
- bands = ds.RasterCount
57
-
58
- # 仿射变换参数
59
- gt = ds.GetGeoTransform()
60
-
61
- # 投影信息
62
- proj = ds.GetProjection()
63
-
64
- # 读取第一个波段为 NumPy 数组
65
- band = ds.GetRasterBand(1)
66
- data = band.ReadAsArray() # numpy.ndarray
67
-
68
- # 获取 NoData 值
69
- nodata = band.GetNoDataValue()
70
-
71
- ds = None # 关闭数据集
72
- ```
73
-
74
- ### Java
75
-
76
- ```java
77
- import org.gdal.gdal.gdal;
78
- import org.gdal.gdal.Dataset;
79
- import org.gdal.gdal.Band;
80
- import org.gdal.gdalconst.gdalconstConstants;
81
-
82
- gdal.AllRegister();
83
-
84
- Dataset ds = gdal.Open("dem.tif", gdalconstConstants.GA_ReadOnly);
85
- if (ds == null) { throw new RuntimeException("无法打开文件"); }
86
-
87
- // 基本信息
88
- int width = ds.getRasterXSize();
89
- int height = ds.getRasterYSize();
90
- int bands = ds.getRasterCount();
91
-
92
- // 仿射变换参数
93
- double[] gt = new double[6];
94
- ds.GetGeoTransform(gt);
95
-
96
- // 投影信息
97
- String proj = ds.GetProjection();
98
-
99
- // 读取第一个波段
100
- Band band = ds.GetRasterBand(1);
101
- float[] buf = new float[width * height];
102
- band.ReadRaster(0, 0, width, height, buf);
103
-
104
- // 获取 NoData 值
105
- Double[] nodata = new Double[1];
106
- band.GetNoDataValue(nodata);
107
-
108
- ds.delete();
109
- ```
110
-
111
- ### C#
112
-
113
- ```csharp
114
- using OSGeo.GDAL;
115
-
116
- Gdal.AllRegister();
117
-
118
- Dataset ds = Gdal.Open("dem.tif", Access.GA_ReadOnly);
119
- if (ds == null) { throw new Exception("无法打开文件"); }
120
-
121
- // 基本信息
122
- int width = ds.RasterXSize;
123
- int height = ds.RasterYSize;
124
- int bands = ds.RasterCount;
125
-
126
- // 仿射变换参数
127
- double[] gt = new double[6];
128
- ds.GetGeoTransform(gt);
129
-
130
- // 投影信息
131
- string proj = ds.GetProjection();
132
-
133
- // 读取第一个波段
134
- Band band = ds.GetRasterBand(1);
135
- float[] buf = new float[width * height];
136
- band.ReadRaster(0, 0, width, height, buf, width, height, 0, 0);
137
-
138
- // 获取 NoData 值
139
- double nodata;
140
- int hasNoData;
141
- band.GetNoDataValue(out nodata, out hasNoData);
142
-
143
- ds.Dispose();
144
- ```
145
-
146
- ---
147
-
148
- ## 栅格数据创建与写入
149
-
150
- ### C++
151
-
152
- ```cpp
153
- #include "gdal_priv.h"
154
-
155
- GDALAllRegister();
156
-
157
- GDALDriver *driver = GetGDALDriverManager()->GetDriverByName("GTiff");
158
- GDALDataset *ds = driver->Create("output.tif", 256, 256, 1, GDT_Float32, nullptr);
159
-
160
- // 设置仿射变换
161
- double gt[6] = {116.0, 0.01, 0, 40.0, 0, -0.01};
162
- ds->SetGeoTransform(gt);
163
-
164
- // 设置投影
165
- OGRSpatialReference srs;
166
- srs.SetWellKnownGeogCS("WGS84");
167
- char *wkt = nullptr;
168
- srs.exportToWkt(&wkt);
169
- ds->SetProjection(wkt);
170
- CPLFree(wkt);
171
-
172
- // 写入像素数据
173
- GDALRasterBand *band = ds->GetRasterBand(1);
174
- float *data = new float[256 * 256];
175
- for (int i = 0; i < 256 * 256; i++) data[i] = (float)i;
176
- band->RasterIO(GF_Write, 0, 0, 256, 256, data, 256, 256, GDT_Float32, 0, 0);
177
- band->SetNoDataValue(-9999.0);
178
-
179
- delete[] data;
180
- GDALClose(ds);
181
- ```
182
-
183
- ### Python
184
-
185
- ```python
186
- from osgeo import gdal, osr
187
- import numpy as np
188
-
189
- driver = gdal.GetDriverByName("GTiff")
190
- ds = driver.Create("output.tif", 256, 256, 1, gdal.GDT_Float32)
191
-
192
- # 设置仿射变换
193
- ds.SetGeoTransform([116.0, 0.01, 0, 40.0, 0, -0.01])
194
-
195
- # 设置投影
196
- srs = osr.SpatialReference()
197
- srs.SetWellKnownGeogCS("WGS84")
198
- ds.SetProjection(srs.ExportToWkt())
199
-
200
- # 写入 NumPy 数组
201
- band = ds.GetRasterBand(1)
202
- data = np.arange(256 * 256, dtype=np.float32).reshape(256, 256)
203
- band.WriteArray(data)
204
- band.SetNoDataValue(-9999.0)
205
-
206
- ds.FlushCache()
207
- ds = None
208
- ```
209
-
210
- ### Java
211
-
212
- ```java
213
- import org.gdal.gdal.gdal;
214
- import org.gdal.gdal.Dataset;
215
- import org.gdal.gdal.Driver;
216
- import org.gdal.gdal.Band;
217
- import org.gdal.osr.SpatialReference;
218
-
219
- gdal.AllRegister();
220
-
221
- Driver driver = gdal.GetDriverByName("GTiff");
222
- Dataset ds = driver.Create("output.tif", 256, 256, 1, org.gdal.gdalconst.gdalconstConstants.GDT_Float32);
223
-
224
- // 设置仿射变换
225
- ds.SetGeoTransform(new double[]{116.0, 0.01, 0, 40.0, 0, -0.01});
226
-
227
- // 设置投影
228
- SpatialReference srs = new SpatialReference();
229
- srs.SetWellKnownGeogCS("WGS84");
230
- ds.SetProjection(srs.ExportToWkt());
231
-
232
- // 写入像素数据
233
- Band band = ds.GetRasterBand(1);
234
- float[] data = new float[256 * 256];
235
- for (int i = 0; i < data.length; i++) data[i] = (float) i;
236
- band.WriteRaster(0, 0, 256, 256, data);
237
- band.SetNoDataValue(-9999.0);
238
-
239
- ds.FlushCache();
240
- ds.delete();
241
- ```
242
-
243
- ### C#
244
-
245
- ```csharp
246
- using OSGeo.GDAL;
247
- using OSGeo.OSR;
248
-
249
- Gdal.AllRegister();
250
-
251
- Driver driver = Gdal.GetDriverByName("GTiff");
252
- Dataset ds = driver.Create("output.tif", 256, 256, 1, DataType.GDT_Float32, null);
253
-
254
- // 设置仿射变换
255
- ds.SetGeoTransform(new double[] { 116.0, 0.01, 0, 40.0, 0, -0.01 });
256
-
257
- // 设置投影
258
- SpatialReference srs = new SpatialReference("");
259
- srs.SetWellKnownGeogCS("WGS84");
260
- string wkt;
261
- srs.ExportToWkt(out wkt);
262
- ds.SetProjection(wkt);
263
-
264
- // 写入像素数据
265
- Band band = ds.GetRasterBand(1);
266
- float[] data = new float[256 * 256];
267
- for (int i = 0; i < data.Length; i++) data[i] = (float)i;
268
- band.WriteRaster(0, 0, 256, 256, data, 256, 256, 0, 0);
269
- band.SetNoDataValue(-9999.0);
270
-
271
- ds.FlushCache();
272
- ds.Dispose();
273
- ```
274
-
275
- ---
276
-
277
- ## 栅格格式转换与重投影
278
-
279
- ### C++
280
-
281
- ```cpp
282
- #include "gdal_priv.h"
283
- #include "gdalwarper.h"
284
-
285
- GDALAllRegister();
286
-
287
- // 格式转换(GeoTIFF → PNG)
288
- GDALDataset *src = (GDALDataset *)GDALOpen("input.tif", GA_ReadOnly);
289
- GDALDriver *pngDriver = GetGDALDriverManager()->GetDriverByName("PNG");
290
- GDALDataset *dst = pngDriver->CreateCopy("output.png", src, FALSE, nullptr, nullptr, nullptr);
291
- GDALClose(dst);
292
- GDALClose(src);
293
-
294
- // 重投影(Warp)
295
- GDALDataset *srcDs = (GDALDataset *)GDALOpen("input.tif", GA_ReadOnly);
296
- const char *dstWKT = nullptr;
297
- OGRSpatialReference dstSRS;
298
- dstSRS.importFromEPSG(3857);
299
- dstSRS.exportToWkt(const_cast<char **>(&dstWKT));
300
-
301
- GDALWarpOptions *warpOpts = GDALCreateWarpOptions();
302
- warpOpts->hSrcDS = srcDs;
303
- warpOpts->nBandCount = 1;
304
- warpOpts->panSrcBands = (int *)CPLMalloc(sizeof(int));
305
- warpOpts->panSrcBands[0] = 1;
306
- warpOpts->panDstBands = (int *)CPLMalloc(sizeof(int));
307
- warpOpts->panDstBands[0] = 1;
308
-
309
- GDALDataset *dstDs = (GDALDataset *)GDALAutoCreateWarpedVRT(
310
- srcDs, nullptr, dstWKT, GRA_Bilinear, 0.0, nullptr);
311
- GDALDriver *tiffDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
312
- GDALDataset *outDs = tiffDriver->CreateCopy("reprojected.tif", dstDs, FALSE, nullptr, nullptr, nullptr);
313
-
314
- GDALClose(outDs);
315
- GDALClose(dstDs);
316
- GDALClose(srcDs);
317
- ```
318
-
319
- ### Python
320
-
321
- ```python
322
- from osgeo import gdal
323
-
324
- # 格式转换(GeoTIFF → PNG)
325
- src = gdal.Open("input.tif")
326
- gdal.GetDriverByName("PNG").CreateCopy("output.png", src)
327
- src = None
328
-
329
- # 重投影(Warp)
330
- gdal.Warp(
331
- "reprojected.tif",
332
- "input.tif",
333
- dstSRS="EPSG:3857",
334
- resampleAlg="bilinear"
335
- )
336
-
337
- # 带更多参数的 Warp
338
- gdal.Warp(
339
- "clipped.tif",
340
- "input.tif",
341
- dstSRS="EPSG:3857",
342
- outputBounds=[12000000, 4000000, 13000000, 5000000],
343
- xRes=100, yRes=100,
344
- resampleAlg="cubic",
345
- creationOptions=["COMPRESS=LZW", "TILED=YES"]
346
- )
347
- ```
348
-
349
- ### Java
350
-
351
- ```java
352
- import org.gdal.gdal.gdal;
353
- import org.gdal.gdal.Dataset;
354
- import org.gdal.gdal.Driver;
355
- import org.gdal.gdal.WarpOptions;
356
- import java.util.Vector;
357
-
358
- gdal.AllRegister();
359
-
360
- // 格式转换(GeoTIFF → PNG)
361
- Dataset src = gdal.Open("input.tif");
362
- Driver pngDriver = gdal.GetDriverByName("PNG");
363
- Dataset dst = pngDriver.CreateCopy("output.png", src);
364
- dst.delete();
365
- src.delete();
366
-
367
- // 重投影(Warp)
368
- Dataset srcDs = gdal.Open("input.tif");
369
- Vector<String> options = new Vector<>();
370
- options.add("-t_srs");
371
- options.add("EPSG:3857");
372
- options.add("-r");
373
- options.add("bilinear");
374
-
375
- WarpOptions warpOpts = new WarpOptions(options);
376
- Dataset[] srcArray = new Dataset[]{srcDs};
377
- Dataset dstDs = gdal.Warp("reprojected.tif", srcArray, warpOpts);
378
- dstDs.delete();
379
- srcDs.delete();
380
- ```
381
-
382
- ### C#
383
-
384
- ```csharp
385
- using OSGeo.GDAL;
386
-
387
- Gdal.AllRegister();
388
-
389
- // 格式转换(GeoTIFF → PNG)
390
- Dataset src = Gdal.Open("input.tif", Access.GA_ReadOnly);
391
- Driver pngDriver = Gdal.GetDriverByName("PNG");
392
- Dataset dst = pngDriver.CreateCopy("output.png", src, 0, null, null, null);
393
- dst.Dispose();
394
- src.Dispose();
395
-
396
- // 重投影(Warp)
397
- Dataset srcDs = Gdal.Open("input.tif", Access.GA_ReadOnly);
398
- string[] warpArgs = new string[] {
399
- "-t_srs", "EPSG:3857",
400
- "-r", "bilinear"
401
- };
402
- GDALWarpAppOptions warpOpts = new GDALWarpAppOptions(warpArgs);
403
- Dataset dstDs = Gdal.Warp("reprojected.tif", new Dataset[] { srcDs }, warpOpts, null, null);
404
- dstDs.Dispose();
405
- srcDs.Dispose();
406
- ```
407
-
408
- ---
409
-
410
- ## 常用栅格数据格式与创建选项
411
-
412
- | 驱动名 | 扩展名 | 说明 | 常用创建选项 |
413
- |---|---|---|---|
414
- | `GTiff` | `.tif` | GeoTIFF,最常用的栅格格式 | `COMPRESS=LZW/DEFLATE/ZSTD`、`TILED=YES`、`BLOCKXSIZE=256` |
415
- | `COG` | `.tif` | Cloud Optimized GeoTIFF | `COMPRESS=DEFLATE`、`OVERVIEW_RESAMPLING=CUBIC` |
416
- | `PNG` | `.png` | PNG 图片 | `ZLEVEL=6` |
417
- | `JPEG` | `.jpg` | JPEG 图片 | `QUALITY=85` |
418
- | `GPKG` | `.gpkg` | GeoPackage 栅格 | `TILE_FORMAT=PNG/JPEG/WEBP` |
419
- | `netCDF` | `.nc` | NetCDF 科学数据格式 | `FORMAT=NC4`、`COMPRESS=DEFLATE` |
420
- | `HFA` | `.img` | Erdas Imagine | — |
421
- | `VRT` | `.vrt` | 虚拟数据集(XML 引用) | — |
422
-
423
- ---
424
-
1
+ # GDAL Raster API Reference
2
+
3
+ > 这是 [../SKILL.md](../SKILL.md) 的补充文件,收录 C++/Python/Java/C# 四种语言的栅格读写、格式转换与重投影完整示例。
4
+
5
+ ---
6
+
7
+ ## 栅格数据读取
8
+
9
+ ### C++
10
+
11
+ ```cpp
12
+ #include "gdal_priv.h"
13
+
14
+ GDALAllRegister();
15
+
16
+ GDALDataset *ds = (GDALDataset *)GDALOpen("dem.tif", GA_ReadOnly);
17
+ if (ds == nullptr) { /* 错误处理 */ }
18
+
19
+ // 基本信息
20
+ int width = ds->GetRasterXSize();
21
+ int height = ds->GetRasterYSize();
22
+ int bands = ds->GetRasterCount();
23
+
24
+ // 仿射变换参数 [左上X, 像素宽度, 旋转, 左上Y, 旋转, 像素高度(负)]
25
+ double gt[6];
26
+ ds->GetGeoTransform(gt);
27
+
28
+ // 投影信息
29
+ const char *proj = ds->GetProjectionRef();
30
+
31
+ // 读取第一个波段
32
+ GDALRasterBand *band = ds->GetRasterBand(1);
33
+ float *buf = new float[width * height];
34
+ band->RasterIO(GF_Read, 0, 0, width, height, buf, width, height, GDT_Float32, 0, 0);
35
+
36
+ // 获取 NoData 值
37
+ int hasNoData;
38
+ double nodata = band->GetNoDataValue(&hasNoData);
39
+
40
+ delete[] buf;
41
+ GDALClose(ds);
42
+ ```
43
+
44
+ ### Python
45
+
46
+ ```python
47
+ from osgeo import gdal
48
+
49
+ ds = gdal.Open("dem.tif", gdal.GA_ReadOnly)
50
+ if ds is None:
51
+ raise RuntimeError("无法打开文件")
52
+
53
+ # 基本信息
54
+ width = ds.RasterXSize
55
+ height = ds.RasterYSize
56
+ bands = ds.RasterCount
57
+
58
+ # 仿射变换参数
59
+ gt = ds.GetGeoTransform()
60
+
61
+ # 投影信息
62
+ proj = ds.GetProjection()
63
+
64
+ # 读取第一个波段为 NumPy 数组
65
+ band = ds.GetRasterBand(1)
66
+ data = band.ReadAsArray() # numpy.ndarray
67
+
68
+ # 获取 NoData 值
69
+ nodata = band.GetNoDataValue()
70
+
71
+ ds = None # 关闭数据集
72
+ ```
73
+
74
+ ### Java
75
+
76
+ ```java
77
+ import org.gdal.gdal.gdal;
78
+ import org.gdal.gdal.Dataset;
79
+ import org.gdal.gdal.Band;
80
+ import org.gdal.gdalconst.gdalconstConstants;
81
+
82
+ gdal.AllRegister();
83
+
84
+ Dataset ds = gdal.Open("dem.tif", gdalconstConstants.GA_ReadOnly);
85
+ if (ds == null) { throw new RuntimeException("无法打开文件"); }
86
+
87
+ // 基本信息
88
+ int width = ds.getRasterXSize();
89
+ int height = ds.getRasterYSize();
90
+ int bands = ds.getRasterCount();
91
+
92
+ // 仿射变换参数
93
+ double[] gt = new double[6];
94
+ ds.GetGeoTransform(gt);
95
+
96
+ // 投影信息
97
+ String proj = ds.GetProjection();
98
+
99
+ // 读取第一个波段
100
+ Band band = ds.GetRasterBand(1);
101
+ float[] buf = new float[width * height];
102
+ band.ReadRaster(0, 0, width, height, buf);
103
+
104
+ // 获取 NoData 值
105
+ Double[] nodata = new Double[1];
106
+ band.GetNoDataValue(nodata);
107
+
108
+ ds.delete();
109
+ ```
110
+
111
+ ### C#
112
+
113
+ ```csharp
114
+ using OSGeo.GDAL;
115
+
116
+ Gdal.AllRegister();
117
+
118
+ Dataset ds = Gdal.Open("dem.tif", Access.GA_ReadOnly);
119
+ if (ds == null) { throw new Exception("无法打开文件"); }
120
+
121
+ // 基本信息
122
+ int width = ds.RasterXSize;
123
+ int height = ds.RasterYSize;
124
+ int bands = ds.RasterCount;
125
+
126
+ // 仿射变换参数
127
+ double[] gt = new double[6];
128
+ ds.GetGeoTransform(gt);
129
+
130
+ // 投影信息
131
+ string proj = ds.GetProjection();
132
+
133
+ // 读取第一个波段
134
+ Band band = ds.GetRasterBand(1);
135
+ float[] buf = new float[width * height];
136
+ band.ReadRaster(0, 0, width, height, buf, width, height, 0, 0);
137
+
138
+ // 获取 NoData 值
139
+ double nodata;
140
+ int hasNoData;
141
+ band.GetNoDataValue(out nodata, out hasNoData);
142
+
143
+ ds.Dispose();
144
+ ```
145
+
146
+ ---
147
+
148
+ ## 栅格数据创建与写入
149
+
150
+ ### C++
151
+
152
+ ```cpp
153
+ #include "gdal_priv.h"
154
+
155
+ GDALAllRegister();
156
+
157
+ GDALDriver *driver = GetGDALDriverManager()->GetDriverByName("GTiff");
158
+ GDALDataset *ds = driver->Create("output.tif", 256, 256, 1, GDT_Float32, nullptr);
159
+
160
+ // 设置仿射变换
161
+ double gt[6] = {116.0, 0.01, 0, 40.0, 0, -0.01};
162
+ ds->SetGeoTransform(gt);
163
+
164
+ // 设置投影
165
+ OGRSpatialReference srs;
166
+ srs.SetWellKnownGeogCS("WGS84");
167
+ char *wkt = nullptr;
168
+ srs.exportToWkt(&wkt);
169
+ ds->SetProjection(wkt);
170
+ CPLFree(wkt);
171
+
172
+ // 写入像素数据
173
+ GDALRasterBand *band = ds->GetRasterBand(1);
174
+ float *data = new float[256 * 256];
175
+ for (int i = 0; i < 256 * 256; i++) data[i] = (float)i;
176
+ band->RasterIO(GF_Write, 0, 0, 256, 256, data, 256, 256, GDT_Float32, 0, 0);
177
+ band->SetNoDataValue(-9999.0);
178
+
179
+ delete[] data;
180
+ GDALClose(ds);
181
+ ```
182
+
183
+ ### Python
184
+
185
+ ```python
186
+ from osgeo import gdal, osr
187
+ import numpy as np
188
+
189
+ driver = gdal.GetDriverByName("GTiff")
190
+ ds = driver.Create("output.tif", 256, 256, 1, gdal.GDT_Float32)
191
+
192
+ # 设置仿射变换
193
+ ds.SetGeoTransform([116.0, 0.01, 0, 40.0, 0, -0.01])
194
+
195
+ # 设置投影
196
+ srs = osr.SpatialReference()
197
+ srs.SetWellKnownGeogCS("WGS84")
198
+ ds.SetProjection(srs.ExportToWkt())
199
+
200
+ # 写入 NumPy 数组
201
+ band = ds.GetRasterBand(1)
202
+ data = np.arange(256 * 256, dtype=np.float32).reshape(256, 256)
203
+ band.WriteArray(data)
204
+ band.SetNoDataValue(-9999.0)
205
+
206
+ ds.FlushCache()
207
+ ds = None
208
+ ```
209
+
210
+ ### Java
211
+
212
+ ```java
213
+ import org.gdal.gdal.gdal;
214
+ import org.gdal.gdal.Dataset;
215
+ import org.gdal.gdal.Driver;
216
+ import org.gdal.gdal.Band;
217
+ import org.gdal.osr.SpatialReference;
218
+
219
+ gdal.AllRegister();
220
+
221
+ Driver driver = gdal.GetDriverByName("GTiff");
222
+ Dataset ds = driver.Create("output.tif", 256, 256, 1, org.gdal.gdalconst.gdalconstConstants.GDT_Float32);
223
+
224
+ // 设置仿射变换
225
+ ds.SetGeoTransform(new double[]{116.0, 0.01, 0, 40.0, 0, -0.01});
226
+
227
+ // 设置投影
228
+ SpatialReference srs = new SpatialReference();
229
+ srs.SetWellKnownGeogCS("WGS84");
230
+ ds.SetProjection(srs.ExportToWkt());
231
+
232
+ // 写入像素数据
233
+ Band band = ds.GetRasterBand(1);
234
+ float[] data = new float[256 * 256];
235
+ for (int i = 0; i < data.length; i++) data[i] = (float) i;
236
+ band.WriteRaster(0, 0, 256, 256, data);
237
+ band.SetNoDataValue(-9999.0);
238
+
239
+ ds.FlushCache();
240
+ ds.delete();
241
+ ```
242
+
243
+ ### C#
244
+
245
+ ```csharp
246
+ using OSGeo.GDAL;
247
+ using OSGeo.OSR;
248
+
249
+ Gdal.AllRegister();
250
+
251
+ Driver driver = Gdal.GetDriverByName("GTiff");
252
+ Dataset ds = driver.Create("output.tif", 256, 256, 1, DataType.GDT_Float32, null);
253
+
254
+ // 设置仿射变换
255
+ ds.SetGeoTransform(new double[] { 116.0, 0.01, 0, 40.0, 0, -0.01 });
256
+
257
+ // 设置投影
258
+ SpatialReference srs = new SpatialReference("");
259
+ srs.SetWellKnownGeogCS("WGS84");
260
+ string wkt;
261
+ srs.ExportToWkt(out wkt);
262
+ ds.SetProjection(wkt);
263
+
264
+ // 写入像素数据
265
+ Band band = ds.GetRasterBand(1);
266
+ float[] data = new float[256 * 256];
267
+ for (int i = 0; i < data.Length; i++) data[i] = (float)i;
268
+ band.WriteRaster(0, 0, 256, 256, data, 256, 256, 0, 0);
269
+ band.SetNoDataValue(-9999.0);
270
+
271
+ ds.FlushCache();
272
+ ds.Dispose();
273
+ ```
274
+
275
+ ---
276
+
277
+ ## 栅格格式转换与重投影
278
+
279
+ ### C++
280
+
281
+ ```cpp
282
+ #include "gdal_priv.h"
283
+ #include "gdalwarper.h"
284
+
285
+ GDALAllRegister();
286
+
287
+ // 格式转换(GeoTIFF → PNG)
288
+ GDALDataset *src = (GDALDataset *)GDALOpen("input.tif", GA_ReadOnly);
289
+ GDALDriver *pngDriver = GetGDALDriverManager()->GetDriverByName("PNG");
290
+ GDALDataset *dst = pngDriver->CreateCopy("output.png", src, FALSE, nullptr, nullptr, nullptr);
291
+ GDALClose(dst);
292
+ GDALClose(src);
293
+
294
+ // 重投影(Warp)
295
+ GDALDataset *srcDs = (GDALDataset *)GDALOpen("input.tif", GA_ReadOnly);
296
+ const char *dstWKT = nullptr;
297
+ OGRSpatialReference dstSRS;
298
+ dstSRS.importFromEPSG(3857);
299
+ dstSRS.exportToWkt(const_cast<char **>(&dstWKT));
300
+
301
+ GDALWarpOptions *warpOpts = GDALCreateWarpOptions();
302
+ warpOpts->hSrcDS = srcDs;
303
+ warpOpts->nBandCount = 1;
304
+ warpOpts->panSrcBands = (int *)CPLMalloc(sizeof(int));
305
+ warpOpts->panSrcBands[0] = 1;
306
+ warpOpts->panDstBands = (int *)CPLMalloc(sizeof(int));
307
+ warpOpts->panDstBands[0] = 1;
308
+
309
+ GDALDataset *dstDs = (GDALDataset *)GDALAutoCreateWarpedVRT(
310
+ srcDs, nullptr, dstWKT, GRA_Bilinear, 0.0, nullptr);
311
+ GDALDriver *tiffDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
312
+ GDALDataset *outDs = tiffDriver->CreateCopy("reprojected.tif", dstDs, FALSE, nullptr, nullptr, nullptr);
313
+
314
+ GDALClose(outDs);
315
+ GDALClose(dstDs);
316
+ GDALClose(srcDs);
317
+ ```
318
+
319
+ ### Python
320
+
321
+ ```python
322
+ from osgeo import gdal
323
+
324
+ # 格式转换(GeoTIFF → PNG)
325
+ src = gdal.Open("input.tif")
326
+ gdal.GetDriverByName("PNG").CreateCopy("output.png", src)
327
+ src = None
328
+
329
+ # 重投影(Warp)
330
+ gdal.Warp(
331
+ "reprojected.tif",
332
+ "input.tif",
333
+ dstSRS="EPSG:3857",
334
+ resampleAlg="bilinear"
335
+ )
336
+
337
+ # 带更多参数的 Warp
338
+ gdal.Warp(
339
+ "clipped.tif",
340
+ "input.tif",
341
+ dstSRS="EPSG:3857",
342
+ outputBounds=[12000000, 4000000, 13000000, 5000000],
343
+ xRes=100, yRes=100,
344
+ resampleAlg="cubic",
345
+ creationOptions=["COMPRESS=LZW", "TILED=YES"]
346
+ )
347
+ ```
348
+
349
+ ### Java
350
+
351
+ ```java
352
+ import org.gdal.gdal.gdal;
353
+ import org.gdal.gdal.Dataset;
354
+ import org.gdal.gdal.Driver;
355
+ import org.gdal.gdal.WarpOptions;
356
+ import java.util.Vector;
357
+
358
+ gdal.AllRegister();
359
+
360
+ // 格式转换(GeoTIFF → PNG)
361
+ Dataset src = gdal.Open("input.tif");
362
+ Driver pngDriver = gdal.GetDriverByName("PNG");
363
+ Dataset dst = pngDriver.CreateCopy("output.png", src);
364
+ dst.delete();
365
+ src.delete();
366
+
367
+ // 重投影(Warp)
368
+ Dataset srcDs = gdal.Open("input.tif");
369
+ Vector<String> options = new Vector<>();
370
+ options.add("-t_srs");
371
+ options.add("EPSG:3857");
372
+ options.add("-r");
373
+ options.add("bilinear");
374
+
375
+ WarpOptions warpOpts = new WarpOptions(options);
376
+ Dataset[] srcArray = new Dataset[]{srcDs};
377
+ Dataset dstDs = gdal.Warp("reprojected.tif", srcArray, warpOpts);
378
+ dstDs.delete();
379
+ srcDs.delete();
380
+ ```
381
+
382
+ ### C#
383
+
384
+ ```csharp
385
+ using OSGeo.GDAL;
386
+
387
+ Gdal.AllRegister();
388
+
389
+ // 格式转换(GeoTIFF → PNG)
390
+ Dataset src = Gdal.Open("input.tif", Access.GA_ReadOnly);
391
+ Driver pngDriver = Gdal.GetDriverByName("PNG");
392
+ Dataset dst = pngDriver.CreateCopy("output.png", src, 0, null, null, null);
393
+ dst.Dispose();
394
+ src.Dispose();
395
+
396
+ // 重投影(Warp)
397
+ Dataset srcDs = Gdal.Open("input.tif", Access.GA_ReadOnly);
398
+ string[] warpArgs = new string[] {
399
+ "-t_srs", "EPSG:3857",
400
+ "-r", "bilinear"
401
+ };
402
+ GDALWarpAppOptions warpOpts = new GDALWarpAppOptions(warpArgs);
403
+ Dataset dstDs = Gdal.Warp("reprojected.tif", new Dataset[] { srcDs }, warpOpts, null, null);
404
+ dstDs.Dispose();
405
+ srcDs.Dispose();
406
+ ```
407
+
408
+ ---
409
+
410
+ ## 常用栅格数据格式与创建选项
411
+
412
+ | 驱动名 | 扩展名 | 说明 | 常用创建选项 |
413
+ |---|---|---|---|
414
+ | `GTiff` | `.tif` | GeoTIFF,最常用的栅格格式 | `COMPRESS=LZW/DEFLATE/ZSTD`、`TILED=YES`、`BLOCKXSIZE=256` |
415
+ | `COG` | `.tif` | Cloud Optimized GeoTIFF | `COMPRESS=DEFLATE`、`OVERVIEW_RESAMPLING=CUBIC` |
416
+ | `PNG` | `.png` | PNG 图片 | `ZLEVEL=6` |
417
+ | `JPEG` | `.jpg` | JPEG 图片 | `QUALITY=85` |
418
+ | `GPKG` | `.gpkg` | GeoPackage 栅格 | `TILE_FORMAT=PNG/JPEG/WEBP` |
419
+ | `netCDF` | `.nc` | NetCDF 科学数据格式 | `FORMAT=NC4`、`COMPRESS=DEFLATE` |
420
+ | `HFA` | `.img` | Erdas Imagine | — |
421
+ | `VRT` | `.vrt` | 虚拟数据集(XML 引用) | — |
422
+
423
+ ---
424
+