[GRASS-SVN] r44722 - in grass/trunk: include include/vect lib/vector/Vlib

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Dec 25 15:43:32 EST 2010


Author: martinl
Date: 2010-12-25 12:43:32 -0800 (Sat, 25 Dec 2010)
New Revision: 44722

Modified:
   grass/trunk/include/vect/dig_structs.h
   grass/trunk/include/vector.h
   grass/trunk/lib/vector/Vlib/build.c
   grass/trunk/lib/vector/Vlib/build_ogr.c
   grass/trunk/lib/vector/Vlib/header.c
   grass/trunk/lib/vector/Vlib/open.c
   grass/trunk/lib/vector/Vlib/open_ogr.c
   grass/trunk/lib/vector/Vlib/write_ogr.c
Log:
vlib: introduce Vect_get_ogr_geometry_type() and V2_open_new_ogr()


Modified: grass/trunk/include/vect/dig_structs.h
===================================================================
--- grass/trunk/include/vect/dig_structs.h	2010-12-25 16:25:52 UTC (rev 44721)
+++ grass/trunk/include/vect/dig_structs.h	2010-12-25 20:43:32 UTC (rev 44722)
@@ -179,7 +179,10 @@
     void *ds;
     void *layer;
 #endif
-
+    
+    char **dsn_options;
+    char **layer_options;
+    
     /* Level 1 (used by V*_read_next_line_ogr) */
     struct line_pnts **lines;	/* points cache */
     int *lines_types;           /* list of line types */

Modified: grass/trunk/include/vector.h
===================================================================
--- grass/trunk/include/vector.h	2010-12-25 16:25:52 UTC (rev 44721)
+++ grass/trunk/include/vector.h	2010-12-25 20:43:32 UTC (rev 44722)
@@ -164,6 +164,7 @@
 const char *Vect_get_ogr_dsn_name(const struct Map_info *);
 const char *Vect_get_ogr_layer_name(const struct Map_info *);
 const char *Vect_get_ogr_format_info(const struct Map_info *);
+const char *Vect_get_ogr_geometry_type(const struct Map_info *);
 int Vect_is_3d(const struct Map_info *);
 int Vect_set_organization(struct Map_info *, const char *);
 const char *Vect_get_organization(const struct Map_info *);
@@ -456,6 +457,7 @@
 int V2_open_old_ogr(struct Map_info *);
 int V1_open_new_nat(struct Map_info *, const char *, int);
 int V1_open_new_ogr(struct Map_info *, const char *, int);
+int V2_open_new_ogr(struct Map_info *, int);
 int V1_rewind_nat(struct Map_info *);
 int V1_rewind_ogr(struct Map_info *);
 int V2_rewind_nat(struct Map_info *);

Modified: grass/trunk/lib/vector/Vlib/build.c
===================================================================
--- grass/trunk/lib/vector/Vlib/build.c	2010-12-25 16:25:52 UTC (rev 44721)
+++ grass/trunk/lib/vector/Vlib/build.c	2010-12-25 20:43:32 UTC (rev 44722)
@@ -124,10 +124,6 @@
 
     plus = &(Map->plus);
     if (build > GV_BUILD_NONE) {
-	if (Vect_maptype(Map) != GV_FORMAT_NATIVE)
-	    G_message(_("Using external data format '%s'"),
-		      Vect_get_ogr_format_info(Map));
-	
 	G_message(_("Building topology for vector map <%s>..."),
 		  Vect_get_full_name(Map));
     }

Modified: grass/trunk/lib/vector/Vlib/build_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/build_ogr.c	2010-12-25 16:25:52 UTC (rev 44721)
+++ grass/trunk/lib/vector/Vlib/build_ogr.c	2010-12-25 20:43:32 UTC (rev 44722)
@@ -15,7 +15,7 @@
    (C) 2001-2010 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 Radim Blazek, Piero Cavalieri
    \author Various updates for GRASS 7 by Martin Landa <landa.martin gmail.com>
@@ -371,6 +371,10 @@
 	return 0;
     }
 
+    G_message(_("Using external data format '%s' (feature type '%s')"),
+	      Vect_get_ogr_format_info(Map),
+	      Vect_get_ogr_geometry_type(Map));
+    
     /* initialize data structures */
     init_parts(&parts);
 

