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' 36 ds = gdalcreate(driver, trim(file)//char(0), i1, j1, k1, gdt_byte, &
40 IF (.NOT.gdalassociated(ds))
THEN 41 print*,
'Error creating a GeoTIFF dataset on file ',trim(file)
46 print*,
'Setting color interpretation to RGB' 47 ierr = gdalsetrastercolorinterpretation(gdalgetrasterband(ds, 1), gci_redband)
48 ierr = gdalsetrastercolorinterpretation(gdalgetrasterband(ds, 2), gci_greenband)
49 ierr = gdalsetrastercolorinterpretation(gdalgetrasterband(ds, 3), gci_blueband)
53 ALLOCATE(z3(i1,j1,k1))
57 z3(i,j,k) = i/2+j*mod(k,2)+(j1-j)*(1-mod(k,2))
66 print*,
'Writing data to dataset' 67 print*,
'using the simplified Fortran interface' 70 print*,
'Error writing data to GeoTIFF dataset on file ',trim(file)
78 print*,
'Opening a GeoTIFF gdal dataset for reading' 79 print*,
'using the simplified Fortran interface' 80 ds = gdalopen(trim(file)//char(0), ga_readonly)
81 IF (.NOT.gdalassociated(ds))
THEN 82 print*,
'Error opening dataset on file ',trim(file)
86 print*,
'Getting the affine transformation' 87 ierr = gdalgetgeotransform(ds, gt)
93 print*,
'The affine transformation matrix is: ',gt
95 print*,
'Getting the dataset size' 96 i2 = gdalgetrasterxsize(ds)
97 j2 = gdalgetrasterysize(ds)
98 k2 = gdalgetrastercount(ds)
99 IF (i1 /= i2 .OR. j1 /= j2)
THEN 100 print*,
'Error, wrong dataset x or y size ',i1,i2,j1,j2
103 print*,
'The x/y size of the raster is: ',i2,j2
106 print*,
'Error, wrong raster band number ',k1,k2
109 print*,
'The number of raster bands is: ',k2
113 CALL gdalapplygeotransform(gt, 0.5_c_double, 0.5_c_double, x1, y1)
116 IF (x1 /= x2 .OR. y1 /= y2)
THEN 117 print*,
'Error gdal and Fortran version of GDALApplyGeoTransform' 118 print*,
'give different results' 124 REAL(i1, kind=c_double)-0.5_c_double, &
125 REAL(j1, kind=c_double)-0.5_c_double, x2, y2)
126 print*,
'The raster coordinates of the corners are: ',x1,y1,x2,y2
131 print*,
'Getting a raster band' 132 band = gdalgetrasterband(ds, 1)
133 IF (.NOT.gdalassociated(band))
THEN 134 print*,
'Error getting raster band from GeoTIFF dataset on file ',trim(file)
140 print*,
'Reading data from dataset' 143 print*,
'Error reading data from GeoTIFF dataset on file ',trim(file)
144 print*,
'with simplified Fortran interface' 148 print*,
'The sum of the buffer read is: ',sum(z)
149 print*,
'Average error after write/read process: ',&
150 sum(abs(z(:,:) - z3(:,:,1)))/(i2*j2)
156 print*,
'Opening a GeoTIFF gdal dataset for reading' 157 print*,
'using the even more simplified Fortran interface' 158 ds = gdalopen(trim(file)//char(0), ga_readonly)
159 IF (.NOT.gdalassociated(ds))
THEN 160 print*,
'Error opening dataset on file ',trim(file)
166 print*,
'Reading data from dataset' 167 CALL gdaldatasetsimpleread_f(ds, 5._c_double, 5._c_double, 20._c_double, 15._c_double, &
170 IF (.NOT.
ALLOCATED(zr3))
THEN 171 print*,
'Error reading data from GeoTIFF dataset on file ',trim(file)
172 print*,
'with even more simplified Fortran interface' 176 print*,
'The shape of the buffer read is: ',shape(zr3)
177 print*,
'The sum of the buffer read is: ',sum(zr3)
178 print*,
'The raster coordinates of the corners are: ',x1,y1,x2,y2
184 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.
Fortran 2003 interface to the gdal http://www.gdal.org/ library.
Utility module for supporting Fortran 2003 C language interface module.