-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp.B3.d.fill_polygons_nulls.py
70 lines (67 loc) · 2.75 KB
/
p.B3.d.fill_polygons_nulls.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
#!/usr/bin/env python
#
############################################################################
#
# MODULE : p.B3.d.fill_polygons_null.py
# AUTHOR(S) : Sanzana P. 01/06/2015
#
# PURPOSE : To fill polygons with null values in the statistic step
#
# 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/>.
#
#
#############################################################################
#
#
#
#Fill null polygons values
import grass.script as grass
#####Display the mapset content#####
env = grass.gisenv()
print env
vectors = grass.read_command("g.list", type='vect')
print vectors
polygons=raw_input("Please enter the name of the polygons : ")
columns=grass.read_command("v.info", map=polygons, flags='c')
print columns
col_fill=raw_input("Please enter the name of the column that you desire to fill: ")
raster = grass.read_command("g.list", type='rast')
print raster
dem=raw_input("Please enter the name of the dem to fill column values : ")
grass.run_command("g.region",rast=dem)
grass.run_command("v.db.addcolumn.py", map=polygons, column="E double,N double")
grass.run_command("v.to.db", map=polygons, option='coor', col='E, N')
condition=col_fill+" IS NULL"
grass.run_command("v.extract", input=polygons, output='polygons_temp', where=condition, overwrite=True)
list_column=grass.read_command("v.db.select",map='polygons_temp',col='cat',flags='c')
for i in list_column.splitlines():
coordinates = grass.read_command("v.db.select",map='polygons_temp', where="cat=%s"%i, col='E,N', flags='c', separator=" ")
c_1=coordinates.rsplit()
E=c_1[0]
N=c_1[1]
centr=E+","+N
print "Filling Polygon Cat: "+ str(i)
print "Coordinates centroid: " + centr
value=grass.read_command("r.what", map=dem, separator=' ', coordinates=centr)
print value
v_1=value.rsplit()
v_3=v_1[2]
print v_3
grass.run_command("v.db.update",map=polygons,col=col_fill,value=v_3,where="cat=%s"%i)
grass.run_command("v.db.dropcolumn.py", map=polygons, column='N')
grass.run_command("v.db.dropcolumn.py", map=polygons, column='E')
grass.run_command("g.remove", flags='f', vect='polygons_temp', overwrite=True)