FortranGIS  Version2.5
shapelib_dump.F90
1 ! Copyright 2011 Davide Cesari <dcesari69 at gmail dot com>
2 !
3 ! This file is part of FortranGIS.
4 !
5 ! FortranGIS is free software: you can redistribute it and/or modify
6 ! it under the terms of the GNU Lesser General Public License as
7 ! published by the Free Software Foundation, either version 3 of the
8 ! License, or (at your option) any later version.
9 !
10 ! FortranGIS is distributed in the hope that it will be useful, but
11 ! WITHOUT ANY WARRANTY; without even the implied warranty of
12 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ! Lesser General Public License for more details.
14 !
15 ! You should have received a copy of the GNU Lesser General Public
16 ! License along with FortranGIS. If not, see
17 ! <http://www.gnu.org/licenses/>.
18 
19 PROGRAM shapelib_dump
20 use,INTRINSIC :: iso_c_binding
21 USE shapelib
22 IMPLICIT NONE
23 
24 INTEGER,PARAMETER :: lencharattr=40, nshp=4, tshp=shpt_polygonz
25 
26 TYPE(shpfileobject) :: shphandle
27 TYPE(shpobject) :: shpobj
28 INTEGER :: i, j
29 CHARACTER(len=1024) :: filename
30 
31 INTEGER :: nshpr, tshpr, nfield, nrec, nd
32 REAL(kind=c_double) :: minbound(4), maxbound(4)
33 CHARACTER(len=lencharattr) :: charattrr
34 INTEGER :: intattrr
35 REAL(kind=c_double) :: doubleattrr
36 
37 CALL getarg(1,filename)
38 IF (filename == '') THEN
39  print'(A)','Usage: shapelib_dump <shp_file>'
40  stop
41 ENDIF
42 
43 ! ==== How to read a shapefile ====
44 
45 ! open an exixting shapefile and associate it to a shapefile object
46 ! filename does not include extension
47 shphandle = shpopen(trim(filename), 'rb')
48 ! error check
49 IF (shpfileisnull(shphandle) .OR. dbffileisnull(shphandle)) THEN
50  print*,'Error opening ',trim(filename),' for reading'
51  stop 1
52 ENDIF
53 
54 ! get general information about the shapefile object
55 CALL shpgetinfo(shphandle, nshpr, tshpr, minbound, maxbound, nfield, nrec)
56 print*,'number and type of shapes:',nshpr,tshpr
57 
58 ! read the nshp shapes
59 DO i = 0, nshpr - 1
60  print*,'Checking shape',i
61 ! read the i-th shape from the shapefile object and obtain a shape object
62  shpobj = shpreadobject(shphandle, i)
63 ! error check
64  IF (shpisnull(shpobj)) THEN
65  print*,'Error in shpreadobject',i
66  stop 1
67  ENDIF
68 
69 ! now access all the components of the shape object
70 ! number of vertices
71  print*,'nvertices:',shpobj%nvertices
72  IF (ASSOCIATED(shpobj%panpartstart)) THEN
73  print*,'nparts:',SIZE(shpobj%panpartstart)
74  print*,shpobj%panpartstart
75  ENDIF
76 
77 ! destroy the shape object to avoid memory leaks
78 ! notice that for accessing dbf attributes the shape object is not required
79  CALL shpdestroyobject(shpobj)
80 
81 ENDDO
82 
83 ! close the shapefile object
84 CALL shpclose(shphandle)
85 
86 
87 END PROGRAM shapelib_dump
88 
Object describing the geometrical properties of a shape.
Definition: shapelib.F90:95
Fortran 2003 interface to the shapelib http://shapelib.maptools.org/ library.
Definition: shapelib.F90:53
Object describing a shapefile dataset.
Definition: shapelib.F90:84