[GRASS-SVN] r39375 - in grass/trunk: include include/vect lib/vector/Vlib vector/v.in.ascii vector/v.out.ascii

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 2 11:48:03 EDT 2009


Author: martinl
Date: 2009-10-02 11:48:03 -0400 (Fri, 02 Oct 2009)
New Revision: 39375

Added:
   grass/trunk/lib/vector/Vlib/simple_features.c
Modified:
   grass/trunk/include/vect/dig_defines.h
   grass/trunk/include/vector.h
   grass/trunk/lib/vector/Vlib/ascii.c
   grass/trunk/vector/v.in.ascii/main.c
   grass/trunk/vector/v.out.ascii/args.c
   grass/trunk/vector/v.out.ascii/main.c
Log:
very initial simple features API implemented (in progress)
     * v.out.ascii format=wkt
     * currently only points, lines supported


Modified: grass/trunk/include/vect/dig_defines.h
===================================================================
--- grass/trunk/include/vect/dig_defines.h	2009-10-02 12:04:22 UTC (rev 39374)
+++ grass/trunk/include/vect/dig_defines.h	2009-10-02 15:48:03 UTC (rev 39375)
@@ -225,6 +225,13 @@
 
 /*! \brief GRASS ASCII vector format */
 #define GV_ASCII_FORMAT_POINT 0
-#define GV_ASCII_FORMAT_ALL   1
+#define GV_ASCII_FORMAT_STD   1
+#define GV_ASCII_FORMAT_WKT   2
 
+/*! \brief Simple feature types */
+#define SF_POINT      0x01
+#define SF_LINE	      0x02
+#define SF_LINESTRING 0x04
+#define SF_LINEARRING 0x08
+
 #define HEADSTR	50

Modified: grass/trunk/include/vector.h
===================================================================
--- grass/trunk/include/vector.h	2009-10-02 12:04:22 UTC (rev 39374)
+++ grass/trunk/include/vector.h	2009-10-02 15:48:03 UTC (rev 39375)
@@ -415,6 +415,11 @@
 		     int, char*, char **);
 void Vect_write_ascii_head(FILE *, struct Map_info *);
 
+/* Simple Features */
+void Vect_sfa_write_line_wkt(const struct line_pnts *, int, int, int, FILE *);
+int Vect_sfa_check_line_type(const struct line_pnts *, int, int, int);
+int Vect_sfa_get_line_type(const struct line_pnts *, int, int);
+
 /*
  * Internal functions, MUST NOT be used in modules
  */

Modified: grass/trunk/lib/vector/Vlib/ascii.c
===================================================================
--- grass/trunk/lib/vector/Vlib/ascii.c	2009-10-02 12:04:22 UTC (rev 39374)
+++ grass/trunk/lib/vector/Vlib/ascii.c	2009-10-02 15:48:03 UTC (rev 39375)
@@ -413,8 +413,6 @@
 	}
 
 	if (format == GV_ASCII_FORMAT_POINT) {
-	    /*fprintf(ascii, "%c", ctype); */
-
 	    if (region_flag) {
 		if ((window.east < Points->x[0]) ||
 		    (window.west > Points->x[0]))
@@ -497,7 +495,7 @@
 
 	    fprintf(ascii, "\n");
 	}
-	else {
+	else if (format == GV_ASCII_FORMAT_STD) {
 	    /* FORMAT_STANDARD */
 	    if (ver == 5 && Cats->n_cats > 0)
 		fprintf(ascii, "%c  %d %d\n", ctype, Points->n_points,
@@ -560,6 +558,13 @@
 		}
 	    }
 	}
+	else if (format == GV_ASCII_FORMAT_WKT) {
+	    /* Well-Known Text */
+	    Vect_sfa_write_line_wkt(Points, type, Vect_is_3d(Map), dp, ascii);
+	}
+	else {
+	    G_fatal_error(_("Unknown format"));
+	}
 	n_lines++;
     }
 

