00001
00021 #include <unistd.h>
00022 #include <string.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025
00026 #include <grass/Vect.h>
00027 #include <grass/gis.h>
00028 #include <grass/glocale.h>
00029
00030 #ifdef HAVE_OGR
00031 #include <ogr_api.h>
00032
00045 int V1_open_old_ogr(struct Map_info *Map, int update)
00046 {
00047 int i, layer, nLayers;
00048 OGRDataSourceH Ogr_ds;
00049 OGRLayerH Ogr_layer = NULL;
00050 OGRFeatureDefnH Ogr_featuredefn;
00051
00052 if (update) {
00053 G_warning(_("OGR format cannot be updated"));
00054 return -1;
00055 }
00056
00057 G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", Map->fInfo.ogr.dsn,
00058 Map->fInfo.ogr.layer_name);
00059
00060 OGRRegisterAll();
00061
00062
00063 Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
00064 if (Ogr_ds == NULL)
00065 G_fatal_error(_("Unable to open OGR data source '%s'"),
00066 Map->fInfo.ogr.dsn);
00067 Map->fInfo.ogr.ds = Ogr_ds;
00068
00069
00070 layer = -1;
00071 nLayers = OGR_DS_GetLayerCount(Ogr_ds);
00072 G_debug(2, "%d layers found in data source", nLayers);
00073
00074 for (i = 0; i < nLayers; i++) {
00075 Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
00076 Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
00077 if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name)
00078 == 0) {
00079 layer = i;
00080 break;
00081 }
00082 }
00083 if (layer == -1) {
00084 OGR_DS_Destroy(Ogr_ds);
00085 G_fatal_error(_("Unable to open layer <%s>"),
00086 Map->fInfo.ogr.layer_name);
00087 }
00088 G_debug(2, "OGR layer %d opened", layer);
00089
00090 Map->fInfo.ogr.layer = Ogr_layer;
00091
00092 Map->fInfo.ogr.lines = NULL;
00093 Map->fInfo.ogr.lines_types = NULL;
00094 Map->fInfo.ogr.lines_alloc = 0;
00095 Map->fInfo.ogr.lines_num = 0;
00096 Map->fInfo.ogr.lines_next = 0;
00097
00098 Map->head.with_z = WITHOUT_Z;
00099
00100 Map->fInfo.ogr.feature_cache = NULL;
00101 Map->fInfo.ogr.feature_cache_id = -1;
00102
00103 return (0);
00104 }
00105
00114 int V2_open_old_ogr(struct Map_info *Map)
00115 {
00116 char elem[GPATH_MAX];
00117 char buf[5];
00118 long length;
00119 GVFILE fp;
00120 struct Port_info port;
00121 int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
00122
00123 G_debug(3, "V2_open_old_ogr()");
00124
00125 sprintf(elem, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00126 dig_file_init(&fp);
00127 fp.file = G_fopen_old(elem, "fidx", Map->mapset);
00128 if (fp.file == NULL) {
00129 G_warning(_("Unable to open fidx file for vector map <%s@%s>"),
00130 Map->name, Map->mapset);
00131 return -1;
00132 }
00133
00134
00135 if (0 >= dig__fread_port_C(buf, 5, &fp))
00136 return (-1);
00137 Version_Major = buf[0];
00138 Version_Minor = buf[1];
00139 Back_Major = buf[2];
00140 Back_Minor = buf[3];
00141 byte_order = buf[4];
00142
00143
00144
00145 if (Version_Major > 5 || Version_Minor > 0) {
00146 if (Back_Major > 5 || Back_Minor > 0) {
00147 G_fatal_error(_("Feature index format version %d.%d is not supported by this release."
00148 " Try to rebuild topology or upgrade GRASS."),
00149 Version_Major, Version_Minor);
00150 return (-1);
00151 }
00152 G_warning(_("Your GRASS version does not fully support feature index format %d.%d of the vector."
00153 " Consider to rebuild topology or upgrade GRASS."),
00154 Version_Major, Version_Minor);
00155 }
00156
00157 dig_init_portable(&port, byte_order);
00158 dig_set_cur_port(&port);
00159
00160
00161
00162 if (0 >= dig__fread_port_L(&length, 1, &fp))
00163 return (-1);
00164 G_debug(3, " header size %ld", length);
00165
00166 fseek(fp.file, length, SEEK_SET);
00167
00168
00169 if (0 >= dig__fread_port_I(&(Map->fInfo.ogr.offset_num), 1, &fp))
00170 return (-1);
00171
00172
00173 Map->fInfo.ogr.offset =
00174 (int *)G_malloc(Map->fInfo.ogr.offset_num * sizeof(int));
00175 Map->fInfo.ogr.offset_alloc = Map->fInfo.ogr.offset_num;
00176
00177
00178 if (0 >= dig__fread_port_I(Map->fInfo.ogr.offset,
00179 Map->fInfo.ogr.offset_num, &fp))
00180 return (-1);
00181
00182 fclose(fp.file);
00183
00184 G_debug(3, "%d records read from fidx", Map->fInfo.ogr.offset_num);
00185
00186
00187 Map->fInfo.ogr.next_line = 1;
00188
00189 return 0;
00190 }
00191
00192 #endif