Modified: grass/trunk/lib/vector/Vlib/header.c
===================================================================
--- grass/trunk/lib/vector/Vlib/header.c	2010-12-25 16:25:52 UTC (rev 44721)
+++ grass/trunk/lib/vector/Vlib/header.c	2010-12-25 20:43:32 UTC (rev 44722)
@@ -5,13 +5,14 @@
 
    Higher level functions for reading/writing/manipulating vectors.
 
-   (C) 2001-2009 by the GRASS Development Team
+   (C) 2001-2010 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 (OGR support) by Martin Landa <landa.martin gmail.com>
  */
 
 #include <grass/config.h>
@@ -24,12 +25,12 @@
 static int lookup(const char *file, const char *key, char *value, size_t len);
 
 /*!
-   \brief Print vector map header
+   \brief Print vector map header to stdout
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
    \return 0 on success
- */
+*/
 int Vect_print_header(const struct Map_info *Map)
 {
     fprintf(stdout, "\nSelected information from dig header\n");
@@ -41,28 +42,26 @@
     return 0;
 }
 
-
 /*!
    \brief Read vector map header from map head file
 
-   \param Map vector map
+   \param Map pointrt to Map_info structure
 
    \return 0
- */
+*/
 int Vect_read_header(struct Map_info *Map)
 {
     Vect__read_head(Map);
     return 0;
 }
 
-
 /*!
    \brief Write vector map header to map head file
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
    \return 0
- */
+*/
 int Vect_write_header(const struct Map_info *Map)
 {
     /* do some sanity checking here */
@@ -70,11 +69,10 @@
     return 0;
 }
 
