Skip to content

Commit 34ca98f

Browse files
authoredJul 19, 2016
Merge pull request #3164 from NINAnor/add_grass_tools
New modules for the GRASS plugin
2 parents 3142c84 + 3608799 commit 34ca98f

26 files changed

+1150
-3
lines changed
 

‎src/plugins/grass/modules/default.qgc

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<grass name="r.in.gdal"/>
3939
<grass name="r.in.gdal.loc"/>
4040
<grass name="r.in.xyz"/>
41+
<grass name="r.in.lidar" version_min="7"/>
4142
<grass name="r.in.ascii"/>
4243
<grass name="r.in.poly"/>
4344
<grass name="r.in.bin"/>
@@ -48,6 +49,7 @@
4849
<grass name="r.in.srtm"/>
4950
<grass name="r.in.aster"/>
5051
<grass name="r.in.wms"/>
52+
<grass name="r.unpack" version_min="7"/>
5153
</section>
5254
</section>
5355
<section label="Import vector into GRASS">
@@ -56,6 +58,7 @@
5658
<grass name="v.in.ogr.loc"/>
5759
<grass name="v.in.ogr.all"/>
5860
<grass name="v.in.ogr.all.loc"/>
61+
<grass name="v.in.lidar" version_min="7"/>
5962
<grass name="v.in.dxf"/>
6063
<grass name="v.in.e00"/>
6164
<grass name="v.in.ascii"/>
@@ -65,6 +68,7 @@
6568
<grass name="v.in.gpsbabel" version_max="6.4"/>
6669
<grass name="v.in.geonames" version_min="6.4"/>
6770
<grass name="v.in.gns" version_max="6.4"/>
71+
<grass name="v.unpack" version_min="7"/>
6872
</section>
6973
<section label="Import from database into GRASS">
7074
<grass name="db.in.ogr"/>
@@ -91,6 +95,7 @@
9195
<grass name="r.out.tiff" version_max="6.4"/>
9296
<grass name="r.out.vrml"/>
9397
<grass name="r.out.vtk"/>
98+
<grass name="r.pack" version_min="7"/>
9499
</section>
95100
<section label="Export vector from GRASS">
96101
<grass name="v.out.ogr"/>
@@ -102,6 +107,7 @@
102107
<grass name="v.out.pov"/>
103108
<grass name="v.out.svg"/>
104109
<grass name="v.out.vtk"/>
110+
<grass name="v.pack" version_min="7"/>
105111
</section>
106112
<section label="Export vector table from GRASS to database format">
107113
<grass name="db.out.ogr" version_min="6.4"/>
@@ -174,6 +180,7 @@
174180
<grass name="r.colors.rast"/>
175181
<grass name="r.colors.stddev" version_min="6.4"/>
176182
<grass name="r.blend"/>
183+
<grass name="r.shade" version_min="7"/>
177184
<grass name="r.composite"/>
178185
<grass name="r.his"/>
179186
</section>
@@ -199,6 +206,8 @@
199206
<grass name="r.series"/>
200207
<grass name="r.patch"/>
201208
<grass name="r.statistics"/>
209+
<grass name="r.stats.zonal" version_min="7"/>
210+
<grass name="r.stats.quantile" version_min="7"/>
202211
</section>
203212
<section label="Solar and irradiation model">
204213
<grass name="r.sunmask.position"/>
@@ -262,6 +271,8 @@
262271
<grass name="r.surf.gauss"/>
263272
<grass name="r.plane"/>
264273
<grass name="r.surf.random"/>
274+
<grass name="r.latitude"/>
275+
<grass name="r.longitude"/>
265276
</section>
266277
<section label="Generate vector contour lines">
267278
<grass name="r.contour"/>
@@ -295,7 +306,8 @@
295306
<grass name="r.univar"/>
296307
<grass name="r.covar"/>
297308
<grass name="r.regression.line"/>
298-
<grass name="r.coin"/>
309+
<grass name="r.regression.multi" version_min="7"/>
310+
<grass name="r.coin"/>
299311
<grass name="r.distance"/>
300312
</section>
301313
</section>
@@ -405,6 +417,7 @@
405417
<section label="Reclass category values">
406418
<grass name="v.reclass.file"/>
407419
<grass name="v.reclass.attr"/>
420+
<grass name="v.class.mlpy.qgis" version_min="7.0" version_max="7.0"/>
408421
</section>
409422
</section>
410423
<section label="Work with vector points">
@@ -446,6 +459,7 @@
446459

