[GRASS-SVN] r41297 - in grass/trunk: include lib/gis misc/m.measure

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Mar 5 13:25:25 EST 2010


Author: martinl
Date: 2010-03-05 13:25:25 -0500 (Fri, 05 Mar 2010)
New Revision: 41297

Added:
   grass/trunk/lib/gis/units.c
Modified:
   grass/trunk/include/gis.h
   grass/trunk/include/gisdefs.h
   grass/trunk/lib/gis/parser_standard_options.c
   grass/trunk/lib/gis/proj1.c
   grass/trunk/lib/gis/proj2.c
   grass/trunk/lib/gis/proj3.c
   grass/trunk/misc/m.measure/m.measure.html
   grass/trunk/misc/m.measure/main.c
Log:
libgis: better units handling
m.measure changed to use libgis fns


Modified: grass/trunk/include/gis.h
===================================================================
--- grass/trunk/include/gis.h	2010-03-05 14:42:31 UTC (rev 41296)
+++ grass/trunk/include/gis.h	2010-03-05 18:25:25 UTC (rev 41297)
@@ -61,9 +61,18 @@
 
 #define NEWLINE     '\n'
 
-#define METERS    1
-#define FEET      2
-#define DEGREES   3
+/*!
+  \brief List of units
+*/
+#define U_UNKNOWN       0
+#define U_ACRES		1
+#define U_HECTARES	2
+#define U_KILOMETERS	3
+#define U_METERS	4
+#define U_MILES		5
+#define U_FEET		6
+#define U_RADIANS	7
+#define U_DEGREES	8
 
 #define PROJECTION_XY  0
 #define PROJECTION_UTM 1
@@ -199,7 +208,9 @@
     G_OPT_F_SEP,		/* data field separator */
 
     G_OPT_C_FG,			/* foreground color */
-    G_OPT_C_BG			/* background color */
+    G_OPT_C_BG,			/* background color */
+    
+    G_OPT_M_UNITS,              /* units */
 } STD_OPT;
 
 /* Message format */

Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h	2010-03-05 14:42:31 UTC (rev 41296)
+++ grass/trunk/include/gisdefs.h	2010-03-05 18:25:25 UTC (rev 41297)
@@ -506,7 +506,6 @@
 
 /* proj2.c */
 int G__projection_units(int);
-const char *G__unit_name(int, int);
 const char *G__projection_name(int);
 
 /* proj3.c */
@@ -612,6 +611,12 @@
 /* trim_dec.c */
 void G_trim_decimal(char *);
 
+/* units.c */
+double G_units_to_meters_factor(int);
+double G_units_to_meters_factor_sq(int);
+const char *G_get_units_name(int, int, int);
+int G_units(const char *);
+
 /* user_config.c */
 #ifndef __MINGW32__
 char *G_rc_path(const char *, const char *);

Modified: grass/trunk/lib/gis/parser_standard_options.c
===================================================================
--- grass/trunk/lib/gis/parser_standard_options.c	2010-03-05 14:42:31 UTC (rev 41296)
+++ grass/trunk/lib/gis/parser_standard_options.c	2010-03-05 18:25:25 UTC (rev 41297)
@@ -3,7 +3,7 @@
  *
  * \brief GIS Library - Argument parsing functions (standard options)
  *
- * (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.
@@ -28,7 +28,7 @@
  * If an invalid parameter was specified a empty Option structure will
  * be returned (not NULL).
  *
- *  - general:
+ *  - database:
  *   - G_OPT_DB_WHERE
  *   - G_OPT_DB_COLUMN
  *   - G_OPT_DB_COLUMNS
@@ -67,9 +67,25 @@
  *   - G_OPT_V_MAPS
  *   - G_OPT_V_TYPE
  *   - G_OPT_V_FIELD
+ *   - G_OPT_V_FIELD_ALL
  *   - G_OPT_V_CAT
  *   - G_OPT_V_CATS
+ *   - G_OPT_V_ID
+ *   - G_OPT_V_IDS
+ * 
+ *  - files
+ *   - G_OPT_F_INPUT
+ *   - G_OPT_F_OUTPUT
+ *   - G_OPT_F_SEP
  *
+ *  - colors
+ *   - G_OPT_C_FG
+ *   - G_OPT_C_BG
+ *
+ *  - misc
+ *
+ *   - G_OPT_M_UNITS
+ *
  * \param opt type of Option struct to create
  *
  * \return pointer to an Option struct
@@ -450,6 +466,17 @@
 	Opt->description =
 	    _("Either a standard GRASS color, R:G:B triplet, or \"none\"");
 	break;
+
+	/* misc */
+    case G_OPT_M_UNITS:
+	Opt->key = "units";
+	Opt->type = TYPE_STRING;
+	Opt->required = NO;
+	Opt->multiple = NO;
+	Opt->options =
+	    "miles,feet,meters,kilometers,acres,hectares";
+	Opt->description = _("Units");
+	break;
     }
 
     return (Opt);

