-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp.B7.shape_factors.py
112 lines (112 loc) · 5.44 KB
/
p.B7.shape_factors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
#
############################################################################
#
# MODULE : p.B7.shape_factors.py
# AUTHOR(S) : Sanzana P. 01/06/2015
#
# PURPOSE : To estimate the principal shape factors for polygons:
# area, perimeter, compact,convexity,solidity,formfactor
#
# COPYRIGHT : IRSTEA-UC-UCH
# This file is part of GeoPUMMA
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
#
#############################################################################
#
#
#
import grass.script as grass
env = grass.gisenv()
print env
vectors = grass.read_command("g.list", type='vect')
print vectors
polygons=raw_input("Please enter the name of the polygon map : ")
#Add new colums
grass.run_command("v.db.addcol",map=polygons,col="area double")
grass.run_command("v.db.addcol",map=polygons,col="perimeter double")
grass.run_command("v.db.addcol",map=polygons,col="solidity double")
grass.run_command("v.db.addcol",map=polygons,col="convexity double")
grass.run_command("v.db.addcol",map=polygons,col="compact double")
grass.run_command("v.db.addcol",map=polygons,col="formfactor double")
#Calculate Area, Perimeter y Compactness with v.to.db function
grass.run_command("v.to.db", map=polygons,option='area',col='area')
grass.run_command("v.to.db", map=polygons,option='perimeter',col='perimeter')
grass.run_command("v.to.db", map=polygons,option='compact',col='compact')
polygons_count = grass.read_command("v.db.select",map=polygons,col='cat',flags='c')
grass.run_command("g.region", vect=polygons)
#Calculating solidity, convexity,roundness,formfactor
for i in polygons_count.splitlines():
print "WORKING ON POLYGON CAT = " + str(i)
grass.run_command("v.extract",input=polygons,output='out_poly_1',where="cat=%s"%i,overwrite=True)
area=grass.read_command("v.db.select",map=polygons,col='area',flags='c',where="cat=%s"%i)
area1=area.rsplit()
area2=area1[0]
peri=grass.read_command("v.db.select",map=polygons,col='perimeter',flags='c',where="cat=%s"%i)
peri1=peri.rsplit()
peri2=peri1[0]
#FORM FACTOR
import math
form_fact=4*4*float(area2)/(float(peri2)*float(peri2))
grass.run_command("v.db.update",map=polygons,col='formfactor',value=form_fact,where="cat=%s"%i)
#CONVEX HULL
grass.run_command("g.region", vect=polygons)
points = grass.read_command("v.to.points",input='out_poly_1',output='points_temp_0',flags='v', overwrite=True)
grass.run_command("v.hull", input='points_temp_0', output='out_poly_hull', overwrite=True, flags='a')
grass.run_command("v.db.addtable",map='out_poly_hull')
grass.run_command("v.db.addcol",map='out_poly_hull',col="area double")
grass.run_command("v.db.addcol",map='out_poly_hull',col="perimeter double")
grass.run_command("v.to.db", map='out_poly_hull',option='area',col='area')
grass.run_command("v.to.db", map='out_poly_hull',option='perimeter',col='perimeter')
#Solidity and Convexity
harea=grass.read_command("v.db.select",map='out_poly_hull',col='area',flags='c')
harea1=harea.rsplit()
harea2=harea1[0]
print "harea 2"+harea2
hperi=grass.read_command("v.db.select",map='out_poly_hull',col='perimeter',flags='c')
hperi1=hperi.rsplit()
hperi2=hperi1[0]
print "hperi 2"+hperi2
soli=float(area2)/float(harea2)
conv=float(hperi2)/float(peri2)
print "convexity = " + str(conv)
print "area "+str(area2)
print "harea "+str(harea2)
#checking if it is necessary correct when convexity area is less than polygon area
if float(harea2)<float(area2):
points = grass.read_command("v.to.points",input='out_poly_1',output='points_temp_0',flags='vi',dmax='0.1', overwrite=True)
grass.run_command("v.hull", input='points_temp_0', output='out_poly_hull', overwrite=True, flags='a')
grass.run_command("v.db.addtable",map='out_poly_hull')
grass.run_command("v.db.addcol",map='out_poly_hull',col="area double")
grass.run_command("v.db.addcol",map='out_poly_hull',col="perimeter double")
grass.run_command("v.to.db", map='out_poly_hull',option='area',col='area')
grass.run_command("v.to.db", map='out_poly_hull',option='perimeter',col='perimeter')
#Solidity and Convexity
harea=grass.read_command("v.db.select",map='out_poly_hull',col='area',flags='c')
harea1=harea.rsplit()
harea2=harea1[0]
print "harea 2"+harea2
hperi=grass.read_command("v.db.select",map='out_poly_hull',col='perimeter',flags='c')
hperi1=hperi.rsplit()
hperi2=hperi1[0]
print "hperi 2"+hperi2
soli=float(area2)/float(harea2)
conv=float(hperi2)/float(peri2)
print "convexity = " + str(conv)
else:
print conv
grass.run_command("v.db.update",map=polygons,col='solidity',value=soli,where="cat=%s"%i)
grass.run_command("v.db.update",map=polygons,col='convexity',value=conv,where="cat=%s"%i)