447460
<section label="Imagery">
448461
<section label="Develop images and group">
462+
<grass name="i.group"/>
449463
<grass name="i.image.mosaic"/>
450464
</section>
451465
<section label="Manage image colors">
@@ -460,14 +474,18 @@
460474
<grass name="i.zc"/>
461475
<grass name="r.mfilter"/>
462476
</section>
463-
<section label="Tassled cap vegetation index">
477+
<section label="Vegetation indices">
478+
<grass name="i.albedo" version_min="7"/>
479+
<grass name="i.biomass" version_min="7"/>
464480
<grass name="i.tasscap4"/>
465481
<grass name="i.tasscap5"/>
466482
<grass name="i.tasscap7"/>
467483
<grass name="i.tasscap8" version_min="7.0"/>
468484
<grass name="i.tasscap.modis" version_min="7.0"/>
485+
<grass name="i.vi" version_min="7.0"/>
469486
</section>
470487
<section label="Transform image">
488+
<grass name="i.segment" version_min="7"/>
471489
<grass name="i.fft"/>
472490
<grass name="i.ifft"/>
473491
</section>
@@ -532,6 +550,12 @@
532550
</section>
533551
<section label="Sampling" version_min="7.0">
534552
<grass name="t.sample" version_min="7.0"/>
553+
<grass name="t.rast.what.coordinates.qgis" version_min="7.0" version_max="7.0"/>
554+
<grass name="t.rast.what.coordinates" version_min="7.1"/>
555+
<grass name="t.rast.what.file.qgis" version_min="7.0" version_max="7.0"/>
556+
<grass name="t.rast.what.file" version_min="7.1"/>
557+
<grass name="t.rast.what.points.qgis" version_min="7.0" version_max="7.0"/>
558+
<grass name="t.rast.what.points" version_min="7.1"/>
535559
<grass name="t.vect.what.strds" version_min="7.0"/>
536560
<grass name="t.vect.observe.strds" version_min="7.0"/>
537561
</section>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Computes broad band albedo from surface reflectance" module="i.albedo">
5+
<flag key="m" answer="off"/>
6+
<flag key="n" answer="off"/>
7+
<flag key="l" answer="off"/>
8+
<flag key="a" answer="off"/>
9+
<flag key="c" answer="off"/>
10+
<flag key="d" answer="off"/>
11+
<option key="input"/>
12+
<option key="output"/>
13+
</qgisgrassmodule>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Computes biomass growth, precursor of crop yield calculation" module="i.biomass">
5+
<option key="fpar"/>
6+
<option key="lightuse_efficiency"/>
7+
<option key="latitude"/>
8+
<option key="dayofyear"/>
9+
<option key="transmissivity_singleway"/>
10+
<option key="water_availability"/>
11+
<option key="output"/>
12+
</qgisgrassmodule>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Computes emissivity from NDVI, generic method for sparse land" module="i.emissivity">
5+
<option key="input"/>
6+
<option key="output"/>
7+
</qgisgrassmodule>

‎src/plugins/grass/modules/i.group.qgm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Creates, edits, and lists groups of imagery data." module="i.group">
5+
<option key="input" multiple="yes"/>
6+
<option key="group"/>
7+
<option key="subgroup"/>
8+
<flag key="r" answer="off" advanced="yes"/>
9+
<flag key="l" answer="off" advanced="yes"/>
10+
<flag key="s" answer="off" advanced="yes"/>
11+
<flag key="g" answer="off" advanced="yes"/>
12+
</qgisgrassmodule>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Identifies segments (objects) from imagery data." module="i.segment">
5+
<option key="group"/>
6+
<option key="output"/>
7+
<option key="threshold"/>
8+
<option key="method"/>
9+
<option key="similarity" advanced="yes"/>
10+
<option key="minsize" advanced="yes"/>
11+
<option key="memory" advanced="yes"/>
12+
<option key="iterations" advanced="yes"/>
13+
<option key="seeds" advanced="yes"/>
14+
<option key="bounds" advanced="yes"/>
15+
<option key="goodness" advanced="yes"/>
16+
<flag key="d" answer="off" advanced="yes"/>
17+
<flag key="w" answer="off" advanced="yes"/>
18+
</qgisgrassmodule>

