浏览代码

Simple Features API: Vect_sfa_is_line_closed() added

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@39378 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 15 年之前
父节点
当前提交
a30cf157fc
共有 3 个文件被更改,包括 33 次插入14 次删除
  1. 1 0
      include/vector.h
  2. 1 0
      lib/vector/Vlib/ascii.c
  3. 31 14
      lib/vector/Vlib/simple_features.c

+ 1 - 0
include/vector.h

@@ -419,6 +419,7 @@ void Vect_write_ascii_head(FILE *, struct Map_info *);
 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);
+int Vect_sfa_is_line_closed(const struct line_pnts *, int, int);
 
 /*
  * Internal functions, MUST NOT be used in modules

+ 1 - 0
lib/vector/Vlib/ascii.c

@@ -11,6 +11,7 @@
   (>=v2).  Read the file COPYING that comes with GRASS for details.
   
   \author Original author CERL
+  \author Updated for GRASS 7 (SF support) by Martin Landa <landa.martin gmail.com>
 */
 #include <stdio.h>
 

+ 31 - 14
lib/vector/Vlib/simple_features.c

@@ -119,6 +119,34 @@ void Vect_sfa_write_line_wkt(const struct line_pnts *Points, int type, int with_
     fflush(file);
 }
 
+/*!
+  \brief Check if feature is closed
+
+  \param Points pointer to line_pnts structure
+  \param type   feature type (GV_POINT, GV_LINE, ...)
+
+  \return 1  feature closed
+  \return 0  feature not closed
+  \return -1 feature type not supported (GV_POINT, GV_CENTROID, ...)
+*/
+int Vect_sfa_is_line_closed(const struct line_pnts *Points, int type, int with_z)
+{
+    int npoints;
+    if (type & (GV_LINES)) {
+	npoints = Vect_get_num_line_points(Points);
+	if (npoints > 2 &&
+	    Points->x[0] == Points->x[npoints-1] &&
+	    Points->y[0] == Points->y[npoints-1]) {
+	    if (!with_z)
+		return 1;
+	    if (Points->z[0] == Points->z[npoints-1])
+		return 1;
+	}
+	return 0;
+    }
+    return -1;
+}
+
 int check_sftype(const struct line_pnts *points, int type, int sftype, int with_z)
 {
     if (type == GV_POINT && sftype == SF_POINT) {
@@ -133,26 +161,15 @@ int check_sftype(const struct line_pnts *points, int type, int sftype, int with_
 	    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;
-		}
-	    }
+	    if (Vect_sfa_is_line_closed(points, type, with_z))
+		return 1;
 	}
     }
 
     if (type == GV_BOUNDARY) {
-	int num = Vect_get_num_line_points(points);
 	if (sftype == SF_POLYGON &&
-	    points->x[0] == points->x[num-1] &&
-	    points->y[0] == points->y[num-1]) {
+	    Vect_sfa_is_line_closed(points, type, 0)) /* force 2D */
 	    return 1;
-	}
     }
 
     return 0;