[GRASS-SVN] r61850 - grass/trunk/vector/v.distance

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Sep 10 11:46:24 PDT 2014


Author: mmetz
Date: 2014-09-10 11:46:24 -0700 (Wed, 10 Sep 2014)
New Revision: 61850

Modified:
   grass/trunk/vector/v.distance/distance.c
   grass/trunk/vector/v.distance/main.c
   grass/trunk/vector/v.distance/v.distance.html
Log:
v.distance: use geodesic distance for latlong

Modified: grass/trunk/vector/v.distance/distance.c
===================================================================
--- grass/trunk/vector/v.distance/distance.c	2014-09-10 00:48:15 UTC (rev 61849)
+++ grass/trunk/vector/v.distance/distance.c	2014-09-10 18:46:24 UTC (rev 61850)
@@ -56,9 +56,12 @@
     double tmp_dist, tmp_x, tmp_y, tmp_z, tmp_along;
     int ret = 1;
     static struct line_pnts *iPoints = NULL;
+    static struct line_pnts *LPoints = NULL;
     
-    if (!iPoints)
+    if (!iPoints) {
 	iPoints = Vect_new_line_struct();
+	LPoints = Vect_new_line_struct();
+    }
 
     *dist = PORT_DOUBLE_MAX;
 
@@ -80,7 +83,6 @@
 	Vect_line_distance(TPoints, FPoints->x[0], FPoints->y[0],
 			   FPoints->z[0], with_z, tx, ty,tz, dist, 
 			   NULL, talong);
-
     }
 
     /* point -> line and line -> line */
@@ -176,6 +178,45 @@
 	}
     }
 
+    if (geodesic) {
+	if (*fx != *tx || *fy != *ty || (with_z && *fz != *tz)) {
+	    Vect_reset_line(LPoints);
+	    Vect_append_point(LPoints, *fx, *fy, *fz);
+	    Vect_append_point(LPoints, *tx, *ty, *tz);
+	    *dist = Vect_line_geodesic_length(LPoints);
+	}
+	/* falong */
+	if (FPoints->x[0] != *fx || FPoints->y[0] != *fy ||
+	    (with_z && FPoints->z[0] != *fz)) {
+	
+	    fseg = Vect_line_distance(FPoints, *tx, *ty, *tz,
+			       with_z, &tmp_x, &tmp_y, &tmp_z,
+			       &tmp_dist, NULL, &tmp_along);
+
+	    Vect_reset_line(LPoints);
+	    for (i = 0; i < fseg; i++)
+		Vect_append_point(LPoints, FPoints->x[i], FPoints->y[i],
+		                  FPoints->z[i]);
+	    Vect_append_point(LPoints, *fx, *fy, *fz);
+	    *falong = Vect_line_geodesic_length(LPoints);
+	}
+	/* talong */
+	if (TPoints->x[0] != *tx || TPoints->y[0] != *ty ||
+	    (with_z && TPoints->z[0] != *tz)) {
+	
+	    tseg = Vect_line_distance(TPoints, *fx, *fy, *fz,
+			       with_z, &tmp_x, &tmp_y, &tmp_z,
+			       &tmp_dist, NULL, &tmp_along);
+
+	    Vect_reset_line(LPoints);
+	    for (i = 0; i < tseg; i++)
+		Vect_append_point(LPoints, TPoints->x[i], TPoints->y[i],
+		                  TPoints->z[i]);
+	    Vect_append_point(LPoints, *tx, *ty, *tz);
+	    *talong = Vect_line_geodesic_length(LPoints);
+	}
+    }
+
     return ret;
 }
 

Modified: grass/trunk/vector/v.distance/main.c
===================================================================
--- grass/trunk/vector/v.distance/main.c	2014-09-10 00:48:15 UTC (rev 61849)
+++ grass/trunk/vector/v.distance/main.c	2014-09-10 18:46:24 UTC (rev 61850)
@@ -66,6 +66,7 @@
     double fx, fy, fz, falong, fangle, tx, ty, tz, talong, tangle, dist;
     double tmp_fx, tmp_fy, tmp_fz, tmp_falong, tmp_fangle, tmp_dist;
     double tmp_tx, tmp_ty, tmp_tz, tmp_talong, tmp_tangle;
+    int geodesic;
     struct field_info *Fi, *toFi;
     dbString stmt, dbstr;
     dbDriver *driver, *to_driver;
@@ -236,6 +237,8 @@
     if (flag.all->answer)
 	do_all = TRUE;
 