‎src/plugins/grass/modules/i.vi.qgm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Calculates different types of vegetation indices" module="i.vi">
5+
<option key="viname"/>
6+
<option key="output"/>
7+
<option key="red"/>
8+
<option key="nir"/>
9+
<option key="green"/>
10+
<option key="blue"/>
11+
<option key="band5"/>
12+
<option key="band7"/>
13+
<option key="soil_line_slope" advanced="yes"/>
14+
<option key="soil_line_intercept" advanced="yes"/>
15+
<option key="soil_noise_reduction" advanced="yes"/>
16+
<option key="storage_bit" advanced="yes"/>
17+
</qgisgrassmodule>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Creates a raster map from LAS LiDAR points using univariate statistics." module="r.in.lidar">
5+
<file key="input" label="LiDAR input files in LAS format (*.las or *.laz)" type="old"/>
6+
<option key="output"/>
7+
<option key="method"/>
8+
<option key="type" advanced="yes"/>
9+
<option key="return_filter" advanced="yes"/>
10+
<option key="zrange" advanced="yes"/>
11+
<option key="zscale" advanced="yes"/>
12+
<option key="percent" advanced="yes"/>
13+
<option key="pth" advanced="yes"/>
14+
<option key="trim" advanced="yes"/>
15+
<flag key="i" answer="off"/>
16+
</qgisgrassmodule>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Creates a latitude raster map" module="r.latlong">
5+
<option key="input"/>
6+
<option key="output" label="Name of the output latitude raster map"/>
7+
</qgisgrassmodule>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Creates a longitude raster map" module="r.latlong">
5+
<flag key="l" answer="on" hidden="yes"/>
6+
<option key="input"/>
7+
<option key="output" label="Name of the output longitude raster map"/>
8+
</qgisgrassmodule>

‎src/plugins/grass/modules/r.pack.qgm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Exports a raster map as GRASS GIS specific archive file" module="r.pack">
5+
<option key="input"/>
6+
<file key="output" type="new"/>
7+
<flag key="c" answer="off" advanced="yes"/>
8+
</qgisgrassmodule>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Calculates multiple linear regression from raster maps" module="r.regression.multi">
5+
<flag key="g" answer="off"/>
6+
<option key="mapy"/>
7+
<option key="mapx"/>
8+
<file key="output" label="Output file for regression coefficients" type="new"/>
9+
<option key="residuals"/>
10+
<option key="estimates"/>
11+
</qgisgrassmodule>

