Mapinfo .mif/.mid to GRASS vector C code

Alastair Duncan 100074.213 at compuserve.com
Fri Jun 6 10:45:17 EDT 1997


Hello,

I've just finished writing a routine in C that converts a MapInfo .mif / .mid
file that contains "Polylines" ONLY into a GRASS ASCII vector file. It also
converts the .mid files first attribute field into a GRASS vector attribute
file, and attaches the attribute to the second node of the polyline. The
projection used is a pseudo-British National Grid, though I guess it could be
used for any UTM projection - play around with the parameters that are hard
coded to write the GRASS ASCII dig file's header. As with the vect2mif.c routine
posted earlier to the list, the input and output filenames and paths are hard
coded as #defines. You have to run v.to.ascii and v.support in order to get the
vectors into a binary vector file, with topology and attributes. The code is
attached below.

Please feel free to hack the code, but if you upgrade it to handle polygons and
sites please let me know.

Hope people find it useful,
Cheers, Al.

--------------------------------- Cut Here
------------------------------------------

/**************************************************************************/
/***   mif2vect.c        programme that converts MapInfo mif/mid ASCII  ***/
/***                     vector file (with lines only) into GRASS ASCII ***/
/***                     vector format, retaining attributes.           ***/
/*** v.1.0 Fri. 6th June '97, Alastair Duncan, Environment Agency, UK   ***/
/**************************************************************************/

#include <stdio.h>
#include <string.h>

        /****------  DEFINE INPUT AND OUTPUT FILE PATHS AND NAMES  ----****/
        /*** THESE SHOULD BE KEYBOARD INPUT ARGUMENTS IN FINAL VERSION ****/

  /*** First MAPINFO ASCII file - vector .mif and attribute .mid ***/

#define MIFIN "/home5/AlsJunk/lidar/temp.mif"
#define MIDIN "/home5/AlsJunk/lidar/temp.mid"

  /*** Then GRASS ASCII file - vector dig and attribute att ***/

#define DIGOUT "/home8/grass/uk/lidar/dig_ascii/temp" 
#define ATTOUT "/home8/grass/uk/lidar/dig_att/temp"



        /****------------------------- MAIN ---------------------------****/

main()
{
           /***----  DECLARE VARIABLES ----***/

    FILE   *fptr_mifin,      /*** input pointers - MapInfo .mif file   ***/
           *fptr_midin,      /***                - MapInfo .mid file   ***/
           *fptr_digout,     /*** output pointers- GRASS dig_ascii file***/
           *fptr_attout;     /***                - GRASS dig_att file  ***/

    int     count,           /*** counter for loops                    ***/
            nodes;           /*** counts number of nodes in line       ***/

    char    line[255],       /*** string to read a line of text        ***/
            coords[255],     /*** string to read a line of coordinates ***/
            midline[255];    /*** string to read line of .mid file     ***/

    double  x_coord,         /*** double precision floats to store     ***/
            y_coord,         /*** coordinates and z value.             ***/
            elevation;

/***------------------- OPEN INPUT AND OUTPUT FILES --------------------***/

    if ( (fptr_mifin = fopen(MIFIN, "r") ) == NULL)
        fprintf(stderr, "Could not open file");      /*** mif input file  **/

    if ( (fptr_midin = fopen(MIDIN, "r") ) == NULL)
        fprintf(stderr, "Could not open file");      /*** mid input file  **/

    fptr_digout = fopen(DIGOUT,"a");                 /*** dig output file **/ 

    fptr_attout = fopen(ATTOUT,"a");                 /*** att output file **/

 /***--------------- WRITING GRASS VECTOR DIG FILE HEADER ---------------***/

 /*** following is the heading for pseudo British National Grid projection ***/
 /*** consult GRASS manual for other projections, or make dummy GRASS      ***/
 /*** ASCII Vector file in projection of choice and cut and paste in the   ***/
 /*** necessary parameters.                                                ***/

    fprintf(fptr_digout, "ORGANIZATION: US Army Const. Eng. Rsch. Lab\n");
    fprintf(fptr_digout, "DIGIT DATE:   \n");
    fprintf(fptr_digout, "DIGIT NAME:   \n");
    fprintf(fptr_digout, "MAP NAME:     \n");
    fprintf(fptr_digout, "MAP DATE:     \n");
    fprintf(fptr_digout, "MAP SCALE:    24000\n");
    fprintf(fptr_digout, "OTHER INFO:   \n");
    fprintf(fptr_digout, "ZONE:         0\n");
    fprintf(fptr_digout, "WEST EDGE:    0\n");
    fprintf(fptr_digout, "EAST EDGE:    660000\n");
    fprintf(fptr_digout, "SOUTH EDGE:   0\n");
    fprintf(fptr_digout, "NORTH EDGE:   1250000\n");
    fprintf(fptr_digout, "MAP THRESH:   0\n");
    fprintf(fptr_digout, "VERTI:\n");


/***--- READING ASCII DIG AND ATT FILE AND WRITING MIF AND MID FILE ---***/

    for(count=1; count <=8; count++)
        fgets(line,sizeof(line),fptr_mifin);   /** this skips past the ***/
                                               /** header of the       ***/
                                               /** MapInfo .mif file.  ***/


    do
    {
     fgets(line,sizeof(line),fptr_mifin);   /** this loop reads the mif ***/
     if(!feof(fptr_mifin))                  /** file and writes the dig ***/
       {
        if(line[1] == 'l')
           {
            sscanf(line, "Pline %d\n", &nodes);
            fprintf(fptr_digout, "L  %d\n", nodes);
            for(count=1; count <= nodes; count ++)
               {
                fgets(coords,sizeof(coords),fptr_mifin);
                sscanf(coords, "%lf %lf\n", &y_coord, &x_coord);
                fprintf(fptr_digout, " %lf %lf\n", x_coord, y_coord);

                if(count == 2)             /** this loop reads the mid ***/
                   {                       /** file and writes the att ***/
                                           /** using 2nd node to attach***/
                                           /** the attribute.          ***/
                    fgets(midline,sizeof(midline),fptr_midin);
                    sscanf(midline, "%lf\n", &elevation);
                    if(!feof(fptr_midin))
                       fprintf(fptr_attout, "L %lf %lf       %.0lf\n", y_coord,
x_coord, elevation);
                   } 
               }
           }
        }
      }
      while(!feof(fptr_mifin));

   
 /***------------------ CLOSING INPUT AND OUTPUT FILES -----------------***/

    fclose(fptr_mifin);
    fclose(fptr_midin);
    fclose(fptr_digout);
    fclose(fptr_attout);
}



More information about the grass-user mailing list