GRASS vector to MapInfo mif / mid

Alastair Duncan 100074.213 at compuserve.com
Thu Jun 5 12:34:34 EDT 1997


Hello,

As promised, I've attached some C code that converts GRASS ASCII vector lines
(not polygons) into MapInfo's .mif/.mid ASCII format. It's here for anybody to
hack about to their hearts content.
At the moment it only converts into the British National Grid projection - whose
parameters are hard coded into the programme. Also the input and output
filenames and paths are hard coded as #defines.
I've put quite a lot of notes in the code - so hopefully it shouldn't be to much
hassle to hack.

Hope it's of use to some people. Keep me informed of any updates people make.

Cheers, Al.

_____________________ cut here ___________________________________________

/**************************************************************************/
/***   vect2mif.c        programme that converts GRASS ASCII vector     ***/
/***                     file (with lines only) into MapInfo mif/mid    ***/
/***                     ASCII transfer format, retaining attributes.   ***/
/*** v.1.0 Thurs. 4th 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 GRASS ASCII file - vector and attribute ***/

#define DIGIN "/home8/grass/uk/lidar/dig_ascii/area9.sub.cont" 
#define ATTIN "/home8/grass/uk/lidar/dig_att/area9.sub.cont"

  /*** Then MapInfo ASCII file - vector .mif and attribute .mid ***/

#define MIFOUT "/home5/AlsJunk/lidar/area9sub.mif" 
#define MIDOUT "/home5/AlsJunk/lidar/area9sub.mid"



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

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

    FILE   *fptr_digin,      /*** input pointers - GRASS dig_ascii file ***/
           *fptr_attin,      /***                - GRASS dig_att file   ***/
           *fptr_mifout,     /*** output pointers- MapInfo .mif file    ***/
           *fptr_midout;     /***                - MapInfo .mid 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 ***/

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

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

    if ( (fptr_digin = fopen(DIGIN, "r") ) == NULL)
        fprintf(stderr, "Could not open file");      /*** GRASS dig_ascii **/

    if ( (fptr_attin = fopen(ATTIN, "r") ) == NULL)
        fprintf(stderr, "Could not open file");      /*** GRASS dig_att   **/

    fptr_mifout = fopen(MIFOUT,"a");                 /*** mif output file **/ 

    fptr_midout = fopen(MIDOUT,"a");                 /*** mid output file **/

 /***---------------------- WRITING MIF FILE HEADER -------------------***/

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

    fprintf(fptr_mifout, "Version 300\n");
    fprintf(fptr_mifout, "Charset \"WindowsLatin1\"\n");
    fprintf(fptr_mifout, "Delimiter \",\"\n");
    fprintf(fptr_mifout, "CoordSys Earth Projection 8, 79, \"m\", -2, 49,
0.9996012717, 400000, -100000 Bounds (-7845061.1011, -15524202.1641)
(8645061.1011, 4470074.53373)\n");
    fprintf(fptr_mifout, "Columns 1\n");
    fprintf(fptr_mifout, "  Elevation Float\n");
    fprintf(fptr_mifout, "Data\n");
    fprintf(fptr_mifout, " \n");

/***------- READING ASCII DIG VECTOR FILE AND WRITING MIF FILE --------***/

    for(count=1; count <=14; count++)
        fgets(line,sizeof(line),fptr_digin);   /** this skips past the ***/
                                               /** header of the GRASS ***/
                                               /** dig_ascii file.     ***/


    do
    {
     fgets(line,sizeof(line),fptr_digin);   /** this loop reads the dig ***/
     if(line[0] = 'L')                      /** file and writes the mif ***/
        {
         sscanf(line, "L  %d\n", &nodes);
         if(nodes >= 32000)
            printf("\n\n\t\tA LINE HAS MORE THAN 32000 NODES\n\n\t\tWILL NOT BE
READ BY MAPINFO\n\n\t\tGENERALISE VECTOR LAYER IN GRASS\n\n");
         fprintf(fptr_mifout, "Pline %d\n", nodes);
         for(count=1; count <= nodes; count ++)
            {
             fgets(coords,sizeof(coords),fptr_digin);
             sscanf(coords, " %lf %lf\n", &y_coord, &x_coord);
             fprintf(fptr_mifout, "%lf %lf\n", x_coord, y_coord); 
            }
         fprintf(fptr_mifout, "    Pen (1,2,16744448) \n");
        }
      }
      while(!feof(fptr_digin));

   
    do                                       /** this loop reads the att ***/
    {                                        /** file and writes the mid ***/
        fgets(line,sizeof(line),fptr_attin);
        sscanf(line, "L %lf %lf %lf\n", &x_coord, &y_coord, &elevation);
        if(!feof(fptr_attin))
          fprintf(fptr_midout, "%lf\n", elevation);
    }
    while(!feof(fptr_attin));


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

    fclose(fptr_digin);
    fclose(fptr_attin);
    fclose(fptr_mifout);
    fclose(fptr_midout);
}



More information about the grass-user mailing list