‎src/plugins/grass/modules/r.shade.qgm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Drapes a color raster over an shaded relief or aspect map" module="r.shade">
5+
<flag key="c" answer="off" advanced="yes"/>
6+
<option key="shade"/>
7+
<option key="color"/>
8+
<option key="output"/>
9+
<option key="brighten" advanced="yes"/>
10+
<option key="bgcolor" advanced="yes"/>
11+
</qgisgrassmodule>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Compute category quantiles using two passes." module="r.stats.quantile">
5+
<flag key="p" answer="off" advanced="yes"/>
6+
<flag key="r" answer="off" advanced="yes"/>
7+
<option key="base"/>
8+
<option key="cover"/>
9+
<option key="quantiles"/>
10+
<option key="percentiles"/>
11+
<option key="bins" advanced="yes"/>
12+
<option key="output"/>
13+
</qgisgrassmodule>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Calculates category or object oriented statistics" module="r.stats.zonal">
5+
<flag key="c" answer="off" advanced="yes"/>
6+
<flag key="r" answer="off" advanced="yes"/>
7+
<option key="base"/>
8+
<option key="cover"/>
9+
<option key="method"/>
10+
<option key="output"/>
11+
</qgisgrassmodule>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Imports a raster map as GRASS GIS specific archive file (packed with r.pack) a." module="r.unpack">
5+
<file key="input" type="old"/>
6+
<option key="output"/>
7+
<flag key="o" answer="off" advanced="yes"/>
8+
</qgisgrassmodule>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Sample a space time raster dataset at specific coordinates and write the output to file using different layouts" module="t.rast.what.qgis.py">
5+
<flag key="n" answer="on"/>
6+
<option key="coordinates"/>
7+
<option key="strds"/>
8+
<file key="output" label="Name for the output file" type="new"/>
9+
<option key="layout"/>
10+
<option key="where" advanced="yes" label="Temporal WHERE conditions without 'where' keyword"/>
11+
<option key="order" advanced="yes"/>
12+
<option key="nprocs" advanced="yes"/>
13+
<option key="separator" advanced="yes"/>
14+
<option key="null_value" advanced="yes"/>
15+
</qgisgrassmodule>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Sample a space time raster dataset at specific coordinates and write the output to file using different layouts" module="t.rast.what.qgis.py">
5+
<flag key="n" answer="on"/>
6+
<file key="coordinates" type="old"/>
7+
<option key="strds"/>
8+
<file key="output" label="Name for the output file" type="new"/>
9+
<option key="layout"/>
10+
<option key="where" advanced="yes" label="Temporal WHERE conditions without 'where' keyword"/>
11+
<option key="order" advanced="yes"/>
12+
<option key="nprocs" advanced="yes"/>
13+
<option key="separator" advanced="yes"/>
14+
<option key="null_value" advanced="yes"/>
15+
</qgisgrassmodule>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Sample a space time raster dataset at specific coordinates and write the output to file using different layouts" module="t.rast.what.qgis.py">
5+
<flag key="n" answer="on"/>
6+
<option key="points"/>
7+
<option key="strds"/>
8+
<file key="output" label="Name for the output file" type="new"/>
9+
<option key="layout"/>
10+
<option key="where" advanced="yes" label="Temporal WHERE conditions without 'where' keyword"/>
11+
<option key="order" advanced="yes"/>
12+
<option key="nprocs" advanced="yes"/>
13+
<option key="separator" advanced="yes"/>
14+
<option key="null_value" advanced="yes"/>
15+
</qgisgrassmodule>

‎src/plugins/grass/modules/t.shift.qgm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
<qgisgrassmodule label="Shifts temporally the maps of a space time dataset" module="t.shift">
55
<option key="input"/>
6-
<option key="temporaltype"/>
6+
<option key="type"/>
77
<option key="granularity"/>
88
</qgisgrassmodule>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Vector supervised classification tool which uses attributes as classification parametres" module="v.class.mlpy.qgis.py">
5+
<option key="input"/>
6+
<option key="training"/>
7+
<option key="class_column"/>
8+
<option key="columns"/>
9+
</qgisgrassmodule>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Converts LAS LiDAR point clouds to a GRASS vector map with libLAS." module="v.in.lidar">
5+
<file key="input" label="LiDAR input files in LAS format (*.las or *.laz)" type="old"/>
6+
<option key="output"/>
7+
<option key="spatial" advanced="yes"/>
8+
<option key="location" advanced="yes"/>
9+
<option key="return_filter" advanced="yes"/>
10+
<option key="class_filter" advanced="yes"/>
11+
<flag key="p" answer="off"/>
12+
<flag key="t" answer="off"/>
13+
<flag key="o" answer="off"/>
14+
<flag key="r" answer="off" advanced="yes"/>
15+
<flag key="3" answer="off" advanced="yes"/>
16+
<flag key="i" answer="off" advanced="yes"/>
17+
<flag key="b" answer="off"/>
18+
</qgisgrassmodule>

