[GRASS-SVN] r64010 - in grass/branches/releasebranch_7_0: . raster/r.distance

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 8 12:29:00 PST 2015


Author: neteler
Date: 2015-01-08 12:29:00 -0800 (Thu, 08 Jan 2015)
New Revision: 64010

Modified:
   grass/branches/releasebranch_7_0/
   grass/branches/releasebranch_7_0/raster/r.distance/defs.h
   grass/branches/releasebranch_7_0/raster/r.distance/distance.c
   grass/branches/releasebranch_7_0/raster/r.distance/edges.c
   grass/branches/releasebranch_7_0/raster/r.distance/labels.c
   grass/branches/releasebranch_7_0/raster/r.distance/main.c
   grass/branches/releasebranch_7_0/raster/r.distance/parse.c
   grass/branches/releasebranch_7_0/raster/r.distance/report.c
Log:
r.distance: Added -n flag to report null as * and sort by distance parameter backported; minor sync to trunk


Property changes on: grass/branches/releasebranch_7_0
___________________________________________________________________
Modified: svn:mergeinfo
   - /grass/trunk:60817,61096,61141,61994,62105,62179-62180,62182,62190,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62691,62705,62709,62723,62730,62739,62741,62743,62746,62750-62752,62757,62762,62785,62798,62800-62801,62803,62805,62812,62822,62824,62828,62831,62838,62845,62847,62850,62856,62879,62881,62886,62904,62907-62908,62910,62912,62914,62916,62918,62920,62925,62932-62933,62935,62940,62942,62944-62946,62949,62955-62956,62958,62960,62962,62964,62966-62968,62970,62973,62975,62977,62981,62983,62985,62987,62989,62991,62993,62995,62997,62999-63000,63003,63005,63007,63009,63011,63013,63015,63017,63020,63022,63024,63026,63028-63031,63033,63035,63037,63040,63043-63044,63047,63049,63051,63053,63055,63057,63060,63062-63064,63066,63068,63070-63071,63074,
 63076,63079,63081,63083,63085,63087,63089,63091,63093,63095,63098,63100,63102,63105,63107,63109,63111,63113-63114,63116,63119,63121,63123,63125,63130,63132-63133,63135,63137,63140,63143,63145,63147,63149,63151,63153-63154,63157,63160,63165,63170,63173,63175,63187,63192-63193,63196,63199-63200,63202,63209,63216,63220-63221,63224,63227,63240,63246,63250,63255,63259,63261,63275-63276,63279,63281,63283,63287,63290,63292,63297,63302,63304-63307,63315,63319,63327,63330,63332,63339,63342,63345,63361-63363,63367,63379,63389,63391,63393,63402,63408-63409,63416-63417,63425,63427,63429,63431,63433,63448,63451,63453,63457,63459,63464-63470,63473,63482,63497,63505,63508,63510,63515,63521-63524,63526,63536-63537,63551-63552,63554,63556,63558-63559,63562,63570,63576,63582,63589-63593,63602,63604,63608-63609,63616,63618,63624,63626,63628,63630,63634,63639,63642,63644,63650,63652,63656,63679,63685,63689,63691,63693,63695,63698,63700-63702,63704,63706-63707,63709,63711,63713,63715,63717-63718
 ,63721-63727,63731,63733,63736,63740-63742,63744-63749,63751-63752,63756,63758,63760-63761,63767,63777,63780,63783,63787,63793,63796-63798,63801,63803,63807-63808,63812,63814,63816,63821,63823-63824,63827-63828,63830-63831,63834-63835,63837-63839,63841,63843-63844,63846-63847,63849,63851,63853,63855-63856,63858-63859,63862,63865,63869,63871,63882,63884,63889-63890,63892-63895,63897-63898,63900,63902-63903,63905,63907,63909-63911,63913-63914,63927,63930-63931,63940-63941,63945,63954-63955,63960,63965,63967,63976-63983,63995
   + /grass/trunk:60497,60563-60566,60817,61096,61141,61994,62105,62179-62180,62182,62190,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62691,62705,62709,62723,62730,62739,62741,62743,62746,62750-62752,62757,62762,62785,62798,62800-62801,62803,62805,62812,62822,62824,62828,62831,62838,62845,62847,62850,62856,62879,62881,62886,62904,62907-62908,62910,62912,62914,62916,62918,62920,62925,62932-62933,62935,62940,62942,62944-62946,62949,62955-62956,62958,62960,62962,62964,62966-62968,62970,62973,62975,62977,62981,62983,62985,62987,62989,62991,62993,62995,62997,62999-63000,63003,63005,63007,63009,63011,63013,63015,63017,63020,63022,63024,63026,63028-63031,63033,63035,63037,63040,63043-63044,63047,63049,63051,63053,63055,63057,63060,63062-63064,63066,63068,
 63070-63071,63074,63076,63079,63081,63083,63085,63087,63089,63091,63093,63095,63098,63100,63102,63105,63107,63109,63111,63113-63114,63116,63119,63121,63123,63125,63130,63132-63133,63135,63137,63140,63143,63145,63147,63149,63151,63153-63154,63157,63160,63165,63170,63173,63175,63187,63192-63193,63196,63199-63200,63202,63209,63216,63220-63221,63224,63227,63240,63246,63250,63255,63259,63261,63275-63276,63279,63281,63283,63287,63290,63292,63297,63302,63304-63307,63315,63319,63327,63330,63332,63339,63342,63345,63361-63363,63367,63379,63389,63391,63393,63402,63408-63409,63416-63417,63425,63427,63429,63431,63433,63448,63451,63453,63457,63459,63464-63470,63473,63482,63497,63505,63508,63510,63515,63521-63524,63526,63536-63537,63551-63552,63554,63556,63558-63559,63562,63570,63576,63582,63589-63593,63602,63604,63608-63609,63616,63618,63624,63626,63628,63630,63634,63639,63642,63644,63650,63652,63656,63679,63685,63689,63691,63693,63695,63698,63700-63702,63704,63706-63707,63709,63711,63713
 ,63715,63717-63718,63721-63727,63731,63733,63736,63740-63742,63744-63749,63751-63752,63756,63758,63760-63761,63767,63777,63780,63783,63787,63793,63796-63798,63801,63803,63807-63808,63812,63814,63816,63821,63823-63824,63827-63828,63830-63831,63834-63835,63837-63839,63841,63843-63844,63846-63847,63849,63851,63853,63855-63856,63858-63859,63862,63865,63869,63871,63882,63884,63889-63890,63892-63895,63897-63898,63900,63902-63903,63905,63907,63909-63911,63913-63914,63927,63930-63931,63940-63941,63945,63954-63955,63960,63965,63967,63976-63983,63995

