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

Generated on Sat Jul 22 22:05:57 2006 for GRASS by  doxygen 1.4.7