FortranGIS Version 3.0
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
19PROGRAM shapelib_dump
20use,INTRINSIC :: iso_c_binding
21USE shapelib
22IMPLICIT NONE
23
24INTEGER,PARAMETER :: lencharattr=40, nshp=4, tshp=shpt_polygonz
25
26TYPE(shpfileobject) :: shphandle
27TYPE(shpobject) :: shpobj
28INTEGER :: i, j
29CHARACTER(len=1024) :: filename
30
31INTEGER :: nshpr, tshpr, nfield, nrec, nd
32REAL(kind=c_double) :: minbound(4), maxbound(4)
33CHARACTER(len=lencharattr) :: charattrr
34INTEGER :: intattrr
35REAL(kind=c_double) :: doubleattrr
36
37CALL get_command_argument(1,filename)
38IF (filename == '') THEN
39 print'(A)','Usage: shapelib_dump <shp_file>'
40 stop
41ENDIF
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
47shphandle = shpopen(trim(filename), 'rb')
48! error check
49IF (shpfileisnull(shphandle) .OR. dbffileisnull(shphandle)) THEN
50 print*,'Error opening ',trim(filename),' for reading'
51 stop 1
52ENDIF
53
54! get general information about the shapefile object
55CALL shpgetinfo(shphandle, nshpr, tshpr, minbound, maxbound, nfield, nrec)
56print*,'number and type of shapes:',nshpr,tshpr
57
58! read the nshp shapes
59DO 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
81ENDDO
82
83! close the shapefile object
84CALL shpclose(shphandle)
85
86
87END PROGRAM shapelib_dump
88
Fortran 2003 interface to the shapelib http://shapelib.maptools.org/ library.
Definition shapelib.F90:53