program main
  use mod_io_shapefile
  implicit none
  type(shp_) :: shp
  type(dbf_) :: dbf
  character(128) :: f
  integer :: iEntity
  integer :: iPart
  type(shp_entity_), pointer :: entity
  type(shp_part_)  , pointer :: part
  integer :: iField
  integer :: iRecord
  type(dbf_field_) , pointer :: field
  type(dbf_record_), pointer :: record

  f = '/data19/akira/land/Japan/GMJ/data/org/all_japan_d_a'

  print*, 'Open '//trim(f)//'.shp'
  call SHPOpen(f)

  print*, 'Get all data.'
  call SHPGetAll(shp)

  print*, 'Close the file.'
  call SHPClose()

  print*, 'Open '//trim(f)//'.dbf'
  call DBFOpen(f)

  print*, 'Get all data.'
  call DBFGetAll(dbf)

  print*, 'Close the file.'
  call DBFClose()


  print*, '================================ shp ================================'
  print*, 'nEntities:', shp%nEntities
  print*, 'SHPType:', shp%SHPType, shp%SHPTypeName
  print"(1x,a,4(1x,es12.5))", 'minBound:', shp%minBound(:)
  print"(1x,a,4(1x,es12.5))", 'maxBound:', shp%maxBound(:)

  iEntity = 1
  entity => shp%entity(iEntity)
  print*, 'Entity', iEntity
  print*, '  nParts:', entity%nParts
  do iPart = 1, entity%nParts
    part => entity%part(iPart)
    print"(3x,2(a,i6),4(a,f11.6))", &
           'Part', iPart, ' nVertices: ', part%nVertices, &
           ', x: ', part%x(1), ', ..., ', part%x(part%nVertices), &
           ', y: ', part%y(1), ', ..., ', part%y(part%nVertices)
  enddo

  print*, '================================ dbf ================================'
  print*, 'nRecords:', dbf%nRecords
  print*, 'nFields:', dbf%nFields
  do iField = 1, dbf%nFields
    field => dbf%field(iField)
    print"(3x,3(a,i0))", &
           'Field ', iField,&
           ': Type='//trim(field%typeChar)//'/'//trim(field%typeName)//&
           ", Title='"//trim(field%title)//"', Width=", field%width,', Decimals=', field%decimals
  enddo

  iRecord = 1
  record => dbf%record(iRecord)
  print*, 'Record', iRecord
  do iField = 1, dbf%nFields
    field => dbf%field(iField)
    selectcase( field%typeChar )
    case( 'C' )  ! String
      print"(3x,a,1x,i0,1x,a,1x,a)", &
             'Field', iField, trim(field%title)//':', trim(record%value(iField)%s)
    case( 'N' )  ! Integer
      print"(3x,a,1x,i0,1x,a,1x,i0)", &
             'Field', iField, trim(field%title)//':', record%value(iField)%i
    case( 'D' )  ! Double
      print"(3x,a,1x,i0,1x,a,1x,es12.5)", &
             'Field ', iField, trim(field%title)//':', record%value(iField)%d
    endselect
  enddo
end program main