Michael,
I've attached a patch to your very useful v.nn.py addon script. The
patch provides the following:
- a change of option names from input1,2 to 'from' and 'to'
- the possibility to provide a vector map with polygons giving the
contours of the study area for which to calculate the nearest neighbor
coefficient - the script just gets the total of all areas and all
perimeters in the given map
- the possibility to provide numerical values for area and perimeter
Even though some people argue that the absolute value of the coefficient
does not mean much and that the exact size of the study area does not
have an influence on the relative values of the coefficient, I think it
is a plus to have the possibility to circumscribe the area. Especially
since in its current state the module gets the area and perimenter from
the current region and as this is a vector module most people won't even
think about region settings. (Running the module in the default n=1 s=0
w=0 e=1 region gives very weird coefficient values.
Up to you to decide whether you want to add this into your script.
BTW: the script works perfectly in grass7 as well.
Moritz
--- /data/GRASS/SCRIPTS/v.nn.py 2013-09-13 14:55:37.000000000 +0200
+++ v.nn2.py 2013-09-14 18:07:33.000000000 +0200
@@ -29,7 +29,7 @@
#%End
#%option
-#% key: input1
+#% key: from
#% type: string
#% gisprompt: old,vector,vector
#% description: Vector points base file
@@ -37,15 +37,38 @@
#%end
#%option
-#% key: input2
+#% key: to
#% type: string
#% gisprompt: old,vector,vector
#% description: Vector points file to compare with base file
#% required : no
#%end
+#%option
+#% key: areamap
+#% type: string
+#% gisprompt: old,vector,vector
+#% description: Vector polygon file with study zone
+#% required : no
+#%end
+
+#%option
+#% key: area
+#% type: double
+#% description: Area of the study zone
+#% required : no
+#%end
+
+#%option
+#% key: perimeter
+#% type: double
+#% description: Perimeter of the study zone
+#% required : no
+#%end
+
+
import math
-import os
+import os,sys
if not os.environ.has_key("GISBASE"):
print "You must be in GRASS GIS to run this program."
@@ -58,16 +81,19 @@
sys.exit(_("No GRASS-python library found"))
-def NNcalc(map1, map2):
+def NNcalc(map1, map2, area, perimeter):
"""calculates nearest neighbor coefficient for a single map
or for two maps"""
- ## Calculate region perimeter and area (rectangular area only)
- ns_extent = float(grass.parse_command('g.region', flags='eg')['ns_extent'])
- ew_extent = float(grass.parse_command('g.region', flags='eg')['ew_extent'])
- area = ns_extent * ew_extent
- perimeter = (2 * ns_extent) + (2 * ew_extent)
+ if area == '' or perimeter == '':
+ ## Calculate region perimeter and area (rectangular area only)
+ ns_extent = float(grass.parse_command('g.region', flags='eg')['ns_extent'])
+ ew_extent = float(grass.parse_command('g.region', flags='eg')['ew_extent'])
+ area = ns_extent * ew_extent
+ perimeter = (2 * ns_extent) + (2 * ew_extent)
+ grass.message(_("Using area = %g and perimeter = %g.") % (area, perimeter))
+
## Calculates the number of points in each vector points file
npts1 = int(grass.parse_command('v.info', map=map1, flags='t')['points'])
npts2 = int(grass.parse_command('v.info', map=map2, flags='t')['points'])
@@ -97,19 +123,36 @@
def main():
"""Main method for script"""
- map1 = options['input1']
- map2 = options['input2']
+ map1 = options['from']
+ map2 = options['to']
+ areamap = options['areamap']
+ area = options['area']
+ perimeter = options['perimeter']
+ if (area != '' or perimeter != '') and (areamap != ''):
+ grass.message(_("If both area/perimeter and areamap are given, areamap has precedence"))
+ if areamap == '' and ((area != '' and perimeter == '') or (area == '' and perimeter != '')):
+ sys.exit(_("If one of area or perimeter is given, the other must be given as well"))
+
if map2 == '' or map1 == map2:
# Single map nearest neighbor for clustering analysis
map2 = map1
- header = 'Nearest neighbor coeficient for points in %s: ' % map1
+ header = 'Nearest neighbor coefficient for points in %s: ' % map1
else:
# Nearest neighbor for correspondence between two point maps
- header = 'Nearest neighbor coeficient of association between points in %s and %s: ' % (map2, map1)
+ header = 'Nearest neighbor coefficient of association between points in %s and %s: ' % (map2, map1)
+
+ if areamap != '':
+ res = grass.read_command('v.to.db', flags='pc', map=areamap, option='area')
+ area = float(res.splitlines()[-1].split('|')[1])
+ res = grass.read_command('v.to.db', flags='pc', map=areamap, option='perimeter')
+ perimeter = float(res.splitlines()[-1].split('|')[1])
+ elif area != '' and perimeter != '':
+ area = float(area)
+ perimeter = float(perimeter)
- NN = NNcalc(map1, map2)
+ NN = NNcalc(map1, map2, area, perimeter)
# Display results in output window
print '\n%s %G\n' % (header, NN)
_______________________________________________
grass-dev mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-dev