Modified: grass/branches/releasebranch_7_0/raster/r.distance/defs.h
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.distance/defs.h	2015-01-08 20:25:52 UTC (rev 64009)
+++ grass/branches/releasebranch_7_0/raster/r.distance/defs.h	2015-01-08 20:29:00 UTC (rev 64010)
@@ -4,15 +4,16 @@
  * MODULE:       r.distance
  *
  * AUTHOR(S):    Michael Shapiro - CERL
+ *               Sort/reverse sort by distance by Huidae Cho
  *
- * PURPOSE:      Locates the closest points between objects in two
+ * PURPOSE:      Locates the closest points between objects in two 
  *               raster maps.
  *
- * COPYRIGHT:    (C) 2003 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2014 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.
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
  *
  ***************************************************************************/
 
@@ -51,6 +52,10 @@
     int labels;			/* boolean: report includes cat labels */
     char *fs;			/* report field separator     */
     int overlap;		/* checking for overlapping, than distance is 0 */
+    int null;			/* report null objects as * */
+    int sort;			/* 0: sort by cat1,cat2 (default)
+				   1: sort by distance in ascending order
+				   2: sort by distance in descending order */
 };
 
 /* distance.c */
@@ -61,7 +66,7 @@
 
 /* edges.c */
 void print_edge_info(struct Map *);
