def test_add_two_columns_check(self): """Add two column to the attribute table""" module = SimpleModule('v.db.addcolumn', map='myroads', columns='slope_2 double precision, \ myname varchar(15)') self.assertModule(module) m = SimpleModule('v.info', map='myroads', flags='c') self.assertModule(m) self.assertRegexpMatches(decode(m.outputs.stdout), 'slope_2') self.assertRegexpMatches(decode(m.outputs.stdout), 'myname')
def tearDownClass(cls): gisenv = SimpleModule('g.gisenv', get='GISDBASE') cls.runModule(gisenv, expecting_stdout=True) gisdbase = gisenv.outputs.stdout.strip() gisenv = SimpleModule('g.gisenv', get='LOCATION_NAME') cls.runModule(gisenv, expecting_stdout=True) location = gisenv.outputs.stdout.strip() cls.runModule("g.mapset", mapset=cls.old_mapset) for mapset_name in cls.mapsets_to_remove: mapset_path = os.path.join(gisdbase, location, mapset_name) silent_rmtree(mapset_path)
def test_drop_column_with_force(self): """Drop column with force, the column should not be in the table""" module = SimpleModule("db.dropcolumn", table=self.mapName, column=self.colName, flags="f") self.assertModule(module) m = SimpleModule("db.columns", table=self.mapName) self.assertModule(m) self.assertNotRegexpMatches(decode(m.outputs.stdout), self.colName)
def test_rename_column(self): """Renaming a column""" module = SimpleModule('v.db.renamecolumn', map='myroads', column=('ROAD_NAME', 'roadname')) self.assertModule(module) m = SimpleModule('v.info', flags='c', map='myroads') self.assertModule(m) self.assertRegexpMatches(decode(m.outputs.stdout), 'roadname') self.assertNotRegexpMatches(decode(m.outputs.stdout), 'ROAD_NAME')
def test_rename_column(self): """Renaming a column""" module = SimpleModule("v.db.renamecolumn", map="myroads", column=("ROAD_NAME", "roadname")) self.assertModule(module) m = SimpleModule("v.info", flags="c", map="myroads") self.assertModule(m) self.assertRegexpMatches(decode(m.outputs.stdout), "roadname") self.assertNotRegexpMatches(decode(m.outputs.stdout), "ROAD_NAME")
def test_stvds_info(self): self.runModule("g.mapset", mapset="testvect4") tinfo_string = """id=A@testvect1 name=A mapset=testvect1 start_time='2001-01-01 00:00:00' end_time='2001-04-01 00:00:00' granularity='1 month'""" info = SimpleModule( "t.info", flags="g", type="stvds", input="A@testvect1") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") self.runModule("g.mapset", mapset="testvect3") tinfo_string = """id=A@testvect2 name=A mapset=testvect2 start_time='2001-01-01 00:00:00' end_time='2001-07-01 00:00:00' granularity='2 months'""" info = SimpleModule( "t.info", flags="g", type="stvds", input="A@testvect2") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") self.runModule("g.mapset", mapset="testvect2") tinfo_string = """id=A@testvect3 name=A mapset=testvect3 start_time='2001-01-01 00:00:00' end_time='2001-10-01 00:00:00' granularity='3 months'""" info = SimpleModule( "t.info", flags="g", type="stvds", input="A@testvect3") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") self.runModule("g.mapset", mapset="testvect1") tinfo_string = """id=A@testvect4 name=A mapset=testvect4 start_time='2001-01-01 00:00:00' end_time='2002-01-01 00:00:00' granularity='4 months'""" info = SimpleModule( "t.info", flags="g", type="stvds", input="A@testvect4") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=")
def test_strds_info(self): self.runModule("g.mapset", mapset="test3d4") tinfo_string = """id=A@test3d1 name=A mapset=test3d1 start_time=2001-01-01 00:00:00 end_time=2001-04-01 00:00:00 granularity=1 month""" info = SimpleModule( "t.info", flags="g", type="str3ds", input="A@test3d1") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") self.runModule("g.mapset", mapset="test3d3") tinfo_string = """id=A@test3d2 name=A mapset=test3d2 start_time=2001-01-01 00:00:00 end_time=2001-07-01 00:00:00 granularity=2 months""" info = SimpleModule( "t.info", flags="g", type="str3ds", input="A@test3d2") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") self.runModule("g.mapset", mapset="test3d2") tinfo_string = """id=A@test3d3 name=A mapset=test3d3 start_time=2001-01-01 00:00:00 end_time=2001-10-01 00:00:00 granularity=3 months""" info = SimpleModule( "t.info", flags="g", type="str3ds", input="A@test3d3") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") self.runModule("g.mapset", mapset="test3d1") tinfo_string = """id=A@test3d4 name=A mapset=test3d4 start_time=2001-01-01 00:00:00 end_time=2002-01-01 00:00:00 granularity=4 months""" info = SimpleModule( "t.info", flags="g", type="str3ds", input="A@test3d4") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=")
def tearDownClass(cls): """Remove temporary data""" gisenv = SimpleModule("g.gisenv", get="GISDBASE") cls.runModule(gisenv, expecting_stdout=True) gisdbase = gisenv.outputs.stdout.strip() gisenv = SimpleModule("g.gisenv", get="LOCATION_NAME") cls.runModule(gisenv, expecting_stdout=True) location = gisenv.outputs.stdout.strip() cls.runModule("g.remove", flags="f", type="raster", name=(cls.temp1,)) cls.runModule("g.mapset", mapset=cls.old_mapset) for mapset_name in cls.mapsets_to_remove: mapset_path = os.path.join(gisdbase, location, mapset_name) silent_rmtree(mapset_path)
def test_simple_2procs(self): self.assertModule( "t.rast.gapfill", input="A", suffix="num%01", basename="test", nprocs=2, verbose=True, ) # self.assertModule("t.info", type="strds", flags="g", input="A") tinfo_string = """start_time='2001-01-01 00:00:00' end_time='2002-01-01 00:00:00' granularity='1 month' map_time=interval number_of_maps=12 min_min=100.0 min_max=1200.0 max_min=100.0 max_max=1200.0""" info = SimpleModule("t.info", flags="g", type="strds", input="A") self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=") text = """name|start_time|end_time|min|max a_1|2001-01-01 00:00:00|2001-02-01 00:00:00|100.0|100.0 test_6_1|2001-02-01 00:00:00|2001-03-01 00:00:00|200.0|200.0 test_6_2|2001-03-01 00:00:00|2001-04-01 00:00:00|300.0|300.0 a_2|2001-04-01 00:00:00|2001-05-01 00:00:00|400.0|400.0 test_7_1|2001-05-01 00:00:00|2001-06-01 00:00:00|500.0|500.0 test_7_2|2001-06-01 00:00:00|2001-07-01 00:00:00|600.0|600.0 test_7_3|2001-07-01 00:00:00|2001-08-01 00:00:00|700.0|700.0 test_7_4|2001-08-01 00:00:00|2001-09-01 00:00:00|800.0|800.0 test_7_5|2001-09-01 00:00:00|2001-10-01 00:00:00|900.0|900.0 test_7_6|2001-10-01 00:00:00|2001-11-01 00:00:00|1000.0|1000.0 test_7_7|2001-11-01 00:00:00|2001-12-01 00:00:00|1100.0|1100.0 a_3|2001-12-01 00:00:00|2002-01-01 00:00:00|1200.0|1200.0 """ rast_list = SimpleModule( "t.rast.list", columns=("name", "start_time", "end_time", "min,max"), input="A", ) self.assertModule(rast_list) self.assertLooksLike(text, rast_list.outputs.stdout)
def test_add_two_columns_check(self): """Add two column to the attribute table""" module = SimpleModule( "v.db.addcolumn", map="myroads", columns="slope_2 double precision, \ myname varchar(15)", ) self.assertModule(module) m = SimpleModule("v.info", map="myroads", flags="c") self.assertModule(m) self.assertRegexpMatches(decode(m.outputs.stdout), "slope_2") self.assertRegexpMatches(decode(m.outputs.stdout), "myname")
def test_raster_info(self): self.runModule("g.mapset", mapset="test3d3") tinfo_string = """id=a1@test3d1 name=a1 mapset=test3d1 temporal_type=absolute start_time=2001-01-01 00:00:00 end_time=2001-02-01 00:00:00 """ info = SimpleModule( "t.info", flags="g", type="raster_3d", input="a1@test3d1") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") tinfo_string = """id=a1@test3d2 name=a1 mapset=test3d2 temporal_type=absolute start_time=2001-01-01 00:00:00 end_time=2001-03-01 00:00:00 """ info = SimpleModule( "t.info", flags="g", type="raster_3d", input="a1@test3d2") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") tinfo_string = """id=a1@test3d3 name=a1 mapset=test3d3 temporal_type=absolute start_time=2001-01-01 00:00:00 end_time=2001-04-01 00:00:00 """ info = SimpleModule( "t.info", flags="g", type="raster_3d", input="a1@test3d3") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") tinfo_string = """id=a1@test3d4 name=a1 mapset=test3d4 temporal_type=absolute start_time=2001-01-01 00:00:00 end_time=2001-05-01 00:00:00 """ info = SimpleModule( "t.info", flags="g", type="raster_3d", input="a1@test3d4") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=")
def testv_vector_info(self): self.runModule("g.mapset", mapset="testvect3") tinfo_string = """id=a1@testvect1 name=a1 mapset=testvect1 temporal_type=absolute start_time='2001-01-01 00:00:00' end_time='2001-02-01 00:00:00'""" info = SimpleModule( "t.info", flags="g", type="vector", input="a1@testvect1") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") tinfo_string = """id=a1@testvect2 name=a1 mapset=testvect2 temporal_type=absolute start_time='2001-01-01 00:00:00' end_time='2001-03-01 00:00:00'""" info = SimpleModule( "t.info", flags="g", type="vector", input="a1@testvect2") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") tinfo_string = """id=a1@testvect3 name=a1 mapset=testvect3 temporal_type=absolute start_time='2001-01-01 00:00:00' end_time='2001-04-01 00:00:00'""" info = SimpleModule( "t.info", flags="g", type="vector", input="a1@testvect3") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=") tinfo_string = """id=a1@testvect4 name=a1 mapset=testvect4 temporal_type=absolute start_time='2001-01-01 00:00:00' end_time='2001-05-01 00:00:00'""" info = SimpleModule( "t.info", flags="g", type="vector", input="a1@testvect4") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=")
def test_overwrite_existing_raster(self): """Test that existing raster is overriden if desired""" runivar_source = SimpleModule('r.univar', flags='g', map='rename_3') self.runModule(runivar_source, expecting_stdout=True) original_runivar_source = text_to_keyvalue( runivar_source.outputs.stdout, sep='=', skip_empty=True) runivar_target = SimpleModule('r.univar', flags='g', map='exists') self.runModule(runivar_target, expecting_stdout=True) original_runivar_target = text_to_keyvalue( runivar_target.outputs.stdout, sep='=', skip_empty=True) module = SimpleModule('g.rename', raster=['rename_3', 'exists'], overwrite=True) self.assertModule(module) self.assertRasterExists( 'exists', msg= "Destination (here: existing) map (to) should exist after rename") self.assertFalse(is_map_in_mapset('rename_3', type='raster'), msg="Source map (from) should not exist after rename") runivar = SimpleModule('r.univar', flags='g', map='exists') self.runModule(runivar, expecting_stdout=True) new_runivar = text_to_keyvalue(runivar.outputs.stdout, sep='=', skip_empty=True) # both these tests are probably redundant but let's test thoroughly if keyvalue_equals(dict_a=original_runivar_target, dict_b=new_runivar, precision=1e-7): msg = "Raster map did not change. It probably wasn't overwritten." self.fail(msg) if not keyvalue_equals(dict_a=original_runivar_source, dict_b=new_runivar, precision=1e-7): unused, missing, mismatch = diff_keyvalue( dict_a=original_runivar_source, dict_b=new_runivar, precision=1e-7) if mismatch: msg = "Destination raster map is not the same as source." msg += " It probably wasn't overwritten.\n" msg += "Difference between r.univar of maps:\n" msg += "mismatch values" msg += " (key, reference, actual): %s\n" % mismatch self.fail(msg)
def test_flage(self): """Testing flag e with map geology""" output_str = """number of features with non NULL attribute: 1832 number of missing attributes: 0 number of NULL attributes: 0 minimum: 166.947 maximum: 2.72948e+06 range: 2.72932e+06 sum: 7.88761e+07 mean: 43054.7 mean of absolute values: 43054.7 population standard deviation: 132689 population variance: 1.76064e+10 population coefficient of variation: 3.08187 sample standard deviation: 132725 sample variance: 1.7616e+10 kurtosis: 139.157 skewness: 9.7065 1st quartile: 3699.32 median (even number of cells): 10308.4 3rd quartile: 29259.1 90th percentile: 86449.7""" v_univar = SimpleModule('v.univar', map='geology', column='PERIMETER', flags='e') v_univar.run() self.assertLooksLike(actual=v_univar.outputs.stdout.encode('utf8'), reference=output_str)
def testComma(self): sel = SimpleModule('v.db.select', flags='c', map=self.invect, where="{col}='{val}'".format(col=self.col, val=self.val), separator=',') sel.run() self.assertLooksLike(reference=out_sep, actual=sel.outputs.stdout)
def test_selection_and_expression(self): """Perform a selection by datetime and a r3.mapcalc expression""" self.assertModule("t.rast3d.extract", input="A", output="B", where="start_time > '2001-06-01'", expression=" if(A > 400, A, null())", basename="b", nprocs=2, overwrite=True) #self.assertModule("t.info", flags="g", input="B") tinfo_string = """start_time=2002-01-01 00:00:00 end_time=2002-07-01 00:00:00 granularity=3 months map_time=interval aggregation_type=None number_of_maps=2 min_min=500.0 min_max=600.0 max_min=500.0 max_max=600.0""" info = SimpleModule("t.info", flags="g", type="str3ds", input="B") self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
def testWhere(self): """Testing v.db.select with where option""" sel = SimpleModule('v.db.select', flags='c', map=self.invect, where="{col}='{val}'".format(col=self.col, val=self.val)) sel.run() self.assertLooksLike(reference=out_where, actual=sel.outputs.stdout)
def test_simple_area(self): self.assertModule( "t.rast.to.vect", input="A", output="result", type="area", flags="n", column="values", basename="test", nprocs=1, overwrite=True, verbose=True, ) # self.assertModule("t.info", type="stvds", flags="g", input="result") tinfo_string = """start_time='2001-01-01 00:00:00' end_time='2002-04-01 00:00:00' granularity='3 months' map_time=interval number_of_maps=5 boundaries=4 centroids=4 primitives=8 nodes=4 areas=4 islands=4""" info = SimpleModule("t.info", flags="g", type="stvds", input="result") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=" )
def test_selection(self): """Perform a simple selection by datetime""" self.assertModule("t.rast3d.extract", input="A", output="B", where="start_time > '2001-06-01'") #self.assertModule("t.info", flags="g", input="B") tinfo_string = """start_time=2001-07-01 00:00:00 end_time=2002-07-01 00:00:00 granularity=3 months map_time=interval aggregation_type=None number_of_maps=4 min_min=300.0 min_max=600.0 max_min=300.0 max_max=600.0""" info = SimpleModule("t.info", flags="g", type="str3ds", input="B") self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
def test_profile_resolution(self): rprofile = SimpleModule('r.profile', input='elevation', resolution=25, coordinates=[637656, 224222, 637766, 224289]) self.assertModule(rprofile) self.assertMultiLineEqual(rprofile.outputs.stdout.strip(), output4.strip()) self.assertIn('128.798292 [meters]', rprofile.outputs.stderr) # distance self.assertIn('25 [meters]', rprofile.outputs.stderr) # resolution
def test_disaggregation(self): """Disaggregation with empty maps""" self.assertModule("t.rast.aggregate", input="A", output="B", basename="b", granularity="2 days", method="average", sampling=["overlaps", "overlapped", "during"], nprocs=2, flags="n") tinfo_string = """start_time='2001-01-15 00:00:00' end_time='2001-04-25 00:00:00' granularity='2 days' aggregation_type=average number_of_maps=50 map_time=interval min_min=100.0 min_max=600.0 max_min=100.0 max_max=600.0""" info = SimpleModule("t.info", flags="g", input="B") #info.run() #print info.outputs.stdout self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
def test_pf_flags(self): """Test that -p and -f flags are exclusive""" module = SimpleModule("g.list", flags="pf", type="raster") self.assertModuleFail(module) stderr = module.outputs.stderr self.assertIn("-p", stderr) self.assertIn("-f", stderr)
def test_re_flags(self): """Test that -r and -e flags are exclusive""" module = SimpleModule("g.list", flags="re", type="raster") self.assertModuleFail(module) stderr = module.outputs.stderr self.assertIn("-r", stderr) self.assertIn("-e", stderr)
def test_4(self): """Simple test""" self.assertModule( "t.rast.aggregate", input="A", output="B", basename="b", granularity=21, method="average", sampling=["contains"], nprocs=4, verbose=True, ) tinfo_string = """semantic_type=mean start_time='0' end_time='21' unit=days granularity=21 map_time=interval aggregation_type=average number_of_maps=2 min_min=350.0 min_max=350.0 max_min=350.0 max_max=350.0""" info = SimpleModule("t.info", flags="g", input="B") # info.run() # print info.outputs.stdout self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
def test_1(self): """Simple test""" self.assertModule( "t.rast.aggregate", input="A", output="B", basename="b", granularity=6, method="average", file_limit=0, sampling=["overlaps", "overlapped", "contains"], nprocs=2, verbose=True, ) tinfo_string = """start_time='0' end_time='18' unit=days granularity=6 map_time=interval aggregation_type=average number_of_maps=3 min_min=150.0 min_max=550.0 max_min=150.0 max_max=550.0""" info = SimpleModule("t.info", flags="g", input="B") # info.run() # print info.outputs.stdout self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
def test_parallel(self): self.assertModule( "t.rast.to.vect", input="A", output="result", type="point", flags="t", column="values", basename="test", nprocs=4, overwrite=True, verbose=True, ) # self.assertModule("t.info", type="stvds", flags="g", input="result") tinfo_string = """start_time='2001-01-01 00:00:00' end_time='2002-01-01 00:00:00' granularity='3 months' number_of_maps=4 points=384 primitives=384""" info = SimpleModule("t.info", flags="g", type="stvds", input="result") self.assertModuleKeyValue( module=info, reference=tinfo_string, precision=2, sep="=" )
def test_aggregation_1day_2procs(self): """Aggregation one month""" start = datetime.now() self.assertModule("t.rast.aggregate", input="A", output="B", basename="b", granularity="1 day", method="sum", sampling=["contains"], nprocs=2) end = datetime.now() delta = end - start print("test_aggregation_1day_2procs:", delta.total_seconds()) tinfo_string = """start_time=2001-01-01 00:00:00 end_time=2001-04-01 00:00:00 granularity=1 day map_time=interval aggregation_type=sum number_of_maps=90 min_min=21.0 min_max=3225.0 max_min=21.0 max_max=3225.0""" info = SimpleModule("t.info", flags="g", input="B") #info.run() #print info.outputs.stdout self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
def setUp(self): self.vwhat = SimpleModule( 'v.what', map=['roadsmajor', 'precip_30ynormals_3d', 'lakes'], layer=['-1', '-1', '-1'], coordinates=[636661, 226489], distance=1000)
def test_aggregation_12hours(self): """Aggregation one month""" self.assertModule("t.rast.aggregate", input="A", output="B", basename="b", granularity="12 hours", method="sum", sampling=["contains"], nprocs=9, file_limit=2) tinfo_string = """start_time=2001-01-01 00:00:00 end_time=2001-04-01 00:00:00 granularity=12 hours map_time=interval aggregation_type=sum number_of_maps=180 min_min=6.0 min_max=1617.0 max_min=6.0 max_max=1617.0""" info = SimpleModule("t.info", flags="g", input="B") #info.run() #print info.outputs.stdout self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
def setUp(self): self.vwhat = SimpleModule( 'v.what', map=['schools', 'roadsmajor', 'elev_points', 'geology'], layer=['-1', '-1', '-1', '-1'], coordinates=[636661, 226489], distance=1000)