‎src/plugins/grass/modules/v.pack.qgm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Exports a vector map as GRASS GIS specific archive file" module="v.pack">
5+
<option key="input"/>
6+
<file key="output" type="new"/>
7+
<flag key="c" answer="off" advanced="yes"/>
8+
</qgisgrassmodule>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Imports a vector map as GRASS GIS specific archive file (packed with v.pack) a." module="v.unpack">
5+
<file key="input" type="old"/>
6+
<option key="output"/>
7+
<flag key="o" answer="off" advanced="yes"/>
8+
</qgisgrassmodule>

‎src/plugins/grass/scripts/t.rast.what.qgis.py

Lines changed: 526 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
#!/usr/bin/env python
2+
3+
############################################################################
4+
#
5+
# MODULE: v.class.mlpy
6+
# AUTHOR(S): Vaclav Petras
7+
# PURPOSE: Classifies features in vecor map.
8+
# COPYRIGHT: (C) 2012 by Vaclav Petras, and the GRASS Development Team
9+
#
10+
# This program is free software; you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation; either version 2 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# This program is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
############################################################################
21+
22+
#%module
23+
#% description: Vector supervised classification tool which uses attributes as classification parametres (order of columns matters, names not), cat column identifies feature, class_column is excluded from classification parametres.
24+
#% keyword: vector
25+
#% keyword: classification
26+
#% keyword: supervised
27+
#%end
28+
#%option G_OPT_V_MAP
29+
#% key: input
30+
#% description: Input vector map (attribut table required)
31+
#% required: yes
32+
#% multiple: no
33+
#%end
34+
#%option G_OPT_V_MAP
35+
#% key: training
36+
#% description: Training vector map (attribut table required)
37+
#% required: yes
38+
#% multiple: no
39+
#%end
40+
#%option G_OPT_V_FIELD
41+
#% key: class_column
42+
#% type: string
43+
#% label: Name of column containing class
44+
#% description: Used for both input/output and training dataset. If column does not exists in input map attribute table, it will be created.
45+
#% required: no
46+
#% multiple: no
47+
#% answer: class
48+
#%end
49+
#%option
50+
#% key: columns
51+
#% type: string
52+
#% label: Columns to be used in classification
53+
#% description: Columns to be used in classification. If left empty, all columns will be used for classification except for class_column and cat column.
54+
#% required: no
55+
#% multiple: yes
56+
#%end
57+
58+
59+
# TODO: add other classifiers
60+
# TODO: improve doc
61+
# TODO: input/training could be multiple
62+
# TODO: handle layers
63+
# TODO: ouput to new map (all classes/one class), depens what is faster
64+
65+
66+
import grass.script as grass
67+
68+
import numpy as np
69+
70+
71+
def addColumn(mapName, columnName, columnType):
72+
"""Adds column to the map's table."""
73+
columnDefinition = columnName + ' ' + columnType
74+
grass.run_command('v.db.addcolumn', map=mapName,
75+
columns=columnDefinition)
76+
77+
78+
def hasColumn(tableDescription, column):
79+
"""Checks if the column is in the table description
80+
81+
@todo This should be part of some object in the lib.
82+
"""
83+
for col in tableDescription['cols']:
84+
if col[0] == column:
85+
return True
86+
return False
87+
88+
89+
def updateColumn(mapName, column, cats, values=None):
90+
"""!Updates column values for rows with a given categories.
91+
92+
\param cats categories to be updated
93+
or a list of tuples (cat, value) if \p values is None
94+
\param values to be set for column (same length as cats) or \c None
95+
"""
96+
statements = ''
97+
for i in range(len(cats)):
98+
if values is None:
99+
cat = str(cats[i][0])
100+
val = str(cats[i][1])
101+
else:
102+
cat = str(cats[i])
103+
val = str(values[i])
104+
statement = 'UPDATE ' + mapName + ' SET '
105+
statement += column + ' = ' + val
106+
statement += ' WHERE cat = ' + cat
107+
statements += statement + ';\n'
108+
109+
grass.write_command('db.execute', input='-', stdin=statements)
110+
111+
112+
class Classifier:
113+
"""!Interface class between mlpy and other code
114+
115+
It does not uses numpy in the interface bu this may be wrong.
116+
"""
117+
def __init__(self):
118+
try:
119+
import mlpy
120+
except ImportError:
121+
grass.fatal(_("Cannot import mlpy (http://mlpy.sourceforge.net)"
122+
" library."
123+
" Please install it or ensure that it is on path"
124+
" (use PYTHONPATH variable)."))
125+
# Pytlit has a problem with this mlpy and v.class.mlpy.py
126+
# thus, warinings for objects from mlpy has to be disabled
127+
self.mlclassifier = mlpy.DLDA(delta=0.01) # pylint: disable=E1101
128+
129+
def learn(self, values, classes):
130+
self.mlclassifier.learn(np.array(values), np.array(classes))
131+
132+
def pred(self, values):
133+
return self.mlclassifier.pred(np.array(values))
134+
135+
136+
# TODO: raise exception when str can not be float
137+
# TODO: repair those functions, probably create a class
138+
# TODO: use numpy or array
139+
def fromDbTableToSimpleTable(dbTable, columnsDescription, columnWithClass):
140+
sTable = []
141+
for row in dbTable:
142+
sRow = []
143+
for i, col in enumerate(row):
144+
columnName = columnsDescription[i][0]
145+
if columnName != columnWithClass and columnName != 'cat':
146+
sRow.append(float(col))
147+
sTable.append(sRow)
148+
149+
return sTable
150+
151+
152+
def extractColumnWithClass(dbTable, columnsDescription, columnWithClass):
153+
classColumn = []
154+
for row in dbTable:
155+
for i, col in enumerate(row):
156+
columnName = columnsDescription[i][0]
157+
if columnName == columnWithClass:
158+
classColumn.append(float(col))
159+
160+
return classColumn
161+
162+
163+
def extractNthColumn(dbTable, columnNumber):
164+
classColumn = []
165+
for row in dbTable:
166+
for i, col in enumerate(row):
167+
if columnNumber == i:
168+
classColumn.append(float(col))
169+
170+
return classColumn
171+
172+
173+
def extractColumnWithCats(dbTable, columnsDescription):
174+
column = []
175+
for row in dbTable:
176+
for i, col in enumerate(row):
177+
columnName = columnsDescription[i][0]
178+
if columnName == 'cat':
179+
column.append(float(col))
180+
181+
return column
182+
183+
184+
# unused
185+
def fatal_noAttributeTable(mapName):
186+
grass.fatal(_("Vector map <%s> has no or empty attribute table")
187+
% mapName)
188+
189+
190+
def fatal_noEnoughColumns(mapName, ncols, required):
191+
grass.fatal(_("Not enough columns in vector map <%(map)s>"
192+
" (found %(ncols)s, expected at least %(r)s")
193+
% {'map': mapName, 'ncols': ncols, 'r': required})
194+
195+
196+
def fatal_noClassColumn(mapName, columnName):
197+
grass.fatal(_("Vector map <%(map)s> does not have"
198+
" the column <%(col)s> cointaining class")
199+
% {'map': mapName, 'col': columnName})
200+
201+
202+
def fatal_noRows(mapName):
203+
grass.fatal(_("Empty attribute table for map vector <%(map)s>")
204+
% {'map': mapName})
205+
206+
207+
def checkNcols(mapName, tableDescription, requiredNcols):
208+
ncols = tableDescription['ncols']
209+
if ncols < requiredNcols:
210+
fatal_noEnoughColumns(mapName, ncols, requiredNcols)
211+
212+
213+
def checkNrows(mapName, tableDescription):
214+
if not tableDescription['nrows'] > 0:
215+
fatal_noRows(mapName)
216+
217+
218+
def checkDbConnection(mapName):
219+
"""! Checks if vector map has an attribute table.
220+
221+
\todo check layer
222+
"""
223+
ret = grass.vector_db(mapName)
224+
if not ret:
225+
grass.fatal(_("Vector map <%s> has no attribute table") % mapName)
226+
227+
228+
def main():
229+
options, unused = grass.parser()
230+
231+
mapName = options['input']
232+
trainingMapName = options['training']
233+
234+
columnWithClass = options['class_column']
235+
236+
useAllColumns = True
237+
if options['columns']:
238+
# columns as string
239+
columns = options['columns'].strip()
240+
useAllColumns = False
241+
242+
# TODO: allow same input and output map only if --overwrite was specified
243+
# TODO: is adding column overwriting or overwriting is only updating of existing?
244+
245+
# variable names conected to training dataset have training prefix
246+
# variable names conected to classified dataset have no prefix
247+
248+
# checking database connection (if map has a table)
249+
# TODO: layer
250+
checkDbConnection(trainingMapName)
251+
checkDbConnection(mapName)
252+
253+
# loading descriptions first to check them
254+
255+
trainingTableDescription = grass.db_describe(table=trainingMapName)
256+
257+
if useAllColumns:
258+
trainingMinNcols = 3
259+
checkNcols(trainingMapName, trainingTableDescription, trainingMinNcols)
260+
else:
261+
pass
262+
263+
checkNrows(trainingMapName, trainingTableDescription)
264+
265+
if not hasColumn(trainingTableDescription, columnWithClass):
266+
fatal_noClassColumn(trainingMapName, columnWithClass)
267+
268+
tableDescription = grass.db_describe(table=mapName)
269+
270+
if useAllColumns:
271+
minNcols = 2
272+
checkNcols(mapName, tableDescription, minNcols)
273+
else:
274+
pass
275+
276+
checkNrows(mapName, tableDescription)
277+
278+
# TODO: check same (+-1) number of columns
279+
280+
# loadnig data
281+
282+
# TODO: make fun from this
283+
if useAllColumns:
284+
dbTable = grass.db_select(table=trainingMapName)
285+
else:
286+
# assuming that columns concatenated by comma
287+
sql = 'SELECT %s,%s FROM %s' % (columnWithClass, columns, trainingMapName)
288+
dbTable = grass.db_select(sql=sql)
289+
290+
trainingParameters = fromDbTableToSimpleTable(dbTable,
291+
columnsDescription=trainingTableDescription['cols'],
292+
columnWithClass=columnWithClass)
293+
294+
if useAllColumns:
295+
trainingClasses = extractColumnWithClass(dbTable,
296+
columnsDescription=trainingTableDescription['cols'],
297+
columnWithClass=columnWithClass)
298+
else:
299+
# FIXME: magic num?
300+
trainingClasses = extractNthColumn(dbTable, 0)
301+
302+
# TODO: hard coded 'cat'?
303+
if useAllColumns:
304+
dbTable = grass.db_select(table=mapName)
305+
else:
306+
# assuming that columns concatenated by comma
307+
sql = 'SELECT %s,%s FROM %s' % ('cat', columns, mapName)
308+
dbTable = grass.db_select(sql=sql)
309+
310+
parameters = fromDbTableToSimpleTable(dbTable,
311+
columnsDescription=tableDescription['cols'],
312+
columnWithClass=columnWithClass)
313+
if useAllColumns:
314+
cats = extractColumnWithCats(dbTable, columnsDescription=tableDescription['cols'])
315+
else:
316+
cats = extractNthColumn(dbTable, 0)
317+
318+
# since dbTable can be big it is better to avoid to have it in memory twice
319+
del dbTable
320+
del trainingTableDescription
321+
322+
classifier = Classifier()
323+
classifier.learn(trainingParameters, trainingClasses)
324+
classes = classifier.pred(parameters)
325+
326+
# add column only if not exists and the classification was successful
327+
if not hasColumn(tableDescription, columnWithClass):
328+
addColumn(mapName, columnWithClass, 'int')
329+
330+
updateColumn(mapName, columnWithClass, cats, classes)
331+
332+
# TODO: outupt as a new map (use INSERT, can be faster)
333+
# TODO: output as a new layer?
334+
335+
336+
if __name__ == "__main__":
337+
main()

0 commit comments

Comments
 (0)
Please sign in to comment.