open_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 <unistd.h>
00017 #include <string.h>
00018 #include <sys/types.h>
00019 #include <sys/stat.h>
00020 #include <grass/Vect.h>
00021 #include <grass/gis.h>
00022 
00023 #ifdef HAVE_OGR
00024 #include <ogr_api.h>
00025 
00026 /* Open old file.
00027 *  Map->name and Map->mapset must be set before
00028 *
00029 *  Return: 0 success
00030 *         -1 error
00031 */
00032 int
00033 V1_open_old_ogr (struct Map_info *Map, int update)
00034 {
00035     int i, layer, nLayers;
00036     OGRDataSourceH Ogr_ds;
00037     OGRLayerH Ogr_layer=NULL;
00038     OGRFeatureDefnH Ogr_featuredefn;
00039 
00040     if (update) {
00041         G_warning ("OGR format cannot be updated.");
00042         return -1;
00043     }
00044 
00045     G_debug (2, "V1_open_old_ogr(): dsn = %s layer = %s", Map->fInfo.ogr.dsn,
00046              Map->fInfo.ogr.layer_name);
00047 
00048     OGRRegisterAll ();
00049 
00050     /*Data source handle */
00051     Ogr_ds = OGROpen (Map->fInfo.ogr.dsn, FALSE, NULL);
00052     if (Ogr_ds == NULL)
00053         G_fatal_error ("Cannot open OGR data source '%s'", Map->fInfo.ogr.dsn);
00054     Map->fInfo.ogr.ds = Ogr_ds;
00055 
00056     /* Layer number */
00057     layer = -1;
00058     nLayers = OGR_DS_GetLayerCount (Ogr_ds);
00059     G_debug (2, "%d layers found in data source", nLayers);
00060 
00061     for (i = 0; i < nLayers; i++) {
00062         Ogr_layer = OGR_DS_GetLayer (Ogr_ds, i);
00063         Ogr_featuredefn = OGR_L_GetLayerDefn (Ogr_layer);
00064         if ( strcmp( OGR_FD_GetName (Ogr_featuredefn), Map->fInfo.ogr.layer_name) == 0) {
00065             layer = i;
00066             break;
00067         }
00068     }
00069     if (layer == -1) {
00070         OGR_DS_Destroy (Ogr_ds);
00071         G_fatal_error ("Cannot open layer '%s'", Map->fInfo.ogr.layer_name);
00072     }
00073     G_debug (2, "OGR layer %d opened", layer);
00074 
00075     Map->fInfo.ogr.layer = Ogr_layer;
00076 
00077     Map->fInfo.ogr.lines = NULL;
00078     Map->fInfo.ogr.lines_types = NULL;
00079     Map->fInfo.ogr.lines_alloc = 0;
00080     Map->fInfo.ogr.lines_num = 0;
00081     Map->fInfo.ogr.lines_next = 0;
00082 
00083     Map->head.with_z = WITHOUT_Z; /* TODO: 3D */
00084 
00085     Map->fInfo.ogr.feature_cache = NULL;
00086     Map->fInfo.ogr.feature_cache_id = -1; /* FID >= 0 */
00087 
00088     return (0);
00089 }
00090 
00091 /* Open OGR specific level 2 files (feature index).
00092 *
00093 *  Return: 0 success
00094 *         -1 error
00095 */
00096 int
00097 V2_open_old_ogr (struct Map_info *Map )
00098 {
00099     char elem[1000];
00100     char buf[5];
00101     long length; 
00102     GVFILE fp;
00103     struct Port_info port;
00104     int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
00105         
00106     G_debug (3, "V2_open_old_ogr()" );
00107     
00108     sprintf (elem, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00109     dig_file_init ( &fp );
00110     fp.file = G_fopen_old ( elem, "fidx", Map->mapset);
00111     if ( fp.file ==  NULL) {
00112         G_warning("Can't open fidx file for vector '%s@%s'.", Map->name, Map->mapset);
00113         return -1;
00114     }
00115     
00116     /* Header */
00117     if (0 >= dig__fread_port_C (buf, 5, &fp))  return (-1);
00118     Version_Major = buf[0];
00119     Version_Minor = buf[1];
00120     Back_Major    = buf[2];
00121     Back_Minor    = buf[3];
00122     byte_order    = buf[4];
00123 
00124 
00125     /* check version numbers */
00126     if ( Version_Major > 5 || Version_Minor > 0 ) {
00127         if ( Back_Major > 5 || Back_Minor > 0 ) {
00128             G_fatal_error ( "Feature index format version %d.%d is not supported by this release."
00129                             " Try to rebuild topology or upgrade GRASS.", Version_Major, Version_Minor);
00130             return (-1);
00131         }
00132         G_warning ( "Your GRASS version does not fully support feature index format %d.%d of the vector."
00133                     " Consider to rebuild topology or upgrade GRASS.",
00134                     Version_Major, Version_Minor );
00135     }
00136 
00137     dig_init_portable ( &port, byte_order );
00138     dig_set_cur_port ( &port );
00139 
00140     /* Body */
00141     /* bytes 6 - 9 : header size */
00142     if (0 >= dig__fread_port_L (&length, 1, &fp)) return (-1);
00143     G_debug (3, "  header size %d", length );
00144     
00145     fseek ( fp.file, length, SEEK_SET);
00146     
00147     /* number of records  */
00148     if (0 >= dig__fread_port_I (&(Map->fInfo.ogr.offset_num), 1, &fp)) return (-1);
00149 
00150     /* alloc space */
00151     Map->fInfo.ogr.offset = (int *) G_malloc ( Map->fInfo.ogr.offset_num * sizeof(int) );
00152     Map->fInfo.ogr.offset_alloc = Map->fInfo.ogr.offset_num;
00153 
00154     /* offsets */
00155     if (0 >= dig__fread_port_I ( Map->fInfo.ogr.offset, 
00156                                  Map->fInfo.ogr.offset_num, &fp)) return (-1);
00157 
00158     fclose( fp.file );
00159 
00160     G_debug (3, "%d records read from fidx", Map->fInfo.ogr.offset_num );
00161 
00162 
00163     Map->fInfo.ogr.next_line = 1;
00164 
00165     return 0;
00166 }
00167 
00168 #endif

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