-void find_edge_cells(struct Map *);
+void find_edge_cells(struct Map *, int);
 void add_edge_cell(struct Map *, CELL, int, int);
 void init_edge_list(struct Map *);
 void sort_edge_list(struct Map *);

Modified: grass/branches/releasebranch_7_0/raster/r.distance/distance.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.distance/distance.c	2015-01-08 20:25:52 UTC (rev 64009)
+++ grass/branches/releasebranch_7_0/raster/r.distance/distance.c	2015-01-08 20:29:00 UTC (rev 64010)
@@ -4,15 +4,16 @@
  * MODULE:       r.distance
  *
  * AUTHOR(S):    Michael Shapiro - CERL
+ *               Sort/reverse sort by distance by Huidae Cho
  *
- * PURPOSE:      Locates the closest points between objects in two
+ * PURPOSE:      Locates the closest points between objects in two 
  *               raster maps.
  *
- * COPYRIGHT:    (C) 2003 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2014 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.
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
  *
  ***************************************************************************/
 

Modified: grass/branches/releasebranch_7_0/raster/r.distance/edges.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.distance/edges.c	2015-01-08 20:25:52 UTC (rev 64009)
+++ grass/branches/releasebranch_7_0/raster/r.distance/edges.c	2015-01-08 20:29:00 UTC (rev 64010)
@@ -4,15 +4,16 @@
  * MODULE:       r.distance
  *
  * AUTHOR(S):    Michael Shapiro - CERL
+ *               Sort/reverse sort by distance by Huidae Cho
  *
- * PURPOSE:      Locates the closest points between objects in two
+ * PURPOSE:      Locates the closest points between objects in two 
  *               raster maps.
  *
- * COPYRIGHT:    (C) 2003 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2014 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.
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
  *
  ***************************************************************************/
 
@@ -33,7 +34,7 @@
     fprintf(stdout, "\n");
 }
 
-void find_edge_cells(struct Map *map)
+void find_edge_cells(struct Map *map, int null)
 {
     void init_edge_list();
     void add_edge_cell();
@@ -74,11 +75,12 @@
 	Rast_get_c_row(fd, &buf1[1], row);
 
 	for (col = 1; col <= ncols; col++) {
-	    if (buf1[col]	/* is a valid category */
+	    if ((buf1[col]	/* is a valid category */
 		&&(buf1[col - 1] != buf1[col]	/* 4 neighbors not the same? */
 		   ||buf1[col + 1] != buf1[col]
 		   || buf0[col] != buf1[col]
-		   || buf2[col] != buf1[col]))
+		   || buf2[col] != buf1[col])) &&
+		(null || !Rast_is_c_null_value(&buf1[col])))
 		add_edge_cell(map, buf1[col], row, col - 1);
 	}
     }

Modified: grass/branches/releasebranch_7_0/raster/r.distance/labels.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.distance/labels.c	2015-01-08 20:25:52 UTC (rev 64009)
+++ grass/branches/releasebranch_7_0/raster/r.distance/labels.c	2015-01-08 20:29:00 UTC (rev 64010)
@@ -4,15 +4,16 @@
  * MODULE:       r.distance
  *
  * AUTHOR(S):    Michael Shapiro - CERL
+ *               Sort/reverse sort by distance by Huidae Cho
  *
- * PURPOSE:      Locates the closest points between objects in two
+ * PURPOSE:      Locates the closest points between objects in two 
  *               raster maps.
  *
- * COPYRIGHT:    (C) 2003 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2014 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.
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
  *
  ***************************************************************************/
 

Modified: grass/branches/releasebranch_7_0/raster/r.distance/main.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.distance/main.c	2015-01-08 20:25:52 UTC (rev 64009)
+++ grass/branches/releasebranch_7_0/raster/r.distance/main.c	2015-01-08 20:29:00 UTC (rev 64010)
@@ -4,11 +4,12 @@
  * MODULE:       r.distance
  *
  * AUTHOR(S):    Michael Shapiro - CERL
+ *               Sort/reverse sort by distance by Huidae Cho
  *
  * PURPOSE:      Locates the closest points between objects in two 
  *               raster maps.
  *
