Skip to content

Commit b601d22

Browse files
authored
Doc: Move 2 more tutorials to use tabs (OSGeo#12167)
1 parent 3243cab commit b601d22

File tree

4 files changed

+1028
-1157
lines changed

4 files changed

+1028
-1157
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
from osgeo import gdal
3+
from osgeo import ogr
4+
5+
gdal.UseExceptions()
6+
7+
ds = gdal.OpenEx("point", gdal.OF_VECTOR)
8+
9+
lyr = ds.GetLayerByName("point")
10+
11+
lyr.ResetReading()
12+
13+
for feat in lyr:
14+
15+
feat_defn = lyr.GetLayerDefn()
16+
for i in range(feat_defn.GetFieldCount()):
17+
field_defn = feat_defn.GetFieldDefn(i)
18+
19+
# Tests below can be simplified with just :
20+
# print feat.GetField(i)
21+
if (
22+
field_defn.GetType() == ogr.OFTInteger
23+
or field_defn.GetType() == ogr.OFTInteger64
24+
):
25+
print("%d" % feat.GetFieldAsInteger64(i))
26+
elif field_defn.GetType() == ogr.OFTReal:
27+
print("%.3f" % feat.GetFieldAsDouble(i))
28+
elif field_defn.GetType() == ogr.OFTString:
29+
print("%s" % feat.GetFieldAsString(i))
30+
else:
31+
print("%s" % feat.GetFieldAsString(i))
32+
33+
geom = feat.GetGeometryRef()
34+
if geom is not None and geom.GetGeometryType() == ogr.wkbPoint:
35+
print("%.3f, %.3f" % (geom.GetX(), geom.GetY()))
36+
else:
37+
print("no point geometry")
38+
39+
ds.Close()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
3+
from osgeo import gdal, ogr
4+
5+
gdal.UseExceptions()
6+
driverName = "ESRI Shapefile"
7+
drv = gdal.GetDriverByName(driverName)
8+
if drv is None:
9+
print("%s driver not available." % driverName)
10+
sys.exit(1)
11+
12+
ds = drv.Create("point_out.shp", 0, 0, 0, gdal.GDT_Unknown)
13+
14+
lyr = ds.CreateLayer("point_out", None, ogr.wkbPoint)
15+
16+
field_defn = ogr.FieldDefn("Name", ogr.OFTString)
17+
field_defn.SetWidth(32)
18+
19+
lyr.CreateField(field_defn)
20+
21+
# Expected format of user input: x y name
22+
linestring = input()
23+
linelist = linestring.split()
24+
25+
while len(linelist) == 3:
26+
x = float(linelist[0])
27+
y = float(linelist[1])
28+
name = linelist[2]
29+
30+
feat = ogr.Feature(lyr.GetLayerDefn())
31+
feat.SetField("Name", name)
32+
33+
pt = ogr.Geometry(ogr.wkbPoint)
34+
pt.SetPoint_2D(0, x, y)
35+
36+
feat.SetGeometry(pt)
37+
38+
lyr.CreateFeature(feat)
39+
40+
linestring = input()
41+
linelist = linestring.split()
42+
43+
ds.Close()

doc/source/tutorials/multidimensional_api_tut.rst

Lines changed: 126 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -7,146 +7,132 @@ Multidimensional raster API tutorial
77
Read the content of an array
88
----------------------------
99

10-
In C++
11-
++++++
12-
13-
.. code-block:: c++
14-
15-
#include "gdal_priv.h"
16-
int main()
17-
{
18-
GDALAllRegister();
19-
auto poDataset = std::unique_ptr<GDALDataset>(
20-
GDALDataset::Open( "in.nc", GDAL_OF_MULTIDIM_RASTER ));
21-
if( !poDataset )
22-
{
23-
exit(1);
24-
}
25-
auto poRootGroup = poDataset->GetRootGroup();
26-
if( !poRootGroup )
27-
{
28-
exit(1);
29-
}
30-
auto poVar = poRootGroup->OpenMDArray("temperature");
31-
if( !poVar )
32-
{
33-
exit(1);
34-
}
35-
size_t nValues = 1;
36-
std::vector<size_t> anCount;
37-
for( const auto poDim: poVar->GetDimensions() )
38-
{
39-
anCount.push_back(static_cast<size_t>(poDim->GetSize()));
40-
nValues *= anCount.back();
41-
}
42-
std::vector<double> values(nValues);
43-
poVar->Read(std::vector<GUInt64>{0,0,0}.data(),
44-
anCount.data(),
45-
nullptr, /* step: defaults to 1,1,1 */
46-
nullptr, /* stride: default to row-major convention */
47-
GDALExtendedDataType::Create(GDT_Float64),
48-
&values[0]);
49-
return 0;
50-
}
51-
52-
In C
53-
++++
54-
55-
.. code-block:: c
56-
57-
#include "gdal.h"
58-
#include "cpl_conv.h"
59-
int main()
60-
{
61-
GDALDatasetH hDS;
62-
GDALGroupH hGroup;
63-
GDALMDArrayH hVar;
64-
size_t nDimCount;
65-
GDALDimensionH* dims;
66-
size_t nValues;
67-
size_t i;
68-
size_t* panCount;
69-
GUInt64* panOffset;
70-
double* padfValues;
71-
GDALExtendedDataTypeH hDT;
72-
73-
GDALAllRegister();
74-
hDS = GDALOpenEx( "in.nc", GDAL_OF_MULTIDIM_RASTER, NULL, NULL, NULL);
75-
if( !hDS )
76-
{
77-
exit(1);
78-
}
79-
hGroup = GDALDatasetGetRootGroup(hDS);
80-
GDALReleaseDataset(hDS);
81-
if( !hGroup )
82-
{
83-
exit(1);
84-
}
85-
hVar = GDALGroupOpenMDArray(hGroup, "temperature", NULL);
86-
GDALGroupRelease(hGroup);
87-
if( !hVar )
88-
{
89-
exit(1);
90-
}
91-
92-
dims = GDALMDArrayGetDimensions(hVar, &nDimCount);
93-
panCount = (size_t*)CPLMalloc(nDimCount * sizeof(size_t));
94-
nValues = 1;
95-
for( i = 0; i < nDimCount; i++ )
96-
{
97-
panCount[i] = GDALDimensionGetSize(dims[i]);
98-
nValues *= panCount[i];
99-
}
100-
GDALReleaseDimensions(dims, nDimCount);
101-
panOffset = (GUInt64*)CPLCalloc(nDimCount, sizeof(GUInt64));
102-
103-
padfValues = (double*)VSIMalloc2(nValues, sizeof(double));
104-
if( !padfValues )
105-
{
106-
GDALMDArrayRelease(hVar);
107-
CPLFree(panOffset);
108-
CPLFree(panCount);
109-
exit(1);
110-
}
111-
hDT = GDALExtendedDataTypeCreate(GDT_Float64);
112-
GDALMDArrayRead(hVar,
113-
panOffset,
114-
panCount,
115-
NULL, /* step: defaults to 1,1,1 */
116-
NULL, /* stride: default to row-major convention */
117-
hDT,
118-
padfValues,
119-
NULL, /* array start. Omitted */
120-
0 /* array size in bytes. Omitted */);
121-
GDALExtendedDataTypeRelease(hDT);
122-
GDALMDArrayRelease(hVar);
123-
CPLFree(panOffset);
124-
CPLFree(panCount);
125-
VSIFree(padfValues);
126-
127-
return 0;
128-
}
129-
130-
In Python
131-
+++++++++
132-
133-
.. code-block:: python
134-
135-
from osgeo import gdal
136-
ds = gdal.OpenEx("in.nc", gdal.OF_MULTIDIM_RASTER)
137-
rootGroup = ds.GetRootGroup()
138-
var = rootGroup.OpenMDArray("temperature")
139-
data = var.Read(buffer_datatype = gdal.ExtendedDataType.Create(gdal.GDT_Float64))
140-
141-
If NumPy is available:
142-
143-
.. code-block:: python
144-
145-
from osgeo import gdal
146-
ds = gdal.OpenEx("in.nc", gdal.OF_MULTIDIM_RASTER)
147-
rootGroup = ds.GetRootGroup()
148-
var = rootGroup.OpenMDArray("temperature")
149-
data = var.ReadAsArray(buffer_datatype = gdal.ExtendedDataType.Create(gdal.GDT_Float64))
10+
.. tabs::
11+
12+
.. code-tab:: c++
13+
14+
#include "gdal_priv.h"
15+
int main()
16+
{
17+
GDALAllRegister();
18+
auto poDataset = std::unique_ptr<GDALDataset>(
19+
GDALDataset::Open( "in.nc", GDAL_OF_MULTIDIM_RASTER ));
20+
if( !poDataset )
21+
{
22+
exit(1);
23+
}
24+
auto poRootGroup = poDataset->GetRootGroup();
25+
if( !poRootGroup )
26+
{
27+
exit(1);
28+
}
29+
auto poVar = poRootGroup->OpenMDArray("temperature");
30+
if( !poVar )
31+
{
32+
exit(1);
33+
}
34+
size_t nValues = 1;
35+
std::vector<size_t> anCount;
36+
for( const auto poDim: poVar->GetDimensions() )
37+
{
38+
anCount.push_back(static_cast<size_t>(poDim->GetSize()));
39+
nValues *= anCount.back();
40+
}
41+
std::vector<double> values(nValues);
42+
poVar->Read(std::vector<GUInt64>{0,0,0}.data(),
43+
anCount.data(),
44+
nullptr, /* step: defaults to 1,1,1 */
45+
nullptr, /* stride: default to row-major convention */
46+
GDALExtendedDataType::Create(GDT_Float64),
47+
&values[0]);
48+
return 0;
49+
}
50+
51+
.. code-tab:: c
52+
53+
#include "gdal.h"
54+
#include "cpl_conv.h"
55+
int main()
56+
{
57+
GDALDatasetH hDS;
58+
GDALGroupH hGroup;
59+
GDALMDArrayH hVar;
60+
size_t nDimCount;
61+
GDALDimensionH* dims;
62+
size_t nValues;
63+
size_t i;
64+
size_t* panCount;
65+
GUInt64* panOffset;
66+
double* padfValues;
67+
GDALExtendedDataTypeH hDT;
68+
69+
GDALAllRegister();
70+
hDS = GDALOpenEx( "in.nc", GDAL_OF_MULTIDIM_RASTER, NULL, NULL, NULL);
71+
if( !hDS )
72+
{
73+
exit(1);
74+
}
75+
hGroup = GDALDatasetGetRootGroup(hDS);
76+
GDALReleaseDataset(hDS);
77+
if( !hGroup )
78+
{
79+
exit(1);
80+
}
81+
hVar = GDALGroupOpenMDArray(hGroup, "temperature", NULL);
82+
GDALGroupRelease(hGroup);
83+
if( !hVar )
84+
{
85+
exit(1);
86+
}
87+
88+
dims = GDALMDArrayGetDimensions(hVar, &nDimCount);
89+
panCount = (size_t*)CPLMalloc(nDimCount * sizeof(size_t));
90+
nValues = 1;
91+
for( i = 0; i < nDimCount; i++ )
92+
{
93+
panCount[i] = GDALDimensionGetSize(dims[i]);
94+
nValues *= panCount[i];
95+
}
96+
GDALReleaseDimensions(dims, nDimCount);
97+
panOffset = (GUInt64*)CPLCalloc(nDimCount, sizeof(GUInt64));
98+
99+
padfValues = (double*)VSIMalloc2(nValues, sizeof(double));
100+
if( !padfValues )
101+
{
102+
GDALMDArrayRelease(hVar);
103+
CPLFree(panOffset);
104+
CPLFree(panCount);
105+
exit(1);
106+
}
107+
hDT = GDALExtendedDataTypeCreate(GDT_Float64);
108+
GDALMDArrayRead(hVar,
109+
panOffset,
110+
panCount,
111+
NULL, /* step: defaults to 1,1,1 */
112+
NULL, /* stride: default to row-major convention */
113+
hDT,
114+
padfValues,
115+
NULL, /* array start. Omitted */
116+
0 /* array size in bytes. Omitted */);
117+
GDALExtendedDataTypeRelease(hDT);
118+
GDALMDArrayRelease(hVar);
119+
CPLFree(panOffset);
120+
CPLFree(panCount);
121+
VSIFree(padfValues);
122+
123+
return 0;
124+
}
125+
126+
.. code-tab:: python
127+
128+
from osgeo import gdal
129+
ds = gdal.OpenEx("in.nc", gdal.OF_MULTIDIM_RASTER)
130+
rootGroup = ds.GetRootGroup()
131+
var = rootGroup.OpenMDArray("temperature")
132+
data = var.Read(buffer_datatype = gdal.ExtendedDataType.Create(gdal.GDT_Float64))
133+
134+
# if NumPy is available we can use ReadAsArray
135+
data = var.ReadAsArray(buffer_datatype = gdal.ExtendedDataType.Create(gdal.GDT_Float64))
150136

151137

152138
Other examples

0 commit comments

Comments
 (0)