Conversion filter

Rich Shepard rshepard at appl-ecosys.com
Thu Jul 13 17:49:48 EDT 2000


  For anyone who wants it, the attached file compiles and works under linux.
It takes a DEM file exported in GRD format from ARC/Info and creates a text
file of x, y, z triplets.

  Thanks to Michel Wurtz for instructing me that the code compiles cleanly
without modification as soon as the Makefile is removed.

Rich

Dr. Richard B. Shepard, President

                       Applied Ecosystem Services, Inc. (TM)
              Making environmentally-responsible mining happen. (SM)         
                       --------------------------------
            2404 SW 22nd Street | Troutdale, OR 97060-1247 | U.S.A.
 + 1 503-667-4517 (voice) | + 1 503-667-8863 (fax) | rshepard at appl-ecosys.com
-------------- next part --------------
/* ==================================================================== *\

  grid2mi.c
	converts ESRI ASCII Export format GRD data to X Y Z format

  Copyright (c) 1996, 1997 The Codecrafters Guild

\* ==================================================================== */

#include <stdio.h>
#include <math.h>
#include <string.h>
#define BUFF_SIZE 100

main (int argc, char **argv)
	{
    char fname [64];
    char *p;
    FILE *infile, *outfile;
    double swx, swy, nex, ney;
    double xcell, ycell;
    char sBuffer[BUFF_SIZE+1];
    int i, j, k, rows, cols, z;
    double x, y;

    /* make sure user enters file name on command line */
    if (argc != 2) {
      puts("Usage is: GRID2MI <.E00 GRD filename>");
      return(0);
    	}

	/* open input file */
    strcpy(fname, argv[1]);
    if ((infile = fopen(fname, "rt")) == NULL) {
      printf("Can't open %s\n", fname);
      return(0);
    	}

	/* change the extension for outout file */
    p = strchr(fname, '.');
    *p = '\0';
    strcat(fname,".txt");

    /* open output file */
    if ((outfile = fopen(fname, "wt")) == NULL) {
      printf("Can't open %s\n", fname);
      return(0);
    	}

	/* throw away the first two lines; we don't need them */
  fgets (sBuffer, BUFF_SIZE, infile);
  fgets (sBuffer, BUFF_SIZE, infile);

	/* read and display relevant header data */
  fgets (sBuffer, BUFF_SIZE, infile);
	sscanf (sBuffer, "%d %d", &cols, &rows);
  fgets (sBuffer, BUFF_SIZE, infile);
	sscanf (sBuffer, "%lE%lE", &xcell, &ycell);
	printf ("Columns: %d Rows: %d\n", cols, rows);
	printf ("Cell width X: %5.2lf Cell width Y: %5.2lf\n", xcell, ycell);
  fgets (sBuffer, BUFF_SIZE, infile);
	sscanf (sBuffer, "%lE%lE", &swx, &swy);
  fgets (sBuffer, BUFF_SIZE, infile);
	sscanf (sBuffer, "%lE%lE", &nex, &ney);
  printf ("SW corner: %8.3lf %8.3lf\n", swx, swy);
  printf ("NE Corner: %8.3lf %8.3lf\n", nex, ney);

    /* put a header in the output file. Records are tab-delimted */
  fprintf (outfile,"X\tY\tZ\n");

	/*
	Process the points

	The ESRI export GRD format is not officially documented, so we are
	just guessing here. Caveat Emptor!

	First point is the NW corner, 2nd point is to the east on same row.
	Do all points on the north row first, then do the next row to the
	south, and so on.

	At the end of a row, we throw away any leftovers in the 5-digit
	buffer, and start the next row on the next line.
	*/

	x = swx; y = ney; // start at NW corner
	k = 0;
  fgets (sBuffer, BUFF_SIZE, infile);
  for (i=rows; i>0; i--) // do rows from north to south
    	{
	    for (j=0; j<cols; j++) // do columns west to east
    		{
    		if (k == 5) // get a new row of data from E00 file
    			{
          fgets (sBuffer, BUFF_SIZE, infile);
    			k = 0;
    			}

			/* parse the record */
    		sscanf (&sBuffer[k*14], "%d", &z);
    		k++;

    		/* write X,Y,Z data to file; convert X, Y to feet */
        fprintf (outfile,"%8.3lf\t%8.3lf\t%d\n", x, y, z);

    		/* tell the user */
        printf ("row: %3d\r", i);

    		x = x + xcell;
    		}
        fgets (sBuffer, BUFF_SIZE, infile);
   			k = 0;
    	x = swx;
    	y = y - ycell;
		}

	printf ("Done!  \n");
	fcloseall();
	return (1);
	}


More information about the grass-user mailing list