- * COPYRIGHT:    (C) 2003, 2012 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2014 by the GRASS Development Team
  *
  *               This program is free software under the GNU General
  *               Public License (>=v2). Read the file COPYING that
@@ -46,8 +47,8 @@
 	read_labels(&parms.map2);
     }
 
-    find_edge_cells(&parms.map1);
-    find_edge_cells(&parms.map2);
+    find_edge_cells(&parms.map1, parms.null);
+    find_edge_cells(&parms.map2, parms.null);
 
     report(&parms);
 

Modified: grass/branches/releasebranch_7_0/raster/r.distance/parse.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.distance/parse.c	2015-01-08 20:25:52 UTC (rev 64009)
+++ grass/branches/releasebranch_7_0/raster/r.distance/parse.c	2015-01-08 20:29:00 UTC (rev 64010)
@@ -4,28 +4,30 @@
  * MODULE:       r.distance
  *
  * AUTHOR(S):    Michael Shapiro - CERL
+ *               Sort/reverse sort by distance by Huidae Cho
  *
- * PURPOSE:      Locates the closest points between objects in two
+ * PURPOSE:      Locates the closest points between objects in two 
  *               raster maps.
  *
- * COPYRIGHT:    (C) 2003 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2014 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.
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
  *
  ***************************************************************************/
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include "defs.h"
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
 void parse(int argc, char *argv[], struct Parms *parms)
 {
-    struct Option *maps, *fs;
-    struct Flag *labels, *overlap;
+    struct Option *maps, *fs, *sort;
+    struct Flag *labels, *overlap, *null;
     const char *name, *mapset;
 
     maps = G_define_standard_option(G_OPT_R_MAPS);
@@ -35,6 +37,19 @@
     fs = G_define_standard_option(G_OPT_F_SEP);
     fs->answer = ":";		/* colon is default output fs */
 
+    sort = G_define_option();
+    sort->key = "sort";
+    sort->type = TYPE_STRING;
+    sort->required = NO;
+    sort->multiple = NO;
+    sort->label = _("Sort output by distance");
+    sort->description = _("Default: sorted by categories");
+    sort->options = "asc,desc";
+    G_asprintf((char **)&(sort->descriptions),
+               "asc;%s;desc;%s",
+               _("Sort by distance in ascending order"),
+               _("Sort by distance in descending order"));
+
     labels = G_define_flag();
     labels->key = 'l';
     labels->description = _("Include category labels in the output");
@@ -44,6 +59,10 @@
     overlap->description =
 	_("Report zero distance if rasters are overlapping");
 
+    null = G_define_flag();
+    null->key = 'n';
+    null->description = _("Report null objects as *");
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
@@ -51,6 +70,8 @@
     mapset = parms->map1.mapset = G_find_raster2(name, "");
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"), name);
+    if (Rast_map_type(name, mapset) != CELL_TYPE)
+	G_fatal_error(_("Raster map <%s> is not CELL"), name);
 
     parms->map1.fullname = G_fully_qualified_name(name, mapset);
 
@@ -58,10 +79,17 @@
     mapset = parms->map2.mapset = G_find_raster2(name, "");
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"), name);
+    if (Rast_map_type(name, mapset) != CELL_TYPE)
+	G_fatal_error(_("Raster map <%s> is not CELL"), name);
 
     parms->map2.fullname = G_fully_qualified_name(name, mapset);
 
     parms->labels = labels->answer ? 1 : 0;
-    parms->fs = fs->answer;
+    parms->fs = G_option_to_separator(fs);
     parms->overlap = overlap->answer ? 1 : 0;
+    parms->null = null->answer ? 1 : 0;
+    if (sort->answer)
+	parms->sort = strcmp(sort->answer, "asc") == 0 ? 1 : 2;
+    else
+	parms->sort = 0;
 }