+    geodesic = G_projection() == PROJECTION_LL;
+
     /* Read upload and column options */
     /* count */
     i = 0;
@@ -689,7 +692,7 @@
 		    line2line(FPoints, ftype, TPoints, ttype,
 		              &tmp_fx, &tmp_fy, &tmp_fz, &tmp_falong, &tmp_fangle,
 		              &tmp_tx, &tmp_ty, &tmp_tz, &tmp_talong, &tmp_tangle,
-			      &tmp_dist, with_z, 0);
+			      &tmp_dist, with_z, geodesic);
 
 		    if (tmp_dist > max || tmp_dist < min)
 			continue;	/* not in threshold */
@@ -763,7 +766,7 @@
 		    line2area(&To, FPoints, ftype, aList->id[i], &aList->box[i],
 		              &tmp_fx, &tmp_fy, &tmp_fz, &tmp_falong, &tmp_fangle,
 		              &tmp_tx, &tmp_ty, &tmp_tz, &tmp_talong, &tmp_tangle,
-			      &tmp_dist, with_z, 0);
+			      &tmp_dist, with_z, geodesic);
 
 		    if (tmp_dist > max || tmp_dist < min)
 			continue;	/* not in threshold */
@@ -962,7 +965,7 @@
 		    line2area(&From, TPoints, ttype, area, &fbox,
 		              &tmp_tx, &tmp_ty, &tmp_tz, &tmp_talong, &tmp_tangle,
 		              &tmp_fx, &tmp_fy, &tmp_fz, &tmp_falong, &tmp_fangle,
-			      &tmp_dist, with_z, 0);
+			      &tmp_dist, with_z, geodesic);
 
 		    if (tmp_dist > max || tmp_dist < min)
 			continue;	/* not in threshold */
@@ -1048,7 +1051,7 @@
 		    poly = line2area(&From, TPoints, ttype, area, &fbox,
 		              &tmp_tx, &tmp_ty, &tmp_tz, &tmp_talong, &tmp_tangle,
 		              &tmp_fx, &tmp_fy, &tmp_fz, &tmp_falong, &tmp_fangle,
-			      &tmp_dist, with_z, 0);
+			      &tmp_dist, with_z, geodesic);
 
 		    if (poly == 3) {
 			/* 'to' outer ring is outside 'from' area,
@@ -1072,7 +1075,7 @@
 			    poly = line2area(&To, FPoints, ttype, tarea, &aList->box[i],
 				      &tmp_fx, &tmp_fy, &tmp_fz, &tmp_falong, &tmp_fangle,
 				      &tmp_tx, &tmp_ty, &tmp_tz, &tmp_talong, &tmp_tangle,
-				      &tmp_dist, with_z, 0);
+				      &tmp_dist, with_z, geodesic);
 
 			    /* inside isle ? */
 			    poly = poly == 2;
@@ -1092,7 +1095,7 @@
 				line2area(&From, TPoints, ttype, area, &fbox,
 					  &tmp2_tx, &tmp2_ty, &tmp2_tz, &tmp2_talong, &tmp2_tangle,
 					  &tmp2_fx, &tmp2_fy, &tmp2_fz, &tmp2_falong, &tmp2_fangle,
-					  &tmp2_dist, with_z, 0);
+					  &tmp2_dist, with_z, geodesic);
 
 				if (tmp2_dist < tmp_dist) {
 				    tmp_dist = tmp2_dist;

Modified: grass/trunk/vector/v.distance/v.distance.html
===================================================================
--- grass/trunk/vector/v.distance/v.distance.html	2014-09-10 00:48:15 UTC (rev 61849)
+++ grass/trunk/vector/v.distance/v.distance.html	2014-09-10 18:46:24 UTC (rev 61850)
@@ -53,11 +53,9 @@
 <p>The upload <b>column</b>(s) must already exist. Create one with 
 <em><a href="v.db.addcolumn.html">v.db.addcolumn</a></em>.
 
-<!-- needs Vect_line_geodesic_distance()
 <p>In lat-long locations <em>v.distance</em> gives distances 
-(<em>dist</em> and <em>to_along</em>) not in degrees but in meters  
-calculated as geodesic distances on a sphere.
--->
+(<em>dist</em>, <em>from_along</em>, and <em>to_along</em>) not in 
+degrees but in meters calculated as geodesic distances on a sphere.
 
 <h2>EXAMPLES</h2>
 



More information about the grass-commit mailing list