[GRASS-SVN] r39161 - in grass/trunk: include lib/gis lib/vector/Vlib vector/v.info

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Sep 12 19:18:18 EDT 2009


Author: martinl
Date: 2009-09-12 19:18:17 -0400 (Sat, 12 Sep 2009)
New Revision: 39161

Modified:
   grass/trunk/include/vector.h
   grass/trunk/lib/gis/parser.c
   grass/trunk/lib/vector/Vlib/close.c
   grass/trunk/lib/vector/Vlib/close_ogr.c
   grass/trunk/lib/vector/Vlib/header.c
   grass/trunk/lib/vector/Vlib/open.c
   grass/trunk/lib/vector/Vlib/open_nat.c
   grass/trunk/lib/vector/Vlib/open_ogr.c
   grass/trunk/vector/v.info/main.c
Log:
initial steps towards direct OGR support (map/layer <-> OGR dsn/layer)


Modified: grass/trunk/include/vector.h
===================================================================
--- grass/trunk/include/vector.h	2009-09-12 22:31:00 UTC (rev 39160)
+++ grass/trunk/include/vector.h	2009-09-12 23:18:17 UTC (rev 39161)
@@ -212,7 +212,9 @@
 int Vect_legal_filename(const char *);
 int Vect_set_open_level(int);
 int Vect_open_old(struct Map_info *, const char *, const char *);
+int Vect_open_old2(struct Map_info *, const char *, const char *, const char *);
 int Vect_open_old_head(struct Map_info *, const char *, const char *);
+int Vect_open_old_head2(struct Map_info *, const char *, const char *, const char *);
 int Vect_open_new(struct Map_info *, const char *, int);
 int Vect_open_update(struct Map_info *, const char *, const char *);
 int Vect_open_update_head(struct Map_info *, const char *, const char *);

Modified: grass/trunk/lib/gis/parser.c
===================================================================
--- grass/trunk/lib/gis/parser.c	2009-09-12 22:31:00 UTC (rev 39160)
+++ grass/trunk/lib/gis/parser.c	2009-09-12 23:18:17 UTC (rev 39161)
@@ -626,10 +626,10 @@
 	break;
     case G_OPT_V_FIELD:
 	Opt->key = "layer";
-	Opt->type = TYPE_INTEGER;
+	Opt->type = TYPE_STRING;
 	Opt->required = NO;
 	Opt->answer = "1";
-	Opt->label = _("Layer number");
+	Opt->label = _("Layer number or name");
 	Opt->description =
 	    _("A single vector map can be connected to multiple database "
 	      "tables. This number determines which table to use.");

Modified: grass/trunk/lib/vector/Vlib/close.c
===================================================================
--- grass/trunk/lib/vector/Vlib/close.c	2009-09-12 22:31:00 UTC (rev 39160)
+++ grass/trunk/lib/vector/Vlib/close.c	2009-09-12 23:18:17 UTC (rev 39161)
@@ -51,9 +51,9 @@
 
 
 /*!
-   \brief Close vector data file
+   \brief Close vector map
 
-   \param Map vector map to be closed
+   \param Map pointer to Map_info
 
    \return 0 on success
    \return non-zero on error

Modified: grass/trunk/lib/vector/Vlib/close_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/close_ogr.c	2009-09-12 22:31:00 UTC (rev 39160)
+++ grass/trunk/lib/vector/Vlib/close_ogr.c	2009-09-12 23:18:17 UTC (rev 39161)
@@ -23,9 +23,9 @@
 #include <ogr_api.h>
 
 /*!
-  \brief Close OGR layer
+  \brief Close vector map (OGR dsn & layer)
 
-  \param Map vector map
+  \param Map pointer to Map_info
 
   \return 0 on success
   \return non-zero on error

Modified: grass/trunk/lib/vector/Vlib/header.c
===================================================================
--- grass/trunk/lib/vector/Vlib/header.c	2009-09-12 22:31:00 UTC (rev 39160)
+++ grass/trunk/lib/vector/Vlib/header.c	2009-09-12 23:18:17 UTC (rev 39161)
@@ -213,20 +213,27 @@
 }
 
 /*!
-   \brief Get full map name
-
-   Allocated string should be freed by G_free().
-   \param Map vector map
-
-   \return poiter to map name (name at mapset)
+  \brief Get full vector map name (i.e., "name at mapset")
+  
+  Allocated string should be freed by G_free().
+  
+  \param Map pointer to Map_info structure
+  
+  \return pointer to map name (name at mapset)
  */
 const char *Vect_get_full_name(const struct Map_info *Map)
 {
     char *ptr;
 
     ptr = (char *)G_malloc(strlen(Map->name) + strlen(Map->mapset) + 2);
-    sprintf(ptr, "%s@%s", Map->name, Map->mapset);
-    return (ptr);
+    if (strlen(Map->mapset) > 0) {
+	sprintf(ptr, "%s@%s", Map->name, Map->mapset);
+    }
+    else {
+	sprintf(ptr, "%s", Map->name);
+    }
+
+    return ptr;
 }
 
 /*!

Modified: grass/trunk/lib/vector/Vlib/open.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open.c	2009-09-12 22:31:00 UTC (rev 39160)
+++ grass/trunk/lib/vector/Vlib/open.c	2009-09-12 23:18:17 UTC (rev 39161)
@@ -1,7 +1,7 @@
 /*!
  * \file vector/Vlib/open.c
  *
- * \brief Vector library - Open vector map
+ * \brief Vector library - Open vector map (native or OGR format)
  *
  * Higher level functions for reading/writing/manipulating vectors.
  *
@@ -14,6 +14,7 @@
  * Higgins.
  * \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  */
