close_ogr.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002 *
00003 * MODULE:       Vector library 
00004 *               
00005 * AUTHOR(S):    Radim Blazek, Piero Cavalieri 
00006 *
00007 * PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
00008 *
00009 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00010 *
00011 *               This program is free software under the GNU General Public
00012 *               License (>=v2). Read the file COPYING that comes with GRASS
00013 *               for details.
00014 *
00015 *****************************************************************************/
00016 #include <grass/Vect.h>
00017 #include <stdlib.h>
00018 
00019 #ifdef HAVE_OGR
00020 #include <ogr_api.h>
00021 
00022 /* 
00023 ** return 0 on success
00024 **         non-zero on error
00025 */
00026 int
00027 V1_close_ogr (struct Map_info *Map)
00028 {
00029     int i;
00030 
00031     if (!VECT_OPEN (Map)) return -1;
00032 
00033     if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW)
00034         Vect__write_head (Map);
00035 
00036     if ( Map->fInfo.ogr.feature_cache ) OGR_F_Destroy( Map->fInfo.ogr.feature_cache );
00037 
00038     OGR_DS_Destroy (Map->fInfo.ogr.ds);
00039 
00040     for ( i = 0; i < Map->fInfo.ogr.lines_alloc; i++ ) {
00041         Vect_destroy_line_struct ( Map->fInfo.ogr.lines[i] );
00042     }
00043     
00044     free ( Map->fInfo.ogr.lines );
00045     free ( Map->fInfo.ogr.lines_types );
00046     
00047     free (Map->fInfo.ogr.dsn);
00048     free (Map->fInfo.ogr.layer_name);
00049 
00050     return 0;
00051 }
00052 
00053 /* 
00054 *  Write OGR specific files (fidx)
00055 * 
00056 *  return 0 on success
00057 *         non-zero on error
00058 */
00059 int
00060 V2_close_ogr (struct Map_info *Map)
00061 {
00062     char fname[1000], elem[1000];
00063     char buf[5];
00064     long length = 9; 
00065     GVFILE fp;
00066     struct Port_info port;
00067         
00068     G_debug (3, "V2_close_ogr()");
00069     
00070     if (!VECT_OPEN (Map)) return -1;
00071 
00072     if ( strcmp(Map->mapset,G_mapset()) == 0 && Map->support_updated && Map->plus.built == GV_BUILD_ALL ) {
00073         sprintf (elem, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00074         G__file_name (fname, elem, "fidx", Map->mapset);
00075         G_debug (4, "Open fidx: %s", fname);
00076         dig_file_init ( &fp );
00077         fp.file = fopen( fname, "w");
00078         if ( fp.file ==  NULL) {
00079             G_warning("Can't open fidx file for write: %s\n", fname);
00080             return 1;
00081         }
00082         
00083         dig_init_portable ( &port, dig__byte_order_out ());
00084         dig_set_cur_port ( &port );
00085         
00086         /* Header */
00087         /* bytes 1 - 5 */
00088         buf[0] = 5;
00089         buf[1] = 0;
00090         buf[2] = 5;
00091         buf[3] = 0;
00092         buf[4] = (char) dig__byte_order_out();
00093         if (0 >= dig__fwrite_port_C (buf, 5, &fp)) return (1);
00094 
00095         /* bytes 6 - 9 : header size */
00096         if (0 >= dig__fwrite_port_L ( &length, 1, &fp)) return (1);
00097 
00098         /* Body */
00099         /* number of records  */
00100         if (0 >= dig__fwrite_port_I ( &(Map->fInfo.ogr.offset_num), 1, &fp)) return (1);
00101 
00102         /* offsets */
00103         if (0 >= dig__fwrite_port_I ( Map->fInfo.ogr.offset, 
00104                                       Map->fInfo.ogr.offset_num, &fp)) return (1);
00105 
00106         fclose( fp.file );
00107 
00108     }
00109 
00110     free (Map->fInfo.ogr.offset);
00111 
00112     return 0;
00113 }
00114 #endif

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5