Modified: grass/branches/releasebranch_7_0/raster/r.distance/report.c
===================================================================
--- grass/branches/releasebranch_7_0/raster/r.distance/report.c	2015-01-08 20:25:52 UTC (rev 64009)
+++ grass/branches/releasebranch_7_0/raster/r.distance/report.c	2015-01-08 20:29:00 UTC (rev 64010)
@@ -4,31 +4,50 @@
  * MODULE:       r.distance
  *
  * AUTHOR(S):    Michael Shapiro - CERL
+ *               Sort/reverse sort by distance by Huidae Cho
  *
- * PURPOSE:      Locates the closest points between objects in two
+ * PURPOSE:      Locates the closest points between objects in two 
  *               raster maps.
  *
- * COPYRIGHT:    (C) 2003 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2014 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.
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
  *
  ***************************************************************************/
 
+#include <stdlib.h>
 #include <grass/glocale.h>
 
 #include "defs.h"
 
+struct ReportLine
+{
+    CELL cat1;
+    CELL cat2;
+    int isnull1;
+    int isnull2;
+    double east1;
+    double north1;
+    double east2;
+    double north2;
+    double distance;
+};
+
+static void print(struct ReportLine *, struct Parms *);
+static int compare(const void *, const void *);
+static int revcompare(const void *, const void *);
+
 void report(struct Parms *parms)
 {
     int i1, i2;
     struct Map *map1, *map2;
-    char *fs;
     double distance, north1, east1, north2, east2;
     struct Cell_head region;
     struct CatEdgeList *list1, *list2;
-    char temp[100];
+    struct ReportLine *lines;
+    int nlines;
 
     extern void find_minimum_distance();
     extern char *get_label();
@@ -38,48 +57,142 @@
 
     map1 = &parms->map1;
     map2 = &parms->map2;
-    fs = parms->fs;
 
     G_message(_("Processing..."));
 
+    if (parms->sort > 0)
+	lines = (struct ReportLine *)G_malloc(
+			map1->edges.ncats * map2->edges.ncats *
+			sizeof(struct ReportLine));
+    else
+	lines = NULL;
+    nlines = 0;
+
     for (i1 = 0; i1 < map1->edges.ncats; i1++) {
+	int isnull1;
+
 	list1 = &map1->edges.catlist[i1];
+	isnull1 = parms->null ? Rast_is_c_null_value(&(list1->cat)) : 0;
+
 	for (i2 = 0; i2 < map2->edges.ncats; i2++) {
+	    int isnull2;
+	    struct ReportLine line;
+
 	    list2 = &map2->edges.catlist[i2];
+	    isnull2 = parms->null ? Rast_is_c_null_value(&(list2->cat)) : 0;
+
 	    find_minimum_distance(list1, list2,
 				  &east1, &north1, &east2, &north2, &distance,
 				  &region, parms->overlap, map1->name,
 				  map2->name);
 
-	    /* print cat numbers */
-	    fprintf(stdout, "%ld%s%ld", (long)list1->cat, fs,
-		    (long)list2->cat);
+	    line.cat1 = list1->cat;
+	    line.cat2 = list2->cat;
+	    line.isnull1 = isnull1;
+	    line.isnull2 = isnull2;
+	    line.east1 = east1;
+	    line.north1 = north1;
+	    line.east2 = east2;
+	    line.north2 = north2;
+	    line.distance = distance;
 
-	    /* print distance */
-	    sprintf(temp, "%.10f", distance);
-	    G_trim_decimal(temp);
-	    fprintf(stdout, "%s%s", fs, temp);
+	    if (parms->sort > 0)
+		lines[nlines++] = line;
+	    else
+		print(&line, parms);
+	}
+    }
 
-	    /* print coordinates of the closest pair */
-	    G_format_easting(east1, temp,
-			     G_projection() == PROJECTION_LL ? -1 : 0);
-	    fprintf(stdout, "%s%s", fs, temp);
-	    G_format_northing(north1, temp,
-			      G_projection() == PROJECTION_LL ? -1 : 0);
-	    fprintf(stdout, "%s%s", fs, temp);
-	    G_format_easting(east2, temp,
-			     G_projection() == PROJECTION_LL ? -1 : 0);
-	    fprintf(stdout, "%s%s", fs, temp);
-	    G_format_northing(north2, temp,
-			      G_projection() == PROJECTION_LL ? -1 : 0);
-	    fprintf(stdout, "%s%s", fs, temp);
+    if (parms->sort > 0) {
+	int i;
 
-	    /* print category labels */
-	    if (parms->labels) {
-		fprintf(stdout, "%s%s", fs, get_label(map1, list1->cat));
-		fprintf(stdout, "%s%s", fs, get_label(map2, list2->cat));
-	    }
-	    fprintf(stdout, "\n");
-	}
+	if (parms->sort == 1)
+	    qsort(lines, nlines, sizeof(struct ReportLine), compare);
+	else
+	    qsort(lines, nlines, sizeof(struct ReportLine), revcompare);
+
+	for(i = 0; i < nlines; i++)
+	    print(&lines[i], parms);
     }
 }