+
 #include <grass/config.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -21,10 +22,14 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <grass/glocale.h>
+
 #include <grass/gis.h>
 #include <grass/vector.h>
+#include <grass/glocale.h>
 
+/*
+  \brief Number of available levels
+*/
 #define MAX_OPEN_LEVEL 2
 
 static int open_old_dummy()
@@ -68,7 +73,6 @@
     }
 }
 
-
 /*!
  * \brief Predetermine level at which a vector map will be opened for
  * reading.
@@ -80,12 +84,12 @@
  *
  * NOTE: This should only be used to set when you wish to force a
  * lower level open. If you require a higher level, then just check
- * the return to verify the level instead of forcing it.  This is
+ * the return to verify the level instead of forcing it. This is
  * because future releases will have higher levels which will be
  * downward compatible and which your programs should support by
  * default.
  *
- * \param level vector (topo) level
+ * \param level vector access level
  *
  * \return 0 on success
  * \return 1 on error
@@ -95,7 +99,7 @@
 {
     Open_level = level;
     if (Open_level < 1 || Open_level > MAX_OPEN_LEVEL) {
-	G_warning(_("Programmer requested unknown open level %d"),
+	G_warning(_("Programmer requested unknown access level %d"),
 		  Open_level);
 	Open_level = 0;
 	return 1;
@@ -105,22 +109,22 @@
 }
 
 /*! 
- * \brief Open old vector for reading.
+ * \brief Open existing vector map for reading (internal use only)
  *
  * In case of error, the functions respect fatal error settings.
  *
- * \param[out] Map vector map
+ * \param[out] Map pointer to Map_info structure
  * \param name name of vector map to open
- * \param mapset mapset name
- * \param update open for update
- * \param head_only read only header info from 'head', 'dbln', 'topo', 'cidx' is not opened. The header may be opened on level 2 only. 
+ * \param mapset mapset name ("" for search path)
+ * \param update non-zero to open for update otherwise read-only mode
+ * \param head_only read only header info from 'head', 'dbln', 'topo',
+ * 'cidx' is not opened. The header may be opened on level 2 only.
  *
  * \return level of openness (1, 2)
  * \return -1 in error
  */