Added: grass/trunk/lib/vector/Vlib/simple_features.c
===================================================================
--- grass/trunk/lib/vector/Vlib/simple_features.c	                        (rev 0)
+++ grass/trunk/lib/vector/Vlib/simple_features.c	2009-10-02 15:48:03 UTC (rev 39375)
@@ -0,0 +1,164 @@
+/*!
+  \file vector/Vlib/simple_features.c
+ 
+  \brief Vector library - OGC Simple Features Access
+
+  Note: In progress. Currently on GV_POINT, GV_LINE are supported.
+  
+  Higher level functions for reading/writing/manipulating vectors.
+  
+  (C) 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 Martin Landa <landa.martin gmail.com>
+*/
+
+#include <stdio.h>
+
+#include <grass/vector.h>
+#include <grass/glocale.h>
+
+static int check_sftype(const struct line_pnts *, int, int, int);
+static int get_sftype(const struct line_pnts *, int, int);
+static void print_point(const struct line_pnts *, int, int, int, FILE *);
+
+/*!
+  \brief Get SF type of given vector feature
+
+  List of supported feature types:
+   - GV_POINT    -> SF_POINT
+   - GV_LINE     -> SF_LINE / SF_LINESTRING / SF_LINEARRING
+  
+  \param Points  pointer to line_pnts structure
+  \param type    feature type (GV_POINT, GV_LINE, ...)
+  \param with_z  non-zero value for 3D data
+
+  \return SF type identificator (see list of supported types)
+  \return -1 on error
+*/
+int Vect_sfa_get_line_type(const struct line_pnts *Points, int type, int with_z)
+{
+    return get_sftype(Points, type, with_z);
+}
+
+/*!
+  \brief Check SF type
+
+  E.g. if <em>type</em> is GV_LINE with two or more segments and the
+  start node is identical with the end node, and <em>sftype</em> is
+  SF_LINEARRING, functions returns 1, otherwise 0.
+
+  \param Points pointer to line_pnts structure
+  \param type feature type (GV_POINT, GV_LINE, ...)
+  \param sftype SF type to be checked (SF_POINT, SF_LINE, ...)
+  \param with_z non-zero value for 3D data
+  
+  \return 1 if type is sftype
+  \return 0 type differs from sftype
+*/
+int Vect_sfa_check_line_type(const struct line_pnts *Points, int type, int sftype, int with_z)
+{
+    return check_sftype(Points, type, sftype, with_z);
+}
+
+/*!
+  \brief Print feature in Well-Known Text format
+
+  \param Points    pointer to line_pnts structure
+  \param type      feature type
+  \param with_z    non-zero value for 3D data
+  \param precision floating number precision 
+  \param[out] file file where to write the output
+*/
+void Vect_sfa_write_line_wkt(const struct line_pnts *Points, int type, int with_z, int precision, FILE *file)
+{
+    int sftype;
+    
+    sftype = Vect_sfa_get_line_type(Points, type, with_z);
+    
+    switch(sftype) {
+    case SF_POINT: {
+	fprintf(file, "POINT(");
+	print_point(Points, 0, with_z, precision, file);
+	fprintf(file, ")\n");
+	break;
+    }
+    case SF_LINESTRING: case SF_LINE: case SF_LINEARRING: {
+	int i;
+	if (sftype == SF_LINESTRING)
+	    fprintf(file, "LINESTRING(");
+	else if (sftype ==  SF_LINE)
+	    fprintf(file, "LINE(");
+	else
+	    fprintf(file, "LINEARRING(");
+	for (i = 0; i < Points->n_points; i++) {
+	    print_point(Points, i, with_z, precision, file);
+	    if (i < Points->n_points - 1)
+		fprintf(file, ", ");
+	}
+	fprintf(file, ")\n");
+	break;
+    }
+    default: {
+	G_warning(_("Unknown Simple Features type (%d)"), sftype);
+	break;
+    }
+    }
+    fflush(file);
+}
+
+int check_sftype(const struct line_pnts *points, int type, int sftype, int with_z)
+{
+    if (type == GV_POINT && sftype == SF_POINT) {
+	return 1;
+    }
+
+    if (type == GV_LINE) {
+	if (sftype == SF_LINESTRING) {
+	    return 1;
+	}
+	if (sftype == SF_LINE && Vect_get_num_line_points(points) == 2) {
+	    return 1;
+	}
+	if (sftype == SF_LINEARRING) {
+	    int num = Vect_get_num_line_points(points);
+	    if (points->x[0] == points->x[num-1] &&
+		points->y[0] == points->y[num-1]) {
+		if (!with_z) {
+		    return 1;
+		}
+		else if (points->z[0] == points->z[num-1]) {
+		    return 1;
+		}
+	    }
+	}
+    }
+
+    return 0;
+}
+
+int get_sftype(const struct line_pnts *points, int type, int with_z)
+{
+    if (check_sftype(points, type, SF_POINT, with_z))
+	return SF_POINT;
+
+    if (check_sftype(points, type, SF_LINE, with_z))
+	return SF_LINE;
+
+    if (check_sftype(points, type, SF_LINEARRING, with_z))
+	return SF_LINEARRING;
+
+    if (check_sftype(points, type, SF_LINESTRING, with_z))
+	return SF_LINESTRING;
+
+    return -1;
+}
+
+void print_point(const struct line_pnts *Points, int index, int with_z, int precision, FILE *file)
+{
+    fprintf(file, "%.*f %.*f", precision, Points->x[index], precision, Points->y[index]);
+    if (with_z)
+	fprintf(file, " %.*f", precision, Points->z[index]);
+}