+
+static void print(struct ReportLine *line, struct Parms *parms)
+{
+    char *fs;
+    char temp[100];
+
+    fs = parms->fs;
+
+    /* print cat numbers */
+    if (line->isnull1 && line->isnull2)
+	fprintf(stdout, "*%s*", fs);
+    else if (line->isnull1)
+	fprintf(stdout, "*%s%ld", fs, (long)line->cat2);
+    else if (line->isnull2)
+	fprintf(stdout, "%ld%s*", (long)line->cat1, fs);
+    else
+	fprintf(stdout, "%ld%s%ld", (long)line->cat1, fs, (long)line->cat2);
+
+    /* print distance */
+    sprintf(temp, "%.10f", line->distance);
+    G_trim_decimal(temp);
+    fprintf(stdout, "%s%s", fs, temp);
+
+    /* print coordinates of the closest pair */
+    G_format_easting(line->east1, temp,
+		     G_projection() == PROJECTION_LL ? -1 : 0);
+    fprintf(stdout, "%s%s", fs, temp);
+    G_format_northing(line->north1, temp,
+		      G_projection() == PROJECTION_LL ? -1 : 0);
+    fprintf(stdout, "%s%s", fs, temp);
+    G_format_easting(line->east2, temp,
+		     G_projection() == PROJECTION_LL ? -1 : 0);
+    fprintf(stdout, "%s%s", fs, temp);
+    G_format_northing(line->north2, temp,
+		      G_projection() == PROJECTION_LL ? -1 : 0);
+    fprintf(stdout, "%s%s", fs, temp);
+
+    /* print category labels */
+    if (parms->labels) {
+	struct Map *map1, *map2;
+
+	map1 = &parms->map1;
+	map2 = &parms->map2;
+
+	fprintf(stdout, "%s%s", fs, get_label(map1, line->cat1));
+	fprintf(stdout, "%s%s", fs, get_label(map2, line->cat2));
+    }
+    fprintf(stdout, "\n");
+}
+
+static int compare(const void *p1, const void *p2)
+{
+    const struct ReportLine *line1, *line2;
+
+    line1 = (const struct ReportLine *)p1;
+    line2 = (const struct ReportLine *)p2;
+
+    if (line1->distance < line2->distance) return -1; /* short distance first */
+    if (line1->distance > line2->distance) return 1;
+
+    if (!line1->isnull1 && line2->isnull1) return -1; /* non-null first */
+    if (line1->isnull1 && !line2->isnull1) return 1;
+    if (!line1->isnull1 && !line2->isnull1) {
+	if (line1->cat1 < line2->cat1) return -1; /* small cat first */
+	if (line1->cat1 > line2->cat1) return 1;
+    }
+
+    if (!line1->isnull2 && line2->isnull2) return -1;
+    if (line1->isnull2 && !line2->isnull2) return 1;
+    if (!line1->isnull2 && !line2->isnull2) {
+	if (line1->cat2 < line2->cat2) return -1;
+	if (line1->cat2 > line2->cat2) return 1;
+    }
+
+    return 0; /* same cat1, same cat2 */
+}
+
+static int revcompare(const void *p1, const void *p2)
+{
+    return -compare(p1, p2);
+}



More information about the grass-commit mailing list