Modified: grass/trunk/lib/gis/proj1.c
===================================================================
--- grass/trunk/lib/gis/proj1.c	2010-03-05 14:42:31 UTC (rev 41296)
+++ grass/trunk/lib/gis/proj1.c	2010-03-05 18:25:25 UTC (rev 41297)
@@ -1,9 +1,9 @@
 /*!
   \file gis/proj1.c
  *
- * \brief GIS Library - Projection support
+ * \brief GIS Library - Projection support (window related)
  *
- * (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.
@@ -16,8 +16,9 @@
 /*!
   \brief Query cartographic projection
   
-  This routine returns a code indicating the projection for the active region.  The current
-  values are:
+  This routine returns a code indicating the projection for the active
+  region. The current values are:
+
    - PROJECTION_XY  0 - x,y (Raw imagery)
    - PROJECTION_UTM 1 - UTM   Universal Transverse Mercator
    - PROJECTION_SP  2 - State Plane (in feet)

Modified: grass/trunk/lib/gis/proj2.c
===================================================================
--- grass/trunk/lib/gis/proj2.c	2010-03-05 14:42:31 UTC (rev 41296)
+++ grass/trunk/lib/gis/proj2.c	2010-03-05 18:25:25 UTC (rev 41297)
@@ -1,9 +1,9 @@
 /*!
   \file gis/proj2.c
 
-  \brief GIS Library - Projection support
+  \brief GIS Library - Projection support (internal subroutines)
   
-  (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.
@@ -15,58 +15,36 @@
 #include <grass/glocale.h>
 
 /*!
-  \brief Get units code
+  \brief Get projection units code
 
-  \param n project code
+  \param n projection code
 
-  \retun units code
+  \return units code
+  \return -1 if not defined
 */
 int G__projection_units(int n)
 {
     switch (n) {
     case PROJECTION_XY:
-	return 0;
+	return U_UNKNOWN;
     case PROJECTION_UTM:
-	return METERS;
+	return U_METERS;
     case PROJECTION_SP:
-	return FEET;
+	return U_FEET;
     case PROJECTION_LL:
-	return DEGREES;
+	return U_DEGREES;
     default:
 	return -1;
     }
 }
 
 /*!
-  \brief Get units name
-
-  \param unit units code
-  \param plural plural form
-
-  \return units name
-*/
-const char *G__unit_name(int unit, int plural)
-{
-    switch (unit) {
-    case 0:
-	return plural ? "units" : "unit";
-    case METERS:
-	return plural ? "meters" : "meter";
-    case FEET:
-	return plural ? "feet" : "foot";
-    case DEGREES:
-	return plural ? "degrees" : "degree";
-    default:
-	return NULL;
-    }
-}
-
-/*!
   \brief Get projection name
   
   \param n projection code
 
   \return projection name
+  \return NULL on error
 */
 const char *G__projection_name(int n)
 {
@@ -76,7 +54,7 @@
     case PROJECTION_UTM:
 	return "UTM";
     case PROJECTION_SP:
-	return "State Plane";
+	return _("State Plane");
     case PROJECTION_LL:
 	return _("Latitude-Longitude");
     case PROJECTION_OTHER:

Modified: grass/trunk/lib/gis/proj3.c
===================================================================
--- grass/trunk/lib/gis/proj3.c	2010-03-05 14:42:31 UTC (rev 41296)
+++ grass/trunk/lib/gis/proj3.c	2010-03-05 18:25:25 UTC (rev 41297)
@@ -1,9 +1,9 @@
 /*!
   \file gis/proj3.c
 
-  \brief GIS Library - Projection support
+  \brief GIS Library - Projection support (database)
   
-  (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.
@@ -35,16 +35,16 @@
 }
 
 /*!
- * \brief Get database units
- *
- * Returns a string describing the database grid units. It returns a
- * plural form (eg. feet) if <i>plural</i> is true. Otherwise it
- * returns a singular form (eg. foot).
- *
- * \param plural plural form if true
- *
- * \return units name
- */
+  \brief Get database units name
+  
+  Returns a string describing the database grid units. It returns a
+  plural form (eg. 'feet') if <i>plural</i> is true. Otherwise it
+  returns a singular form (eg. 'foot').
+  
+  \param plural plural form if true
+  
+  \return units name
+*/
 const char *G_database_unit_name(int plural)
 {
     int n;
@@ -55,7 +55,7 @@
     case PROJECTION_UTM:
     case PROJECTION_LL:
     case PROJECTION_SP:
-	return G__unit_name(G__projection_units(n), plural);
+	return G_get_units_name(G__projection_units(n), plural, 0);
     }
 
     name = lookup_units(plural ? "units" : "unit");
@@ -65,16 +65,15 @@
     return name;
 }
 