Property changes on: grass/trunk/lib/vector/Vlib/simple_features.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Modified: grass/trunk/vector/v.in.ascii/main.c
===================================================================
--- grass/trunk/vector/v.in.ascii/main.c	2009-10-02 12:04:22 UTC (rev 39374)
+++ grass/trunk/vector/v.in.ascii/main.c	2009-10-02 15:48:03 UTC (rev 39375)
@@ -173,7 +173,7 @@
     if (format_opt->answer[0] == 'p')
 	format = GV_ASCII_FORMAT_POINT;
     else
-	format = GV_ASCII_FORMAT_ALL;
+	format = GV_ASCII_FORMAT_STD;
 
     skip_lines = atoi(skip_opt->answer);
     if (skip_lines < 0)

Modified: grass/trunk/vector/v.out.ascii/args.c
===================================================================
--- grass/trunk/vector/v.out.ascii/args.c	2009-10-02 12:04:22 UTC (rev 39374)
+++ grass/trunk/vector/v.out.ascii/args.c	2009-10-02 15:48:03 UTC (rev 39375)
@@ -28,7 +28,7 @@
     format_opt->type = TYPE_STRING;
     format_opt->required = YES;
     format_opt->multiple = NO;
-    format_opt->options = "point,standard";
+    format_opt->options = "point,standard,wkt";
     format_opt->answer = "point";
     format_opt->description = _("Output format");
     
@@ -71,10 +71,12 @@
 
     *input = G_store(input_opt->answer);
     *output = G_store(output_opt->answer);
-    if (strcmp(format_opt->answer, "point") == 0)
+    if (format_opt->answer[0] == 'p')
 	*format = GV_ASCII_FORMAT_POINT;
+    else if (format_opt->answer[0] == 's')
+	*format = GV_ASCII_FORMAT_STD;
     else
-	*format = GV_ASCII_FORMAT_ALL;
+	*format = GV_ASCII_FORMAT_WKT;
     if (sscanf(dp_opt->answer, "%d", dp) != 1)
 	G_fatal_error(_("Failed to interpret 'dp' parameter as an integer"));
     /* the field separator */

Modified: grass/trunk/vector/v.out.ascii/main.c
===================================================================
--- grass/trunk/vector/v.out.ascii/main.c	2009-10-02 12:04:22 UTC (rev 39374)
+++ grass/trunk/vector/v.out.ascii/main.c	2009-10-02 15:48:03 UTC (rev 39375)
@@ -49,7 +49,7 @@
     parse_args(argc, argv, &input, &output, &format, &dp, &delim,
 	       &field, &columns, &where, &region, &old_format);
 
-    if (format == GV_ASCII_FORMAT_ALL && columns) {
+    if (format == GV_ASCII_FORMAT_STD && columns) {
 	G_warning(_("Parameter 'column' ignored in standard mode"));
     }
 
@@ -90,7 +90,7 @@
 	ascii = stdout;
     }
 
-    if (format == GV_ASCII_FORMAT_ALL) {
+    if (format == GV_ASCII_FORMAT_STD) {
 	Vect_write_ascii_head(ascii, &Map);
 	fprintf(ascii, "VERTI:\n");
     }



More information about the grass-commit mailing list