

/* APRS Color codes - I don't know how we can use them, but they are useful, I supose 
 * 0 - Black (normal background)     * 8 - dark gray (Railroads)
 * 1 - dim blue (ferrys, etc)          9 - Bright Blue
 * 2 - dim grn (Admin areas, Parks)  *10 - Bright Green (Interstates)
 * *3 - dim cyan (Rivers)            *11 - Bright Cyan (Big rivers, Coasts)
 * 4 - deep red (state roads)        *12 - Bright Red   (major roads)
 * 5 - dim violet (custom features)   13 - Bright Violet(special events)
 * *6 - dim orange (state/cnty lines) 14 - Bright Yellow (Cities,airports)
 * *7 - gray     (back roads)         15 - Bright White  (Labels and CALLS
 */

/* Sample map file that Adam Frisch KB0VYO and James Jefferson KB0THN made 

 44.14 ,
 92.06 ,
 1800 ,Pixels per degree
 43.86222 ,   LAT of CENTER
 91.64 ,LON of CENTER
 16.66667 ,Map range from center
 1 ,Reserved
ASCII
  9 ,Box around map
1,1
1,540
878,540
878,1
0,0
  9 , Top line
1,1
1,540
0,0
  9 , 'O'
1,68
878,68
0,0
*/


#include "shapefil.h"
#include <stdio.h>

double atof ();

int
main (int argc, char **argv)
{
  SHPHandle hSHP;
  int nShapeType, nEntities, i, iPart;
  const char *pszPlus;
  double adfMinBound[4], adfMaxBound[4];
  double *xpr, *ypr;
  unsigned long l;
  // DBF Stuff
  DBFHandle hDBF;
  int *panWidth, iRecord;
  char szFormat[32], szField[1024], *pszFilename = NULL;
  int nWidth, nDecimals;
  int bHeader = 0;
  int bRaw = 0;
  int bMultiLine = 0;
  char szTitle[12];
  int name_field = 1;
  unsigned long x = 0;

/* Display a usage message. */
  printf ("argc = %d\n", argc);
  if (argc != 3)
    {
      printf ("%s shp_file name_field\n", argv[0]);
      exit (1);
    }

  name_field = atoi (argv[2]);


/* Open the passed shapefile. */
  hSHP = SHPOpen (argv[1], "rb");

  if (hSHP == NULL)
    {
      printf ("Unable to open:%s\n", argv[1]);
      exit (1);
    }

#if 1
/* Open DBF file associated with shapefile. */
  pszFilename = argv[1];	// DBF
  hDBF = DBFOpen (pszFilename, "rb");
  if (hDBF == NULL)
    {
      printf ("DBFOpen(%s,\"r\") failed.\n", argv[1]);
      exit (2);
    }
/* Warn user that DBF file doesn't contain anything */
  if (DBFGetFieldCount (hDBF) == 0)
    {
      printf ("There are no fields in this table!\n");
      exit (3);
    }
#endif

/*      Print out the file bounds.                                      */
  SHPGetInfo (hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound);

#if 1
  printf ("Shapefile Type: %s   # of Shapes: %d\n\n",
	  SHPTypeName (nShapeType), nEntities);
#endif

  // Check to makesure we are dealing with a polygon shapefile
  if (nShapeType % 5 == 0)
    {
      printf ("Polygon File\n");
    }
  else
    {
      printf ("Not a polygon File!\n");
      return 1;
    }


  printf ("File Bounds: (%12.3f,%12.3f,%lg,%lg)\n"
	  "         to  (%12.3f,%12.3f,%lg,%lg)\n",
	  adfMinBound[0],
	  adfMinBound[1],
	  adfMinBound[2],
	  adfMinBound[3],
	  adfMaxBound[0], adfMaxBound[1], adfMaxBound[2], adfMaxBound[3]);

#if 0
  printf ("(Point to test) Latitude: %04.3f Longitude: %04.3f\n", latitude,
	  longitude);

  if ((longitude < adfMinBound[1]) || (longitude > adfMaxBound[1])
      || (latitude < adfMinBound[0]) || (latitude > adfMaxBound[0]))
    {
      fprintf (stdout, "Test point is outside of file bounds.\n");
      exit (10);
    }
#endif

/* -------------------------------------------------------------------- */
/*		Skim over the list of shapes, printing all the vertices.		*/
/* -------------------------------------------------------------------- */
  for (i = 0; i < nEntities; i++)
    {
      int j;
      SHPObject *psShape;

      psShape = SHPReadObject (hSHP, i);
#if 1
      printf ("\nShape:%d (%s)  nVertices=%d, nParts=%d\n"
	      "  Bounds:(%12.3f,%12.3f, %lg, %lg)\n"
	      "      to (%12.3f,%12.3f, %lg, %lg)\n",
	      i, SHPTypeName (psShape->nSHPType),
	      psShape->nVertices, psShape->nParts,
	      psShape->dfXMin, psShape->dfYMin,
	      psShape->dfZMin, psShape->dfMMin,
	      psShape->dfXMax, psShape->dfYMax,
	      psShape->dfZMax, psShape->dfMMax);
#endif



      xpr = psShape->padfX;
      ypr = psShape->padfY;
	  printf("   %s (%12.3f,%12.3f, %lg, %lg) \n",
                   pszPlus,
                   psShape->padfX[j],
                   psShape->padfY[j],
                   psShape->padfZ[j],
                   psShape->padfM[j]);


      SHPClose (hSHP);

#ifdef USE_DBMALLOC
      malloc_dump (2);
#endif

      exit (0);
    }
}