-
 /*!
- * \brief Query cartographic projection
- *
- * Returns a pointer to a string which is a printable name for
- * projection code <i>proj</i> (as returned by G_projection). Returns
- * NULL if <i>proj</i> is not a valid projection.
- *
- * \return projection name
- */
+  \brief Query cartographic projection
+  
+  Returns a pointer to a string which is a printable name for
+  projection code <i>proj</i> (as returned by G_projection). Returns
+  NULL if <i>proj</i> is not a valid projection.
+  
+  \return projection name
+*/
 const char *G_database_projection_name(void)
 {
     int n;
@@ -96,14 +95,14 @@
 }
 
 /*!
- * \brief Conversion to meters
- *
- * Returns a factor which converts the grid unit to meters (by
- * multiplication). If the database is not metric (eg. imagery) then
- * 0.0 is returned.
- *
- * \return value
- */
+  \brief Conversion to meters
+  
+  Returns a factor which converts the grid unit to meters (by
+  multiplication). If the database is not metric (eg. imagery) then
+  0.0 is returned.
+  
+  \return value
+*/
 double G_database_units_to_meters_factor(void)
 {
     const char *unit;
@@ -139,15 +138,15 @@
 }
 
 /*!
- * \brief Get datum name for database
- *
- * Returns a pointer to the name of the map datum of the current
- * database. If there is no map datum explicitely associated with the
- * acutal database, the standard map datum WGS84 is returned, on error
- * a NULL pointer is returned.
- *
- * \return datum name
- */
+  \brief Get datum name for database
+  
+  Returns a pointer to the name of the map datum of the current
+  database. If there is no map datum explicitely associated with the
+  acutal database, the standard map datum WGS84 is returned, on error
+  a NULL pointer is returned.
+  
+  \return datum name
+*/
 const char *G_database_datum_name(void)
 {
     const char *name;

Added: grass/trunk/lib/gis/units.c
===================================================================
--- grass/trunk/lib/gis/units.c	                        (rev 0)
+++ grass/trunk/lib/gis/units.c	2010-03-05 18:25:25 UTC (rev 41297)
@@ -0,0 +1,234 @@
+/*!
+  \file gis/units.c
+
+  \brief GIS Library - Units management and conversion
+  
+  (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.
+  
+  \author Original author CERL
+  \author Adopted for libgis by Martin Landa <landa.martin gmail.com> (2010)
+ */
+
+#include <string.h>
+
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+/*!
+  \brief Units conversion to meters
+
+  Units codes (gis.h):
+   - U_METERS
+   - U_KILOMETERS
+   - U_MILES
+   - U_FEET
+
+  Returns a factor which converts the grid unit to meters (by
+  multiplication).
+ 
+  \param units units code
+
+  \return factor
+*/
+double G_units_to_meters_factor(int units)
+{
+    switch (units) {
+    case U_METERS:
+	return 1.0;
+	break;
+	
+    case U_KILOMETERS:
+	return 1.0e-3;
+	break;
+	
+    case U_MILES:
+	return 6.21371192237334e-4;	        /*  1 / (0.0254 * 12 * 5280)    */
+	break;
+	
+    case U_FEET:
+	return 3.28083989501312;	        /*  1 / (0.0254 * 12)    */
+	break;
+	
+    default:
+	return 1.0;
+	break;
+    }
+ 
+    return 1.0;
+}
+
+/*!
+  \brief Units conversion to square meters
+
+  Units codes (gis.h):
+   - U_METERS
+   - U_KILOMETERS
+   - U_ACRES
+   - U_HECTARES
+   - U_MILES
+   - U_FEET
+
+  Returns a factor which converts the grid unit to square meters (by
+  multiplication).
+ 
+  \param units units code
+
+  \return factor
+*/
+double G_units_to_meters_factor_sq(int units)
+{
+    switch (units) {
+    case U_METERS:
+	return 1.0;
+	break;
+	
+    case U_KILOMETERS:
+	return 1.0e-6;
+	break;
+	
+    case U_ACRES:
+	return 2.47105381467165e-4;	/* 640 acres in a sq mile */
+	break;
+	
+    case U_HECTARES:
+	return 1.0e-4;
+	break;
+	
+    case U_MILES:
+	return 3.86102158542446e-7;	/*  1 / (0.0254 * 12 * 5280)^2  */
+	break;
+	
+    case U_FEET:
+	return 10.7639104167097;	        /*  1 / (0.0254 * 12)^2  */
+	break;
+	
+    default:
+	return 1.0;
+	break;
+    }
+ 
+    return 1.0;
+}
+
+/*!
+  \brief Get localized units name
+
+  Units codes (gis.h):
+   - U_METERS
+   - U_KILOMETERS
+   - U_ACRES
+   - U_HECTARES
+   - U_MILES
+   - U_FEET
+
+  \param units units code
+  \param plural plural form if true
+  \param square area units if true
+
+  \return units name
+  \return NULL if units not found
+*/
+const char *G_get_units_name(int units, int plural, int square)
+{
+    switch (units) {
+    case U_UNKNOWN:
+	if (square)
+	    return plural ? _("square units") : _("square unit");
+	else
+	    return plural ? _("units") : _("unit");
+	break;
+	  
+    case U_METERS:
+	if (square) 
+	    return plural ? _("square meters") : _("square meter");
+	else
+	    return plural ? _("meters") : _("meter");
+	break;
+	
+    case U_KILOMETERS:
+	if (square)
+	    return plural ? _("square kilometers") : _("square kilometer");
+	else
+	    return plural ? _("kilometers") : _("kilometer");
+	break;
+	
+    case U_ACRES:
+	if (square)
+	    return plural ? _("acres") : _("acre");
+	else
+	    return G_get_units_name(G_units(G_database_unit_name(1)),
+				    plural, square);
+	break;
+	
+    case U_HECTARES:
+	if (square)
+	    return plural ? _("hectares") : _("hectare");
+	else
+	    return G_get_units_name(G_units(G_database_unit_name(1)),
+				    plural, square);
+	break;
+	
+    case U_MILES:
+	if (square)
+	    return plural ? _("square miles") : _("square mile");
+	else
+	    return plural ? _("miles") : _("mile");
+	break;
+	
+    case U_FEET:
+	if (square)
+	    return plural ? _("square feet") : _("square foot");
+	else
+	    return plural ? _("feet") : _("foot");
+	break;
+    }
+    
+    return NULL;
+}
+
+/*!
+  \brief Get units code by name
+
+  Units codes (gis.h):
+   - U_METERS
+   - U_KILOMETERS
+   - U_ACRES
+   - U_HECTARES
+   - U_MILES
+   - U_FEET
+
+  \param units_name units name (singular or plural form)
+
+  \return units code
+  \return U_UNKNOWN if not found
+*/
+int G_units(const char *units_name)
+{
+    if (units_name == NULL) {
+	return G_units(G_database_unit_name(1));
+    }
+	
+    if (strcasecmp(units_name, "meter") == 0 ||
+	strcasecmp(units_name, "meters") == 0)
+	return U_METERS;
+    else if (strcasecmp(units_name, "kilometer") == 0 ||
+	     strcasecmp(units_name, "kilometers") == 0)
+	return U_KILOMETERS;
+    else if (strcasecmp(units_name, "acre") == 0 ||
+	     strcasecmp(units_name, "acres") == 0)
+	return U_ACRES;
+    else if (strcasecmp(units_name, "hectare") == 0 ||
+	     strcasecmp(units_name, "hectares") == 0)
+	return U_HECTARES;
+    else if (strcasecmp(units_name, "mile") == 0 ||
+	     strcasecmp(units_name, "miles") == 0)
+	return U_MILES;
+    else if (strcasecmp(units_name, "foot") == 0 ||
+	     strcasecmp(units_name, "feet") == 0)
+	return U_FEET;
+
+    return U_UNKNOWN;
+}


Property changes on: grass/trunk/lib/gis/units.c
___________________________________________________________________
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Modified: grass/trunk/misc/m.measure/m.measure.html
===================================================================
--- grass/trunk/misc/m.measure/m.measure.html	2010-03-05 14:42:31 UTC (rev 41296)
+++ grass/trunk/misc/m.measure/m.measure.html	2010-03-05 18:25:25 UTC (rev 41297)
@@ -1,20 +1,17 @@
-<H2>DESCRIPTION</H2>
+<h2>DESCRIPTION</h2>
 
-<EM>m.measure</EM> provides the user with a
-way to measure the lengths and areas of lines and polygons.
-Areas can be stated in hectares, square miles, square meters and
-square kilometers.
+<em>m.measure</em> provides the user with a way to measure the lengths
+and areas of lines and polygons. Areas can be stated in acres,
+hectares, square miles, square meters and square kilometers.
 
-<H2>AUTHORS</H2>
+<h2>AUTHORS</h2>
 
 Glynn Clements
-<BR>
-Derived from d.measure, by:
-<BR>
-James Westervelt, 
-<BR>
-Michael Shapiro, <BR>
-U.S. Army Construction Engineering 
-Research Laboratory
+<br>
+Some updates by Martin Landa, CTU in Prague, Czech Republic
+<br><br>
+Derived from d.measure by James Westervelt, Michael Shapiro, U.S. Army
+Construction Engineering Research Laboratory
 
-<p><i>Last changed: $Date: 2008-08-15 07:16:42 +0100 (Fri, 15 Aug 2008) $</i>
+<p>
+<i>Last changed: $Date$</i>

Modified: grass/trunk/misc/m.measure/main.c
===================================================================
--- grass/trunk/misc/m.measure/main.c	2010-03-05 14:42:31 UTC (rev 41296)
+++ grass/trunk/misc/m.measure/main.c	2010-03-05 18:25:25 UTC (rev 41297)
@@ -13,8 +13,9 @@
  *               Glynn Clements <glynn gclements.plus.com>, 
  *               Hamish Bowman <hamish_b yahoo.com>, 
  *               Jan-Oliver Wagner <jan intevation.de>
- * PURPOSE:      distance and area measurement
- * COPYRIGHT:    (C) 1999-2006,2010 by the GRASS Development Team
+ *               Martin Landa <landa.martin gmail.com>
+ * PURPOSE:      Distance and area measurement
+ * COPYRIGHT:    (C) 1999-2006, 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
@@ -30,11 +31,13 @@
 int main(int argc, char **argv)
 {
     struct GModule *module;
-    struct Option *coords;
+    struct Option *coords, *units;
+    struct Flag *shell;
+
     double *x, *y;
-    double length, area;
-    int npoints;
-    int i;
+    double length, area, f, sq_f;
+    int i, npoints;
+    const char *units_name, *sq_units_name;
 
     /* Initialize the GIS calls */
     G_gisinit(argv[0]);
@@ -52,6 +55,14 @@
     coords->multiple = YES;
     coords->key_desc = "x,y";
 
+    units = G_define_standard_option(G_OPT_M_UNITS);
+    units->label = _("Units");
+    units->description = _("Default: location map units");
+    
+    shell = G_define_flag();
+    shell->key = 'g';
+    shell->description = _("Shell script style");
+    
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
@@ -67,22 +78,40 @@
 	y[i] = atof(coords->answers[2*i+1]);
     }
 
+    /* determine units */
+    units_name    = G_get_units_name(G_units(units->answer), 1, 0);
+    sq_units_name = G_get_units_name(G_units(units->answer), 1, 1);
+    
+    f    = G_units_to_meters_factor(G_units(units->answer));
+    sq_f = G_units_to_meters_factor_sq(G_units(units->answer));
+    
+    G_debug(1, "using '%s (%f) %s (%f)'",
+	    units_name, f, sq_units_name, sq_f);
+    
     G_begin_distance_calculations();
     length = 0;
     for (i = 1; i < npoints; i++)
 	length += G_distance(x[i-1], y[i-1], x[i], y[i]);
 
-    printf("LEN:   %10.2f meters\n", length);
-    printf("LEN:   %10.4f kilometers\n", length / 1e3);
-    printf("LEN:   %10.4f miles\n", length / 1609.344);
-
+    if (shell->answer) {
+	printf("units=%s,%s\n", units_name, sq_units_name);
+	/* length */
+	printf("length=%.6f\n", f * length);
+    }
+    else {
+	printf("%-8s %10.6f %s\n", _("Length:"), f * length, units_name);
+	       
+    }
+    
     if (npoints > 3) {
 	G_begin_polygon_area_calculations();
 	area = G_area_of_polygon(x, y, npoints);
-	printf("AREA:  %10.2f square meters\n", area);
-	printf("AREA:  %10.2f hectares\n", area / 1e4);
-	printf("AREA:  %10.4f square kilometers\n", area / 1e6);
-	printf("AREA:  %10.4f square miles\n", area / 2589988.11);
+	if (shell->answer) {
+	    printf("area=%.6f\n", sq_f * area);
+	}
+	else {
+	    printf("%-8s %10.6f %s\n", _("Area:"), sq_f * area, sq_units_name);
+	}
     }
 
     exit(EXIT_SUCCESS);



More information about the grass-commit mailing list