-int
-Vect__open_old(struct Map_info *Map, const char *name, const char *mapset,
-	       int update, int head_only)
+int Vect__open_old(struct Map_info *Map, const char *name, const char *mapset, const char *layer,
+		   int update, int head_only)
 {
     char buf[GNAME_MAX + 10], buf2[GMAPSET_MAX + 10], xname[GNAME_MAX],
 	xmapset[GMAPSET_MAX], errmsg[2000];
@@ -149,17 +153,18 @@
     /* initialize Map->plus */
     dig_init_plus(&(Map->plus));
 
+    /* check OGR mapset */
     ogr_mapset = 0;
     if (G_name_is_fully_qualified(name, xname, xmapset)) {
-	if (strcmp(xmapset, "OGR") == 0) {
+	if (strcasecmp(xmapset, "ogr") == 0) {
 	    /* unique OGR mapset detected */
 	    G_debug(1, "OGR mapset detected");
 	    ogr_mapset = 1;
-	    Map->fInfo.ogr.dsn = xname;
-	    Map->fInfo.ogr.layer_name = NULL; /* no layer to be open */
+	    Map->fInfo.ogr.dsn = G_store(xname);
+	    Map->fInfo.ogr.layer_name = G_store(layer); /* no layer to be open */
 	}
 	else {
-	    sprintf(buf, "%s/%s", GV_DIRECTORY, xname);
+	    sprintf(buf,  "%s/%s", GV_DIRECTORY, xname);
 	    sprintf(buf2, "%s@%s", GV_COOR_ELEMENT, xmapset);
 	}
 	Map->name = G_store(xname);
@@ -167,7 +172,7 @@
     }
     else {
 	sprintf(buf, "%s/%s", GV_DIRECTORY, name);
-	sprintf(buf2, "%s", GV_COOR_ELEMENT);
+	sprintf(buf2, "%s",   GV_COOR_ELEMENT);
 	Map->name = G_store(name);
 
 	if (mapset)
@@ -176,15 +181,24 @@
 	    Map->mapset = G_store("");
     }
 
-    fmapset = G_find_vector2(Map->name, Map->mapset);
-    if (fmapset == NULL) {
-	sprintf(errmsg, _("Vector map <%s> not found"),
-		Vect_get_full_name(Map));
-	fatal_error(ferror, errmsg);
-	return -1;
+    if (!ogr_mapset) {
+	/* try to find vector map (not for OGR mapset) */
+	fmapset = G_find_vector2(Map->name, Map->mapset);
+	if (fmapset == NULL) {
+	    sprintf(errmsg, _("Vector map <%s> not found"),
+		    Vect_get_full_name(Map));
+	    fatal_error(ferror, errmsg);
+	    return -1;
+	}
+	Map->mapset = G_store(fmapset);
     }
-    Map->mapset = G_store(fmapset);
-
+    else { /* OGR mapset */
+	if (update) {
+	    sprintf(errmsg, _("OGR layer cannot be opened for update"));
+	    fatal_error(ferror, errmsg);
+	    return -1;
+	}
+    }
     Map->location = G_store(G_location());
     Map->gisdbase = G_store(G_gisdbase());
     
@@ -225,34 +239,37 @@
     }
     Map->format = format;
 
+    /* projection info */
     Vect_set_proj(Map, G_projection());
     Vect_set_zone(Map, G_zone());
     
-    /* Read vector head */
+    /* read vector head (ignored for OGR mapset) */
     if (!ogr_mapset && Vect__read_head(Map) != 0) {
 	sprintf(errmsg,
 		_("Unable to open vector map <%s> on level %d. "
 		  "Try to rebuild vector topology by v.build."),
 		Vect_get_full_name(Map), level_request);
-	G_warning(_("Unable to read head file of vector <%s>"),
+	G_warning(_("Unable to read header file of vector map <%s>"),
 		  Vect_get_full_name(Map));
     }
-
+    
     /* zone not set */
     if (Vect_get_zone(Map) == -1)
 	Vect_set_zone(Map, G_zone());
-
+    
     G_debug(1, "Level request = %d", level_request);
 
-    /* There are only 2 possible open levels, 1 and 2. Try first to open 'support' files
-     * (topo,sidx,cidx), these files are the same for all formats.
-     * If it is not possible and requested level is 2, return error,
-     * otherwise call Open_old_array[format][1], to open remaining files/sources (level 1)
+    /* There are only 2 possible open levels, 1 and 2. Try first to
+     * open 'support' files (topo,sidx,cidx), these files are the same
+     * for all formats.  If it is not possible and requested level is
+     * 2, return error, otherwise call Open_old_array[format][1], to
+     * open remaining files/sources (level 1)
      */
 
-    /* Try to open support files if level was not requested or requested level is 2 (format independent) */
+    /* Try to open support files if level was not requested or
+     * requested level is 2 (format independent) */
     if (level_request == 0 || level_request == 2) {
-	level = 2;		/* We expect success */
+	level = 2;		/* we expect success */
 	/* open topo */
 	ret = Vect_open_topo(Map, head_only);
 	if (ret == 1) {		/* topo file is not available */
@@ -321,12 +338,12 @@
 	}
     }
     else {
-	level = 1;		/* I.e. requested level is 1 */
+	level = 1;		/* i.e. requested level is 1 */
     }
 
-    /* Open level 1 files / sources (format specific) */
-    if (!head_only) {		/* No need to open coordinates */
-	if (0 != (*Open_old_array[format][1]) (Map, update)) {	/* Cannot open */
+    /* open level 1 files / sources (format specific) */
+    if (!head_only || ogr_mapset) {		/* no need to open coordinates */
+	if (0 != (*Open_old_array[format][1]) (Map, update)) {	/* cannot open */
 	    if (level == 2) {	/* support files opened */
 		dig_free_plus(&(Map->plus));
 		dig_spidx_free(&(Map->plus));
@@ -344,7 +361,7 @@
 	Map->head.with_z = Map->plus.with_z;	/* take dimension from topo */
     }
 
-    /* Set status */
+    /* set status */
     Map->open = VECT_OPEN_CODE;
     Map->level = level;
     Map->head_only = head_only;
@@ -372,17 +389,18 @@
 	Map->plus.built = GV_BUILD_NONE;
     }
     else {			/* level 2, with topology */
-	Map->plus.built = GV_BUILD_ALL;	/* Highest level of topology for level 2 */
+	Map->plus.built = GV_BUILD_ALL;	/* highest level of topology for level 2 */
     }
 
     Map->plus.do_uplist = 0;
 
+    /* read db links */
     Map->dblnk = Vect_new_dblinks_struct();
-    Vect_read_dblinks(Map);
+    if (!ogr_mapset)
+	Vect_read_dblinks(Map);
 
-    /* Open history file */
+    /* open history file */
     sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
-
     if (update) {		/* native only */
 	Map->hist_fp = G_fopen_modify(buf, GV_HIST_ELEMENT);
 	if (Map->hist_fp == NULL) {
@@ -408,11 +426,11 @@
 	}
     }
 
-    if (!head_only) {		/* Cannot rewind if not fully opened */
+    if (!head_only) {		/* cannot rewind if not fully opened */
 	Vect_rewind(Map);
     }
-
-    /* Delete support files if native format was opened for update (not head_only) */
+    
+    /* delete support files if native format was opened for update (not head_only) */
     if (update && !head_only) {
 	char file_path[2000];
 	struct stat info;
@@ -440,8 +458,8 @@
  *
  * In case of error, the functions respect fatal error settings.
  *
- * \param[out] Map vector map
- * \param name name of vector map
+ * \param[out] Map pointer to Map_info structure
+ * \param name name of vector map to open
  * \param mapset mapset name
  *
  * \return level of openness [1, 2, (3)]
@@ -449,28 +467,32 @@
  */
 int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
 {
-    return (Vect__open_old(Map, name, mapset, 0, 0));
+    return (Vect__open_old(Map, name, mapset, NULL, 0, 0));
 }
 
+int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
+{
+    return (Vect__open_old(Map, name, mapset, layer, 0, 0));
+}
+
 /*!
  * \brief Open existing vector for reading/writing
  *
  * In case of error, the functions respect fatal error settings.
  *
- * \param[out] Map vector map
+ * \param[out] Map pointer to Map_info structure
  * \param name name of vector map to update
  * \param mapset mapset name
  *
  * \return level of openness [1, 2, (3)]
  * \return -1 on error
  */
-int
-Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
+int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
 {
     int ret;
-
-    ret = Vect__open_old(Map, name, mapset, 1, 0);
-
+    
+    ret = Vect__open_old(Map, name, mapset, NULL, 1, 0);
+    
     if (ret > 0) {
 	Map->plus.do_uplist = 1;
 
@@ -482,7 +504,6 @@
 	Map->plus.alloc_upnodes = 0;
 
 	/* read spatial index */
-
 	/* Build spatial index from topo */
 	/* Vect_build_sidx_from_topo(Map); */
     }
@@ -497,36 +518,42 @@
  * In case of error, the functions respect fatal error settings.
  * 
  * \param[out] Map pointer to Map_info structure
- * \param name name of vector map to read
- * \param mapset mapset name
+ * \param name name of vector map to read (dsn for OGR)
+ * \param mapset mapset name ("" for search path)
+ * \param layer layer name (only for OGR)
  *
  * \return level of openness [1, 2, (3)]
  * \return -1 on error
  */
 int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
 {
-    return (Vect__open_old(Map, name, mapset, 0, 1));
+    return (Vect__open_old(Map, name, mapset, NULL, 0, 1));
 }
 
+int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
+{
+    return (Vect__open_old(Map, name, mapset, layer, 0, 1));
+}
+
 /*!
- * \brief Open old vector head for updating (mostly for database link updates)
+ * \brief Open header file of existing vector map for updating (mostly
+ * for database link updates)
  *
  * In case of error, the functions respect fatal error settings.
  *
- * \param[out] Map vector map
+ * \param[out] Map pointer to Map_info structure
  * \param name name of vector map to update
  * \param mapset mapset name
  *
  * \return level of openness [1, 2, (3)]
  * \return -1 on error
  */
-int
-Vect_open_update_head(struct Map_info *Map, const char *name,
-		      const char *mapset)
+int Vect_open_update_head(struct Map_info *Map, const char *name,
+			  const char *mapset)
 {
     int ret;
 
-    ret = Vect__open_old(Map, name, mapset, 1, 1);
+    ret = Vect__open_old(Map, name, mapset, NULL, 1, 1);
 
     if (ret > 0) {		/* Probably not important */
 	Map->plus.do_uplist = 1;
@@ -543,7 +570,7 @@
 }
 
 /*!
- * \brief Open new vector for reading/writing
+ * \brief Create new vector map for reading/writing
  *
  * \param[in,out] Map pointer to Map_info structure
  * \param name name of vector map
@@ -559,8 +586,11 @@
     char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
 
     G_debug(2, "Vect_open_new(): name = %s", name);
+    
+    /* init header */
+    Vect__init_head(Map);
 
-    Vect__init_head(Map);
+    /* error handling */
     ferror = Vect_get_fatal_error();
     Vect_set_fatal_error(GV_FATAL_EXIT);
 
@@ -611,7 +641,7 @@
     sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
     Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
     if (Map->hist_fp == NULL) {
-	sprintf(errmsg, _("Unable to open history file for vector map <%s>"),
+	sprintf(errmsg, _("Unable to open history file of vector map <%s>"),
 		Vect_get_full_name(Map));
 	fatal_error(ferror, errmsg);
 	return (-1);
@@ -694,13 +724,17 @@
 }
 
 /*!
- * \brief Gets maptype (native, shape, postgis)
+ * \brief Gets vector map format (as string)
  *
  * Note: string is allocated by G_store(). Free allocated memory with
  * G_free().
  *
- * \param Map vector map
+ * Currently are implemeted:
+ *  - Native format (native)
+ *  - OGR format    (ogr)
  *
+ * \param Map pointer to Map_info structure
+ *
  * \return maptype string on success
  * \return error message on error
  */
@@ -725,7 +759,7 @@
 
 
 /*!
- * \brief Open topo file
+ * \brief Open topology file ('topo')
  *
  * \param[in,out] Map pointer to Map_info structure
  * \param head_only open only head

Modified: grass/trunk/lib/vector/Vlib/open_nat.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_nat.c	2009-09-12 22:31:00 UTC (rev 39160)
+++ grass/trunk/lib/vector/Vlib/open_nat.c	2009-09-12 23:18:17 UTC (rev 39161)
@@ -1,17 +1,17 @@
 /*!
-   \file open_nat.c
-
-   \brief Vector library - open vector map (native format)
-
-   Higher level functions for reading/writing/manipulating vectors.
-
-   (C) 2001-2009 by the GRASS Development Team
-
-   This program is free software under the GNU General Public License
-   (>=v2).  Read the file COPYING that comes with GRASS for details.
-
-   \author Original author CERL, probably Dave Gerdes or Mike Higgins.
-   \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
+  \file vector/Vlib/open_nat.c
+  
+  \brief Vector library - open vector map (native format) - level 1
+  
+  Higher level functions for reading/writing/manipulating vectors.
+  
+  (C) 2001-2009 by the GRASS Development Team
+  
+  This program is free software under the GNU General Public License
+  (>=v2).  Read the file COPYING that comes with GRASS for details.
+  
+  \author Original author CERL, probably Dave Gerdes or Mike Higgins.
+  \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  */
 
 #include <grass/config.h>
@@ -27,15 +27,15 @@
 static int check_coor(struct Map_info *Map);
 
 /*!
-   \brief Open existing vector map
+  \brief Open existing vector map (level 1)
+  
+  Map->name and Map->mapset must be set before.
+  
+  \param Map poiter to Map_info structure
+  \param update non-zero for write mode, otherwise read-only
 
-   Map->name and Map->mapset must be set before.
-
-   \param Map poiter to vector map
-   \param update non-zero for write mode, otherwise read-only
-
-   \return 0 success
-   \return -1 error
+  \return 0 success
+  \return -1 error
 */
 int V1_open_old_nat(struct Map_info *Map, int update)
 {
@@ -54,7 +54,8 @@
 	    G_fopen_old(buf, GV_COOR_ELEMENT, Map->mapset);
 
     if (Map->dig_fp.file == NULL) {
-        G_warning(_("Unable to open coor file for vector map <%s>"), Map->name);
+        G_warning(_("Unable to open coor file for vector map <%s>"),
+		  Vect_get_full_name(Map));
         return -1;
     }
 
@@ -78,10 +79,10 @@
 }
 
 /*!
-   \brief Open/Create new vector map (level 1)
+   \brief Create new vector map (level 1)
 
    \param[out] Map pointer to Map_info structure
-   \param name map name
+   \param name vector map name to be created
    \param with_z 2D or 3D (unused?)
 
    \return 0 success

Modified: grass/trunk/lib/vector/Vlib/open_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_ogr.c	2009-09-12 22:31:00 UTC (rev 39160)
+++ grass/trunk/lib/vector/Vlib/open_ogr.c	2009-09-12 23:18:17 UTC (rev 39161)
@@ -1,17 +1,20 @@
 /*!
-   \file open_ogr.c
+   \file vector/Vlib/open_ogr.c
 
-   \brief Vector library - open vector map (OGR format)
+   \brief Vector library - Open OGR layer as vector map layer
 
    Higher level functions for reading/writing/manipulating vectors.
 
+   \todo Implement V1_open_new_ogr()
+   
    (C) 2001-2009 by the GRASS Development Team
 
    This program is free software under the GNU General Public License
-   (>=v2).  Read the file COPYING that comes with GRASS for details.
+   (>=v2). Read the file COPYING that comes with GRASS for details.
 
    \author Original author CERL, probably Dave Gerdes or Mike Higgins.
    \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
+   \author Update to GRASS 7.0 Martin Landa <landa.martin gmail.com> (2009)
  */
 
 #include <grass/config.h>
@@ -28,14 +31,15 @@
 #include <ogr_api.h>
 
 /*!
-   \brief Open existing vector map
+   \brief Open existing OGR layer (level 1 - without feature index file)
 
-   Map->name and Map->mapset must be set before.
+   Map->name, Map->mapset, Map->fInfo.ogr.dsn and
+   Map->fInfo.ogr.layer_name must be set before.
 
-   \param[out] Map pointer to vector map
+   \param[in,out] Map pointer to Map_info structure
    \param update non-zero for write mode, otherwise read-only
    (write mode is currently not supported)
-
+   
    \return 0 success
    \return -1 error
 */
@@ -43,27 +47,39 @@
 {
     int i, layer, nLayers;
     OGRDataSourceH Ogr_ds;
-    OGRLayerH Ogr_layer = NULL;
+    OGRLayerH Ogr_layer;
     OGRFeatureDefnH Ogr_featuredefn;
 
+    Ogr_layer = NULL;
+    
     if (update) {
-	G_warning(_("OGR format cannot be updated"));
+	G_warning(_("Write mode is not supported for OGR format"));
 	return -1;
     }
 
+    if (!Map->fInfo.ogr.dsn) {
+	G_warning(_("OGR datasource not defined"));
+	return -1;
+    }
+    
+    if (!Map->fInfo.ogr.layer_name) {
+	G_warning(_("OGR layer not defined"));
+	return -1;
+    }
+    
     G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", Map->fInfo.ogr.dsn,
 	    Map->fInfo.ogr.layer_name);
 
     OGRRegisterAll();
 
-    /*Data source handle */
+    /* open data source handle */
     Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
     if (Ogr_ds == NULL)
 	G_fatal_error(_("Unable to open OGR data source '%s'"),
 		      Map->fInfo.ogr.dsn);
     Map->fInfo.ogr.ds = Ogr_ds;
 
-    /* Layer number */
+    /* get layer number */
     layer = -1;
     nLayers = OGR_DS_GetLayerCount(Ogr_ds);
     G_debug(2, "%d layers found in data source", nLayers);
@@ -71,40 +87,39 @@
     for (i = 0; i < nLayers; i++) {
 	Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
 	Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
-	if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name)
-	    == 0) {
+	if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name) == 0) {
 	    layer = i;
 	    break;
 	}
     }
     if (layer == -1) {
 	OGR_DS_Destroy(Ogr_ds);
-	G_fatal_error(_("Unable to open layer <%s>"),
+	G_fatal_error(_("Unable to open OGR layer <%s>"),
 		      Map->fInfo.ogr.layer_name);
     }
     G_debug(2, "OGR layer %d opened", layer);
 
     Map->fInfo.ogr.layer = Ogr_layer;
-
+    
     Map->fInfo.ogr.lines = NULL;
     Map->fInfo.ogr.lines_types = NULL;
     Map->fInfo.ogr.lines_alloc = 0;
     Map->fInfo.ogr.lines_num = 0;
     Map->fInfo.ogr.lines_next = 0;
-
+    
     Map->head.with_z = WITHOUT_Z;	/* TODO: 3D */
-
+    
     Map->fInfo.ogr.feature_cache = NULL;
     Map->fInfo.ogr.feature_cache_id = -1;	/* FID >= 0 */
-
+    
     return (0);
 }
 
 /*!
-   \brief Open OGR specific level 2 files (feature index)
+   \brief Open existing OGR layer (level 2 - feature index)
 
-   \param[out] Map pointer to Map_info structure
-
+   \param[in,out] Map pointer to Map_info structure
+   
    \return 0 success
    \return -1 error
 */
@@ -118,7 +133,7 @@
     int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
 
     G_debug(3, "V2_open_old_ogr()");
-
+    
     sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
     dig_file_init(&fp);
     fp.file = G_fopen_old(elem, GV_FIDX_ELEMENT, Map->mapset);
@@ -136,8 +151,7 @@
     Back_Major = buf[2];
     Back_Minor = buf[3];
     byte_order = buf[4];
-
-
+    
     /* check version numbers */
     if (Version_Major > 5 || Version_Minor > 0) {
 	if (Back_Major > 5 || Back_Minor > 0) {
@@ -180,7 +194,6 @@
 
     G_debug(3, "%d records read from fidx", Map->fInfo.ogr.offset_num);
 
-
     Map->fInfo.ogr.next_line = 1;
 
     return 0;

Modified: grass/trunk/vector/v.info/main.c
===================================================================
--- grass/trunk/vector/v.info/main.c	2009-09-12 22:31:00 UTC (rev 39160)
+++ grass/trunk/vector/v.info/main.c	2009-09-12 23:18:17 UTC (rev 39161)
@@ -3,7 +3,7 @@
  * MODULE:       v.info
  * 
  * AUTHOR(S):    CERL, updated to 5.7 by Markus Neteler
- *               Update to 7.0 by Martin Landa
+ *               Update to 7.0 by Martin Landa <landa.martin gmail.com> (2009)
  *               Support for level 1 by Markus Metz (2009)
  *               
  * PURPOSE:      Print vector map info
@@ -47,18 +47,20 @@
     if (level1_flag) {
 	Vect_set_open_level(1); /* no topology */
 	if (topo_flag)
-	    G_warning(_("Vector map requested on level1 (flag -t ignored)")); 
+	    G_warning(_("Vector map requested on level 1 (flag -t ignored)")); 
 	topo_flag = 0;
     }
-    else
-	Vect_set_open_level(2); /* topology requested */
     
     if (level1_flag) {
-	Vect_open_old(&Map, input_opt, "");      /* level 1 */
+	Vect_open_old2(&Map, input_opt, "", field_opt);      /* level 1 */
 	level_one_info(&Map);
     }
-    else
-	Vect_open_old_head(&Map, input_opt, ""); /* level 2 */
+    else {
+	 /* level 2 */
+	if (Vect_open_old_head2(&Map, input_opt, "", field_opt) < 2)
+	    G_fatal_error(_("Unable to open vector map <%s> on level 2"),
+			  Vect_get_full_name(&Map));
+    }
 
     if (hist_flag) {
 	char buf[1001];
@@ -92,4 +94,3 @@
 
     return (EXIT_SUCCESS);
 }
-



More information about the grass-commit mailing list