-
 /*! 
-   \brief Writes head information to text file.
+   \brief Writes head information to text file (GV_HEAD_ELEMENT)
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
    \return 0 on success
    \return -1 on error
@@ -90,7 +88,7 @@
     if (head_fp == NULL) {
 	G_warning(_("Unable to open header file of vector <%s>"),
 		  Vect_get_full_name(Map));
-	return (-1);
+	return -1;
     }
 
     fprintf(head_fp, "ORGANIZATION: %s\n", Vect_get_organization(Map));
@@ -108,7 +106,7 @@
 }
 
 /*!
-   \brief Reads head information from text file (GV_HEAD_ELEMENT).
+   \brief Reads head information from text file (GV_HEAD_ELEMENT) - for internal use only
 
    \param Map pointer to Map_info structure
 
@@ -138,7 +136,7 @@
     if (head_fp == NULL) {
 	G_warning(_("Unable to open header file of vector <%s>"),
 		  Vect_get_full_name(Map));
-	return (-1);
+	return -1;
     }
 
     while (G_getl2(buff, 2000, head_fp)) {
@@ -180,39 +178,40 @@
 	else if (strncmp(buff, "MAP THRESH:", sizeof(char) * 11) == 0)
 	    Vect_set_thresh(Map, atof(ptr));
 	else
-	    G_warning(_("Unknown keyword %s in vector head"), buff);
+	    G_warning(_("Unknown keyword '%s' in vector head"), buff);
     }
 
     fclose(head_fp);
-    return (0);
+
+    return 0;
 }
 
 /*!
-   \brief Get map name
+   \brief Get name of vector map
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
-   \return poiter to map name
- */
+   \return string containing name
+*/
 const char *Vect_get_name(const struct Map_info *Map)
 {
-    return (Map->name);
+    return Map->name;
 }
 
 /*!
-   \brief Get mapset name
+   \brief Get name of mapset where vector map lives
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
-   \return poiter to mapset name
- */
+   \return string containing mapset name
+*/
 const char *Vect_get_mapset(const struct Map_info *Map)
 {
-    return (Map->mapset);
+    return Map->mapset;
 }
 
 /*!
-  \brief Get full vector map name
+  \brief Get fully qualified name of vector map
   
   - for GV_FORMAT_NATIVE and GV_FORMAT_OGR returns "map at mapset"
   - for GV_FORMAT_OGR_DIRECT returns "layer at datasourse"
@@ -221,7 +220,7 @@
   
   \param Map pointer to Map_info structure
   
-  \return pointer to map name (name at mapset)
+  \return allocated string "name at mapset"
  */
 const char *Vect_get_full_name(const struct Map_info *Map)
 {
@@ -254,11 +253,11 @@
 
    \param Map pointer to Map_info structure
 
-   \return poiter to string with OGR datasource name
+   \return string containing OGR datasource name
  */
 const char *Vect_get_ogr_dsn_name(const struct Map_info *Map)
 {
-    return (Map->fInfo.ogr.dsn);
+    return Map->fInfo.ogr.dsn;
 }
 
 /*!
@@ -266,20 +265,22 @@
 
    \param Map pointer to Map_info structure
 
-   \return poiter to string with OGR layer name
+   \return string containing OGR layer name
  */
 const char *Vect_get_ogr_layer_name(const struct Map_info *Map)
 {
-    return (Map->fInfo.ogr.layer_name);
+    return Map->fInfo.ogr.layer_name;
 }
 
 /*!
   \brief Get OGR format info (relevant only for OGR format)
 
+  Allocated space should be freed by G_free().
+
   \param Map pointer to Map_info structure
   
-  \return poiter to string with OGR format info (allocated by G_store())
-  \return NULL on error
+  \return string containing name of OGR format (allocated by G_store())
+  \return NULL on error (or on missing OGR support)
 */
 const char *Vect_get_ogr_format_info(const struct Map_info *Map)
 {
@@ -291,28 +292,61 @@
 #endif
     return NULL;
 }
-   
+
 /*!
-   \brief Check if vector map is 3D (with z)
+  \brief Get OGR geometry type (relevant only for OGR format)
 
-   \param Map vector map
+  \param Map pointer to Map_info structure
 
-   \return 1 map is 3D
-   \return 0 map is not 3D
+  \return allocated string containing geometry type info
+  \return NULL on error (or on missing OGR support)
+*/
+const char *Vect_get_ogr_geometry_type(const struct Map_info *Map)
+{
+#ifdef HAVE_OGR
+    OGRwkbGeometryType Ogr_geom_type;
+    OGRFeatureDefnH    Ogr_feature_defn;
+    
+    if (!Map->fInfo.ogr.layer)
+	return NULL;
+
+    Ogr_feature_defn = OGR_L_GetLayerDefn(Map->fInfo.ogr.layer);
+    Ogr_geom_type = OGR_FD_GetGeomType(Ogr_feature_defn);
+    switch(Ogr_geom_type) {
+    case wkbPoint:
+	return G_store(_("point"));
+    case wkbLineString:
+	return G_store(_("linestring"));
+    case wkbPolygon:
+	return G_store(_("polygon"));
+    default:
+	return G_store(_("unknown"));
+    }
+#endif
+    return NULL;
+}
+
+/*!
+   \brief Check if vector map is 3D
+
+   \param Map pointer to Map_info structure
+
+   \return TRUE  vector map is 3D
+   \return FALSE vector map is not 3D
  */
 int Vect_is_3d(const struct Map_info *Map)
 {
-    return (Map->head.with_z);
+    return Map->head.with_z;
 }
 
 /*!
    \brief Set organization string in map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
    \param str organization name
 
    \return 0
- */
+*/
 int Vect_set_organization(struct Map_info *Map, const char *str)
 {
     G_free(Map->head.organization);
@@ -324,26 +358,26 @@
 /*!
    \brief Get organization string from map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
-   \return organization string
+   \return string containing organization name
  */
 const char *Vect_get_organization(const struct Map_info *Map)
 {
-    return (Map->head.organization);
+    return Map->head.organization;
 }
 
 /*!
-   \brief Set date of digitization string in map header
+   \brief Set date of digitization in map header
 
-   SUGGESTION: this should be coupled to DateTime functions to support
+   \todo This should be coupled to DateTime functions to support
    time series
 
-   \param Map vector map
-   \param str data string
+   \param Map pointer to Map_info structure
+   \param str date given as string
 
    \return 0
- */
+*/
 int Vect_set_date(struct Map_info *Map, const char *str)
 {
     G_free(Map->head.date);
@@ -353,13 +387,10 @@
 }
 
 /*!
-   \brief Get date of digitization string from map header
+   \brief Get date of digitization from map header
 
-   SUGGESTION: this should be coupled to DateTime functions to support
-   time series
+   \param Map pointer to Map_info structure
 
-   \param Map vector map
-
    \return date of digitization string
  */
 const char *Vect_get_date(const struct Map_info *Map)
@@ -368,13 +399,13 @@
 }
 
 /*!
-   \brief Set user name string who digitized the map in map header
+   \brief Set name of user who digitized the map in map header
 
-   \param Map vector map
-   \param str user name string
+   \param Map pointer to Map_info structure
+   \param str user name
 
-   \return 0 on success
- */
+   \return 0
+*/
 int Vect_set_person(struct Map_info *Map, const char *str)
 {
     G_free(Map->head.your_name);
@@ -386,22 +417,22 @@
 /*!
    \brief Get user name string who digitized the map from map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
-   \return user name string
+   \return string containing user name
  */
 const char *Vect_get_person(const struct Map_info *Map)
 {
-    return (Map->head.your_name);
+    return Map->head.your_name;
 }
 
 /*!
-   \brief Set map name string in map header
+   \brief Set map name in map header
 
-   \param Map vector map
-   \param str map name string
+   \param Map pointer to Map_info structure
+   \param str map name to be set
 
-   \return 0 on success
+   \return 0 
  */
 int Vect_set_map_name(struct Map_info *Map, const char *str)
 {
@@ -412,24 +443,24 @@
 }
 
 /*!
-   \brief Get map name string in map header
+   \brief Get map name from map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
-   \return map name string
- */
+   \return string containing map name
+*/
 const char *Vect_get_map_name(const struct Map_info *Map)
 {
-    return (Map->head.map_name);
+    return Map->head.map_name;
 }
 
 /*!
-   \brief Set date string when the source map was originally produced in map header
+   \brief Set date when the source map was originally produced in map header
 
-   \param Map vector map
-   \param str date when the source map was originally produced string
+   \param Map pointer to Map_info structure
+   \param str date given as a string
 
-   \return 0 on success
+   \return 0
  */
 int Vect_set_map_date(struct Map_info *Map, const char *str)
 {
@@ -440,24 +471,24 @@
 }
 
 /*!
-   \brief Get date string when the source map was originally produced in map header
+   \brief Get date when the source map was originally produced from map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
-   \return date when the source map was originally produced string
+   \return string containg a date
  */
 const char *Vect_get_map_date(const struct Map_info *Map)
 {
-    return (Map->head.source_date);
+    return Map->head.source_date;
 }
 
 /*!
    \brief Set map scale in map header
 
-   \param Map vector map
-   \param map scale
+   \param Map pointer to Map_info structure
+   \param scale map scale
 
-   \return 0 on success
+   \return 0
  */
 int Vect_set_scale(struct Map_info *Map, int scale)
 {
@@ -469,22 +500,22 @@
 /*!
    \brief Get map scale from map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
    \return map scale
  */
 int Vect_get_scale(const struct Map_info *Map)
 {
-    return ((int) Map->head.orig_scale);
+    return (int) Map->head.orig_scale;
 }
 
 /*!
    \brief Set comment or other info string in map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
    \param str comment or other info string
 
-   \return 0 on success
+   \return 0
  */
 int Vect_set_comment(struct Map_info *Map, const char *str)
 {
@@ -497,22 +528,22 @@
 /*!
    \brief Get comment or other info string from map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
    \return comment or other info string
  */
 const char *Vect_get_comment(const struct Map_info *Map)
 {
-    return (Map->head.line_3);
+    return Map->head.line_3;
 }
 
 /*!
    \brief Set projection zone in map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
    \param zone projection zone
 
-   \return 0 on success
+   \return 0
  */
 int Vect_set_zone(struct Map_info *Map, int zone)
 {
@@ -524,26 +555,28 @@
 /*!
    \brief Get projection zone from map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
    \return projection zone
  */
 int Vect_get_zone(const struct Map_info *Map)
 {
-    return (Map->head.plani_zone);
+    return Map->head.plani_zone;
 }
 
 /*!
    \brief Set projection in map header
 
-   \param Map vector map
+   Supported projections:
+    - PROJECTION_XY  0 - x,y (Raw imagery),
+    - PROJECTION_UTM 1 - UTM   Universal Transverse Mercator,
+    - PROJECTION_SP  2 - State Plane (in feet),
+    - PROJECTION_LL  3 - Latitude-Longitude
 
-   PROJECTION_XY  0 - x,y (Raw imagery),
-   PROJECTION_UTM 1 - UTM   Universal Transverse Mercator,
-   PROJECTION_SP  2 - State Plane (in feet),
-   PROJECTION_LL  3 - Latitude-Longitude
+   \param Map pointer to Map_info structure
+   \param proj projection code
    
-   \return 0 on success
+   \return 0
  */
 int Vect_set_proj(struct Map_info *Map, int proj)
 {
@@ -555,28 +588,27 @@
 /*!
    \brief Get projection from map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
    \return PROJECTION_XY  0 - x,y (Raw imagery),
    \return PROJECTION_UTM 1 - UTM   Universal Transverse Mercator,
    \return PROJECTION_SP  2 - State Plane (in feet),
    \return PROJECTION_LL  3 - Latitude-Longitude
- */
+*/
 int Vect_get_proj(const struct Map_info *Map)
 {
-    return (Map->proj);
+    return Map->proj;
 }
 
 /*!
-   \brief Query cartographic projection name of vector map
+   \brief Query cartographic projection name of pointer to Map_info structure
 
    Returns a pointer to a string which is a printable name for
-   projection code <em>proj</em> (as returned by
-   Vect_get_proj()).
+   projection code <em>proj</em> (as returned by Vect_get_proj()).
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
-   \return poiter to projection name
+   \return allocated string containing projection name
    \return NULL if <em>proj</em> is not a valid projection
  */
 
@@ -601,29 +633,29 @@
 /*!
    \brief Set threshold used for digitization in map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
    \param thresh threshold used for digitization
 
-   \return 0 on success
+   \return 0
  */
 int Vect_set_thresh(struct Map_info *Map, double thresh)
 {
     G_debug(1, "Vect_set_thresh(): thresh = %f", thresh);
     Map->head.digit_thresh = thresh;
-    return (0);
+    return 0;
 }
 
 /*!
    \brief Get threshold used for digitization from map header
 
-   \param Map vector map
+   \param Map pointer to Map_info structure
 
    \return threshold used for digitization
  */
 double Vect_get_thresh(const struct Map_info *Map)
 {
     G_debug(1, "Vect_get_thresh(): thresh = %f", Map->head.digit_thresh);
-    return (Map->head.digit_thresh);
+    return Map->head.digit_thresh;
 }
 
 
@@ -633,5 +665,5 @@
     char path[GPATH_MAX];
 
     G__file_name(path, "", file, "PERMANENT");
-    return G_lookup_key_value_from_file(path, key, value, (int)len) == 1;
+    return G_lookup_key_value_from_file(path, key, value, (int) len) == 1;
 }

Modified: grass/trunk/lib/vector/Vlib/open.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open.c	2010-12-25 16:25:52 UTC (rev 44721)
+++ grass/trunk/lib/vector/Vlib/open.c	2010-12-25 20:43:32 UTC (rev 44722)
@@ -723,6 +723,7 @@
 	struct Key_Value *key_val;
 	const char *p;
 
+	G_debug(2, " using OGR format");
 	Map->format = GV_FORMAT_OGR_DIRECT;
 	fp = G_fopen_old("", "OGR", G_mapset());
 	if (!fp) {
@@ -742,6 +743,7 @@
     }
     else {
 	/* native */
+	G_debug(2, " using native format");
 	Map->format = GV_FORMAT_NATIVE;
 
 	/* check if map already exists */

Modified: grass/trunk/lib/vector/Vlib/open_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_ogr.c	2010-12-25 16:25:52 UTC (rev 44721)
+++ grass/trunk/lib/vector/Vlib/open_ogr.c	2010-12-25 20:43:32 UTC (rev 44722)
@@ -202,11 +202,13 @@
 }
 
 /*!
-   \brief Create new OGR layer in given OGR datasource (level 1)
+   \brief Prepare OGR datasource for creating new OGR layer (level 1)
 
+   New OGR layer can be created by V2_open_new_ogr().
+   
    \param[out] Map pointer to Map_info structure
    \param name name of OGR layer to create
-   \param with_z 2D or 3D (unused?)
+   \param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
 
    \return 0 success
    \return -1 error 
@@ -217,11 +219,7 @@
     OGRDataSourceH  Ogr_ds;
     OGRLayerH       Ogr_layer;
     OGRFeatureDefnH Ogr_featuredefn;
-    
-    OGRSpatialReferenceH Ogr_spatial_ref;
 
-    struct Key_Value *projinfo, *projunits;
-    
     int            i, nlayers;
     char         **Ogr_layer_options;
      
@@ -249,7 +247,7 @@
     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), name) == 0) {	
 	    if (G_get_overwrite()) {
 		G_warning(_("OGR layer <%s> already exists and will be overwritten"),
 			  Map->fInfo.ogr.layer_name);
@@ -264,17 +262,73 @@
 		G_fatal_error(_("OGR layer <%s> already exists in datasource '%s'"),
 			      Map->fInfo.ogr.layer_name, Map->fInfo.ogr.dsn);
 	    }
+	    Map->fInfo.ogr.layer = NULL;
 	    break;
 	}
     }
     
-    /* create new OGR layer */
-    projinfo = G_get_projinfo();
+    return 0;
+}
+
+/*!
+   \brief Create new OGR layer in given OGR datasource (level 2)
+
+   V1_open_new_ogr() is required to be called before this function.
+
+   \param[in,out] Map pointer to Map_info structure
+   \param type feature type (GV_POINT, GV_LINE, ...)
+
+   \return 0 success
+   \return -1 error 
+*/
+int V2_open_new_ogr(struct Map_info *Map, int type)
+{
+    OGRLayerH            Ogr_layer;
+    OGRSpatialReferenceH Ogr_spatial_ref;
+    
+    struct Key_Value *projinfo, *projunits;
+    
+    OGRwkbGeometryType Ogr_geom_type;
+    char             **Ogr_layer_options;
+    
+    if (!Map->fInfo.ogr.driver_name ||
+	!Map->fInfo.ogr.layer_name ||
+	!Map->fInfo.ogr.ds)
+	return -1;
+    
+    /* get spatial reference */
+    projinfo  = G_get_projinfo();
     projunits = G_get_projunits();
     Ogr_spatial_ref = GPJ_grass_to_osr(projinfo, projunits);
-    /* Ogr_layer_options = CSLSetNameValue(Ogr_layer_options, "OVERWRITE", "YES"); */
-    Ogr_layer = OGR_DS_CreateLayer(Ogr_ds, Map->fInfo.ogr.layer_name,
-				   Ogr_spatial_ref, wkbPoint, Ogr_layer_options);
+
+    switch(type) {
+    case GV_POINT:
+	Ogr_geom_type = wkbPoint;
+	break;
+    case GV_LINE:
+	Ogr_geom_type = wkbLineString;
+	break;
+    case GV_AREA:
+	Ogr_geom_type = wkbPolygon;
+	break;
+    default:
+	G_warning(_("Unsupported geometry type (%d)"), type);
+	return -1;
+    }
+    
+    Ogr_layer_options = Map->fInfo.ogr.layer_options;
+    if (Vect_is_3d(Map)) {
+	if (strcmp(Map->fInfo.ogr.driver_name, "PostgreSQL") == 0) {
+	    Ogr_layer_options = CSLSetNameValue(Ogr_layer_options, "DIM", "3");
+	}
+    }
+    else {
+	if (strcmp(Map->fInfo.ogr.driver_name, "PostgreSQL") == 0) {
+	    Ogr_layer_options = CSLSetNameValue(Ogr_layer_options, "DIM", "2");
+	}
+    }
+    Ogr_layer = OGR_DS_CreateLayer(Map->fInfo.ogr.ds, Map->fInfo.ogr.layer_name,
+				   Ogr_spatial_ref, Ogr_geom_type, Ogr_layer_options);
     CSLDestroy(Ogr_layer_options);
     if (!Ogr_layer) {
 	G_warning(_("Unable to create OGR layer <%s> in '%s'"),
@@ -282,7 +336,7 @@
 	return -1;
     }
     Map->fInfo.ogr.layer = Ogr_layer;
-    
+
     return 0;
 }
 #endif

Modified: grass/trunk/lib/vector/Vlib/write_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/write_ogr.c	2010-12-25 16:25:52 UTC (rev 44721)
+++ grass/trunk/lib/vector/Vlib/write_ogr.c	2010-12-25 20:43:32 UTC (rev 44722)
@@ -66,8 +66,8 @@
     OGRwkbGeometryType Ogr_geom_type;
 
     if (!Map->fInfo.ogr.layer) {
-	G_warning(_("OGR layer not defined"));
-	return -1;
+	if (V2_open_new_ogr(Map, type) < 0)
+	    return -1;
     }
 
     Ogr_featuredefn = OGR_L_GetLayerDefn(Map->fInfo.ogr.layer);



More information about the grass-commit mailing list