2 use,
INTRINSIC :: iso_c_binding
7 TYPE(gdaldriverh) :: driver
8 TYPE(gdaldataseth) :: ds
9 TYPE(gdalrasterbandh) :: band
10 CHARACTER(len=512) :: file
11 REAL(kind=c_double) :: x1, y1, x2, y2, gt(6)
12 INTEGER(kind=c_int) :: i1, j1, k1, i2, j2, k2, i, j, k, ierr
13 REAL,
ALLOCATABLE :: z(:,:), z3(:,:,:), zr3(:,:,:)
17 CALL gdalallregister()
19 file =
'gdal_test.tiff'
24 print*,
'Getting GeoTIFF driver'
25 driver = gdalgetdriverbyname(
'GTiff'//char(0))
26 IF (.NOT.gdalassociated(driver))
THEN
27 print*,
'Error getting GeoTIFF driver from gdal'
32 print*,
'Creating a GeoTIFF gdal dataset'
39 ds = gdalcreate(driver, trim(file)//char(0), i1, j1, k1, gdt_byte, &
40 c_ptr_ptr_getobject(
c_ptr_ptr_new((/
'BIGTIFF=YES ',
'COMPRESS=DEFLATE'/))))
45 IF (.NOT.gdalassociated(ds))
THEN
46 print*,
'Error creating a GeoTIFF dataset on file ',trim(file)
51 print*,
'Setting color interpretation to RGB'
52 ierr = gdalsetrastercolorinterpretation(gdalgetrasterband(ds, 1), gci_redband)
53 ierr = gdalsetrastercolorinterpretation(gdalgetrasterband(ds, 2), gci_greenband)
54 ierr = gdalsetrastercolorinterpretation(gdalgetrasterband(ds, 3), gci_blueband)
58 ALLOCATE(z3(i1,j1,k1))
62 z3(i,j,k) = i/2+j*mod(k,2)+(j1-j)*(1-mod(k,2))
71 print*,
'Writing data to dataset'
72 print*,
'using the simplified Fortran interface'
75 print*,
'Error writing data to GeoTIFF dataset on file ',trim(file)
83 print*,
'Opening a GeoTIFF gdal dataset for reading'
84 print*,
'using the simplified Fortran interface'
85 ds = gdalopen(trim(file)//char(0), ga_readonly)
86 IF (.NOT.gdalassociated(ds))
THEN
87 print*,
'Error opening dataset on file ',trim(file)
91 print*,
'Getting the affine transformation'
92 ierr = gdalgetgeotransform(ds, gt)
98 print*,
'The affine transformation matrix is: ',gt
100 print*,
'Getting the dataset size'
101 i2 = gdalgetrasterxsize(ds)
102 j2 = gdalgetrasterysize(ds)
103 k2 = gdalgetrastercount(ds)
104 IF (i1 /= i2 .OR. j1 /= j2)
THEN
105 print*,
'Error, wrong dataset x or y size ',i1,i2,j1,j2
108 print*,
'The x/y size of the raster is: ',i2,j2
111 print*,
'Error, wrong raster band number ',k1,k2
114 print*,
'The number of raster bands is: ',k2
118 CALL gdalapplygeotransform(gt, 0.5_c_double, 0.5_c_double, x1, y1)
121 IF (x1 /= x2 .OR. y1 /= y2)
THEN
122 print*,
'Error gdal and Fortran version of GDALApplyGeoTransform'
123 print*,
'give different results'
129 REAL(i1, kind=c_double)-0.5_c_double, &
130 REAL(j1, kind=c_double)-0.5_c_double, x2, y2)
131 print*,
'The raster coordinates of the corners are: ',x1,y1,x2,y2
136 print*,
'Getting a raster band'
137 band = gdalgetrasterband(ds, 1)
138 IF (.NOT.gdalassociated(band))
THEN
139 print*,
'Error getting raster band from GeoTIFF dataset on file ',trim(file)
145 print*,
'Reading data from dataset'
148 print*,
'Error reading data from GeoTIFF dataset on file ',trim(file)
149 print*,
'with simplified Fortran interface'
153 print*,
'The sum of the buffer read is: ',sum(z)
154 print*,
'Average error after write/read process: ',&
155 sum(abs(z(:,:) - z3(:,:,1)))/(i2*j2)
161 print*,
'Opening a GeoTIFF gdal dataset for reading'
162 print*,
'using the even more simplified Fortran interface'
163 ds = gdalopen(trim(file)//char(0), ga_readonly)
164 IF (.NOT.gdalassociated(ds))
THEN
165 print*,
'Error opening dataset on file ',trim(file)
171 print*,
'Reading data from dataset'
172 CALL gdaldatasetsimpleread_f(ds, 5._c_double, 5._c_double, 20._c_double, 15._c_double, &
175 IF (.NOT.
ALLOCATED(zr3))
THEN
176 print*,
'Error reading data from GeoTIFF dataset on file ',trim(file)
177 print*,
'with even more simplified Fortran interface'
181 print*,
'The shape of the buffer read is: ',shape(zr3)
182 print*,
'The sum of the buffer read is: ',sum(zr3)
183 print*,
'The raster coordinates of the corners are: ',x1,y1,x2,y2
189 END PROGRAM gdal_test
Constructor for a c_ptr_ptr object.
Simplified Fortran generic interface to the gdaldatasetrasterio C function.
Simplified Fortran generic interface to the gdalrasterio C function.
Utility module for supporting Fortran 2003 C language interface module.
Fortran 2003 interface to the gdal http://www.gdal.org/ library.