def main(options, flags): import grass.pygrass.modules as pymod import grass.temporal as tgis from grass.pygrass.vector import VectorTopo invect = options["input"] if invect.find('@') != -1: invect = invect.split('@')[0] incol = options["date_column"] indate = options["date"] strds = options["strds"] if strds.find('@') != -1: strds_name = strds.split('@')[0] else: strds_name = strds output = options["output"] cols = options["columns"].split(',') mets = options["method"].split(',') gran = options["granularity"] dateformat = options["date_format"] separator = gscript.separator(options["separator"]) stdout = False if output != '-' and flags['u']: gscript.fatal(_("Cannot combine 'output' option and 'u' flag")) elif output != '-' and flags['c']: gscript.fatal(_("Cannot combine 'output' option and 'c' flag")) elif output == '-' and (flags['u'] or flags['c']): output = invect gscript.warning(_("Attribute table of vector {name} will be updated" "...").format(name=invect)) else: stdout = True if flags['c']: cols = [] for m in mets: colname = "{st}_{me}".format(st=strds_name, me=m) cols.append(colname) try: pymod.Module("v.db.addcolumn", map=invect, columns="{col} " "double precision".format(col=colname)) except CalledModuleError: gscript.fatal(_("Not possible to create column " "{col}".format(col=colname))) if output != '-' and len(cols) != len(mets): gscript.fatal(_("'columns' and 'method' options must have the same " "number of elements")) tgis.init() dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() sp = tgis.open_old_stds(strds, "strds", dbif) if sp.get_temporal_type() == 'absolute': delta = int(tgis.gran_to_gran(gran, sp.get_granularity(), True)) if tgis.gran_singular_unit(gran) in ['year', 'month']: delta = int(tgis.gran_to_gran(gran, '1 day', True)) td = timedelta(delta) elif tgis.gran_singular_unit(gran) == 'day': delta = tgis.gran_to_gran(gran, sp.get_granularity(), True) td = timedelta(delta) elif tgis.gran_singular_unit(gran) == 'hour': td = timedelta(hours=delta) elif tgis.gran_singular_unit(gran) == 'minute': td = timedelta(minutes=delta) elif tgis.gran_singular_unit(gran) == 'second': td = timedelta(seconds=delta) else: if sp.get_granularity() >= int(gran): gscript.fatal(_("Input granularity is smaller or equal to the {iv}" " STRDS granularity".format(iv=strds))) td = int(gran) if incol and indate: gscript.fatal(_("Cannot combine 'date_column' and 'date' options")) elif not incol and not indate: gscript.fatal(_("You have to fill 'date_column' or 'date' option")) elif incol: try: dates = pymod.Module("db.select", flags='c', stdout_=PI, stderr_=PI, sql="SELECT DISTINCT {dc} from " "{vmap} order by {dc}".format(vmap=invect, dc=incol)) mydates = dates.outputs["stdout"].value.splitlines() except CalledModuleError: gscript.fatal(_("db.select return an error")) elif indate: mydates = [indate] pymap = VectorTopo(invect) pymap.open('r') if len(pymap.dblinks) == 0: try: pymap.close() pymod.Module("v.db.addtable", map=invect) except CalledModuleError: dbif.close() gscript.fatal(_("Unable to add table <%s> to vector map " "<%s>" % invect)) if pymap.is_open(): pymap.close() qfeat = pymod.Module("v.category", stdout_=PI, stderr_=PI, input=invect, option='print') myfeats = qfeat.outputs["stdout"].value.splitlines() if stdout: outtxt = '' for data in mydates: if sp.get_temporal_type() == 'absolute': fdata = datetime.strptime(data, dateformat) else: fdata = int(data) if flags['a']: sdata = fdata + td mwhere = "start_time >= '{inn}' and end_time < " \ "'{out}'".format(inn=fdata, out=sdata) else: sdata = fdata - td mwhere = "start_time >= '{inn}' and end_time < " \ "'{out}'".format(inn=sdata, out=fdata) lines = None try: r_what = pymod.Module("t.rast.what", points=invect, strds=strds, layout='timerow', separator=separator, flags="v", where=mwhere, quiet=True, stdout_=PI, stderr_=PI) lines = r_what.outputs["stdout"].value.splitlines() except CalledModuleError: pass if incol: try: qfeat = pymod.Module("db.select", flags='c', stdout_=PI, stderr_=PI, sql="SELECT DISTINCT cat from" " {vmap} where {dc}='{da}' order by " "cat".format(vmap=invect, da=data, dc=incol)) myfeats = qfeat.outputs["stdout"].value.splitlines() except CalledModuleError: gscript.fatal(_("db.select returned an error for date " "{da}".format(da=data))) if not lines and stdout: for feat in myfeats: outtxt += "{di}{sep}{da}".format(di=feat, da=data, sep=separator) for n in range(len(mets)): outtxt += "{sep}{val}".format(val='*', sep=separator) outtxt += "\n" if not lines: continue x = 0 for line in lines: vals = line.split(separator) if vals[0] in myfeats: try: nvals = np.array(vals[4:]).astype(np.float) except ValueError: if stdout: outtxt += "{di}{sep}{da}".format(di=vals[0], da=data, sep=separator) for n in range(len(mets)): outtxt += "{sep}{val}".format(val='*', sep=separator) outtxt += "\n" continue if stdout: outtxt += "{di}{sep}{da}".format(di=vals[0], da=data, sep=separator) for n in range(len(mets)): result = return_value(nvals, mets[n]) if stdout: outtxt += "{sep}{val}".format(val=result, sep=separator) else: try: if incol: pymod.Module("v.db.update", map=output, column=cols[n], value=str(result), where="{dc}='{da}' AND cat=" "{ca}".format(da=data, ca=vals[0], dc=incol)) else: pymod.Module("v.db.update", map=output, column=cols[n], value=str(result), where="cat={ca}".format(ca=vals[0])) except CalledModuleError: gscript.fatal(_("v.db.update return an error")) if stdout: outtxt += "\n" if x == len(myfeats): break else: x += 1 if stdout: print(outtxt)
def main(): parsed_obs = dict() layerscount = 0 service = SensorObservationService(options['url'], version=options['version'], username=options['username'], password=options['password']) if any(value is True and key in ['o', 'v', 'p', 't'] for key, value in flags.items()): soslib.get_description(service, options, flags) soslib.check_missing_params(options['offering'], options['output']) if options['granularity'] != '': import grass.temporal as tgis tgis.init() seconds_granularity = int( tgis.gran_to_gran(options['granularity'], '1 second', True)) else: seconds_granularity = 1 target = soslib.get_target_crs() run_command('g.remove', 'f', type='vector', name=options['output']) new = VectorTopo(options['output']) for off in options['offering'].split(','): # TODO: Find better way than iteration (at best OWSLib upgrade) out = soslib.handle_not_given_options(service, off, options['procedure'], options['observed_properties'], options['event_time']) procedure, observed_properties, event_time = out if flags['s']: create_maps(_, off, _, new, _, _, service, target, _, procedure) else: try: obs = service.get_observation( offerings=[off], responseFormat=options['response_format'], observedProperties=observed_properties, procedure=procedure, eventTime=event_time, timeout=int(options['timeout']), username=options['username'], password=options['password']) except: # TODO: catch errors properly (e.g. timeout) grass.fatal('Request did not succeed!') try: if options['version'] in ['1.0.0', '1.0'] and str( options['response_format'] ) == 'text/xml;subtype="om/1.0.0"': for prop in observed_properties: parsed_obs.update( {prop: soslib.xml2geojson(obs, prop, flags['i'])}) elif str(options['response_format']) == 'application/json': for prop in observed_properties: parsed_obs.update( {prop: soslib.json2geojson(obs, prop)}) except AttributeError: if sys.version_info[0] >= 3: sys.tracebacklimit = None else: sys.tracebacklimit = 0 raise AttributeError( 'There is no data for at least one of your procedures, ' 'could you change the time parameter, observed ' 'properties, procedures or offerings') except ValueError as e: if sys.version_info[0] >= 3: sys.tracebacklimit = None else: sys.tracebacklimit = 0 raise e create_maps(parsed_obs, off, layerscount, new, seconds_granularity, event_time, service, target, observed_properties) layerscount += len(parsed_obs) return 0
def main(): parsed_obs = dict() service = SensorObservationService(options['url'], version=options['version'], username=options['username'], password=options['password']) if any(value is True and key in ['o', 'v', 'p', 't'] for key, value in flags.iteritems()): get_description(service, options, flags) if options['offering'] == '' or options['output'] == '': if sys.version >= (3, 0): sys.tracebacklimit = None else: sys.tracebacklimit = 0 raise AttributeError( "You have to define any flags or use 'output' and 'offering' " "parameters to get the data") if options['granularity'] != '': import grass.temporal as tgis tgis.init() secondsGranularity = int( tgis.gran_to_gran(options['granularity'], '1 second', True)) else: secondsGranularity = 1 if options['resolution'] == '': resolution = None else: resolution = float(options['resolution']) if options['bbox'] != '': bbox = options['bbox'].split(',') run_command('g.region', n=float(bbox[0]), e=float(bbox[1]), s=float(bbox[2]), w=float(bbox[3]), res=resolution) else: grass.warning('You have not setted the bounding box. Bounding box will' ' be automatically based on procedure geometries for ' 'every map.') for off in options['offering'].split(','): # TODO: Find better way than iteration (at best OWSLib upgrade) procedure, observed_properties, event_time = handle_not_given_options( service, off, options['procedure'], options['observed_properties'], options['event_time']) event_time = 'T'.join(event_time.split(' ')) obs = service.get_observation( offerings=[off], responseFormat=options['response_format'], observedProperties=[observed_properties], procedure=procedure, eventTime=event_time, username=options['username'], password=options['password']) try: if options['version'] in ['1.0.0', '1.0'] and \ str(options['response_format']) == 'text/xml;subtype="om/1.0.0"': for property in observed_properties.split(','): parsed_obs.update({property: xml2geojson(obs, property)}) elif str(options['response_format']) == 'application/json': for property in observed_properties.split(','): parsed_obs.update({property: json2geojson(obs, property)}) except AttributeError: if sys.version >= (3, 0): sys.tracebacklimit = None else: sys.tracebacklimit = 0 raise AttributeError('There is no data for at least one of your ' 'procedures, could you change the time ' 'parameter, observed properties, ' 'procedures or offerings') create_maps(parsed_obs, off, secondsGranularity, resolution) return 0
def main(options, flags): import grass.pygrass.modules as pymod import grass.temporal as tgis from grass.pygrass.vector import VectorTopo invect = options["input"] if invect.find("@") != -1: invect = invect.split("@")[0] incol = options["date_column"] indate = options["date"] endcol = options["final_date_column"] enddate = options["final_date"] strds = options["strds"] nprocs = options["nprocs"] if strds.find("@") != -1: strds_name = strds.split("@")[0] else: strds_name = strds output = options["output"] if options["columns"]: cols = options["columns"].split(",") else: cols = [] mets = options["method"].split(",") gran = options["granularity"] dateformat = options["date_format"] separator = gscript.separator(options["separator"]) update = flags["u"] create = flags["c"] stdout = False if output != "-" and update: gscript.fatal(_("Cannot combine 'output' option and 'u' flag")) elif output != "-" and create: gscript.fatal(_("Cannot combine 'output' option and 'c' flag")) elif output == "-" and (update or create): if update and not cols: gscript.fatal(_("Please set 'columns' option")) output = invect else: stdout = True if create: cols = [] for m in mets: colname = "{st}_{me}".format(st=strds_name, me=m) cols.append(colname) try: pymod.Module( "v.db.addcolumn", map=invect, columns="{col} " "double precision".format(col=colname), ) except CalledModuleError: gscript.fatal( _("Not possible to create column " "{col}".format(col=colname))) gscript.warning( _("Attribute table of vector {name} will be updated" "...").format(name=invect)) elif update: colexist = pymod.Module("db.columns", table=invect, stdout_=PI).outputs.stdout.splitlines() for col in cols: if col not in colexist: gscript.fatal( _("Column '{}' does not exist, please create it first". format(col))) gscript.warning( _("Attribute table of vector {name} will be updated" "...").format(name=invect)) if output != "-" and len(cols) != len(mets): gscript.fatal( _("'columns' and 'method' options must have the same " "number of elements")) tgis.init() dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() sp = tgis.open_old_stds(strds, "strds", dbif) if sp.get_temporal_type() == "absolute": if gran: delta = int(tgis.gran_to_gran(gran, sp.get_granularity(), True)) if tgis.gran_singular_unit(gran) in ["year", "month"]: delta = int(tgis.gran_to_gran(gran, "1 day", True)) td = timedelta(delta) elif tgis.gran_singular_unit(gran) == "day": delta = tgis.gran_to_gran(gran, sp.get_granularity(), True) td = timedelta(delta) elif tgis.gran_singular_unit(gran) == "hour": td = timedelta(hours=delta) elif tgis.gran_singular_unit(gran) == "minute": td = timedelta(minutes=delta) elif tgis.gran_singular_unit(gran) == "second": td = timedelta(seconds=delta) else: td = None else: if sp.get_granularity() >= int(gran): gscript.fatal( _("Input granularity is smaller or equal to the {iv}" " STRDS granularity".format(iv=strds))) td = int(gran) if incol and indate: gscript.fatal(_("Cannot combine 'date_column' and 'date' options")) elif not incol and not indate: gscript.fatal(_("You have to fill 'date_column' or 'date' option")) if incol: if endcol: mysql = "SELECT DISTINCT {dc},{ec} from {vmap} order by " "{dc}".format( vmap=invect, dc=incol, ec=endcol) else: mysql = "SELECT DISTINCT {dc} from {vmap} order by " "{dc}".format( vmap=invect, dc=incol) try: dates = pymod.Module("db.select", flags="c", stdout_=PI, stderr_=PI, sql=mysql) mydates = dates.outputs["stdout"].value.splitlines() except CalledModuleError: gscript.fatal(_("db.select return an error")) elif indate: if enddate: mydates = ["{ida}|{eda}".format(ida=indate, eda=enddate)] else: mydates = [indate] mydates = [indate] pymap = VectorTopo(invect) pymap.open("r") if len(pymap.dblinks) == 0: try: pymap.close() pymod.Module("v.db.addtable", map=invect) except CalledModuleError: dbif.close() gscript.fatal( _("Unable to add table <%s> to vector map " "<%s>" % invect)) if pymap.is_open(): pymap.close() qfeat = pymod.Module("v.category", stdout_=PI, stderr_=PI, input=invect, option="print") myfeats = qfeat.outputs["stdout"].value.splitlines() if stdout: outtxt = "" for data in mydates: try: start, final = data.split("|") except ValueError: start = data final = None if sp.get_temporal_type() == "absolute": fdata = datetime.strptime(start, dateformat) else: fdata = int(start) if final: sdata = datetime.strptime(final, dateformat) elif flags["a"]: sdata = fdata + td else: sdata = fdata fdata = sdata - td mwhere = "start_time >= '{inn}' and start_time < " "'{out}'".format( inn=fdata, out=sdata) lines = None try: r_what = pymod.Module( "t.rast.what", points=invect, strds=strds, layout="timerow", separator=separator, flags="v", where=mwhere, quiet=True, stdout_=PI, stderr_=PI, nprocs=nprocs, ) lines = r_what.outputs["stdout"].value.splitlines() except CalledModuleError: gscript.warning("t.rast.what faild with where='{}'".format(mwhere)) pass if incol: if endcol: mysql = ("SELECT DISTINCT cat from {vmap} where {dc}='{da}' " "AND {ec}='{ed}' order by cat".format(vmap=invect, da=start, dc=incol, ed=final, ec=endcol)) else: mysql = ("SELECT DISTINCT cat from {vmap} where {dc}='{da}' " "order by cat".format(vmap=invect, da=start, dc=incol)) try: qfeat = pymod.Module("db.select", flags="c", stdout_=PI, stderr_=PI, sql=mysql) myfeats = qfeat.outputs["stdout"].value.splitlines() except CalledModuleError: gscript.fatal( _("db.select returned an error for date " "{da}".format(da=start))) if not lines and stdout: for feat in myfeats: outtxt += "{di}{sep}{da}".format(di=feat, da=start, sep=separator) for n in range(len(mets)): outtxt += "{sep}{val}".format(val="*", sep=separator) outtxt += "\n" if not lines: continue x = 0 for line in lines: vals = line.split(separator) if vals[0] in myfeats: try: nvals = np.array(vals[3:]).astype(float) except ValueError: if stdout: outtxt += "{di}{sep}{da}".format(di=vals[0], da=start, sep=separator) for n in range(len(mets)): outtxt += "{sep}{val}".format(val="*", sep=separator) outtxt += "\n" continue if stdout: outtxt += "{di}{sep}{da}".format(di=vals[0], da=start, sep=separator) for n in range(len(mets)): result = None if len(nvals) == 1: result = nvals[0] elif len(nvals) > 1: result = return_value(nvals, mets[n]) if stdout: if not result: result = "*" outtxt += "{sep}{val}".format(val=result, sep=separator) else: try: if incol: mywhe = "{dc}='{da}' AND ".format(da=start, dc=incol) if endcol: mywhe += "{dc}='{da}' AND ".format( da=final, dc=endcol) mywhe += "cat={ca}".format(ca=vals[0]) pymod.Module( "v.db.update", map=output, column=cols[n], value=str(result), where=mywhe, ) else: pymod.Module( "v.db.update", map=output, column=cols[n], value=str(result), where="cat={ca}".format(ca=vals[0]), ) except CalledModuleError: gscript.fatal(_("v.db.update return an error")) if stdout: outtxt += "\n" if x == len(myfeats): break else: x += 1 if stdout: print(outtxt)
def main(): parsed_obs = dict() service = SensorObservationService(options['url'], version=options['version'], username=options['username'], password=options['password']) if any(value is True and key in ['o', 'v', 'p', 't'] for key, value in flags.iteritems()): get_description(service, options, flags) if options['offering'] == '' or options['output'] == '': if sys.version >= (3, 0): sys.tracebacklimit = None else: sys.tracebacklimit = 0 raise AttributeError("You have to define any flags or use 'output' and" " 'offering' parameters to get the data") if options['granularity'] != '': import grass.temporal as tgis tgis.init() secondsGranularity = int( tgis.gran_to_gran(options['granularity'], '1 second', True)) else: secondsGranularity = 1 for off in options['offering'].split(','): # TODO: Find better way than iteration (at best OWSLib upgrade) procedure, observed_properties, event_time = handle_not_given_options( service, off, options['procedure'], options['observed_properties'], options['event_time']) event_time = 'T'.join(event_time.split(' ')) obs = service.get_observation( offerings=[off], responseFormat=options['response_format'], observedProperties=observed_properties, procedure=procedure, eventTime=event_time, username=options['username'], password=options['password']) try: if options['version'] in ['1.0.0', '1.0'] and str( options['response_format'] ) == 'text/xml;subtype="om/1.0.0"': for prop in observed_properties: parsed_obs.update({prop: xml2geojson(obs, prop)}) elif str(options['response_format']) == 'application/json': for prop in observed_properties: parsed_obs.update({prop: json2geojson(obs, prop)}) except AttributeError: if sys.version >= (3, 0): sys.tracebacklimit = None else: sys.tracebacklimit = 0 raise AttributeError('There is no data for at least one of your ' 'procedures, could you change the time ' 'parameter, observed properties, ' 'procedures or offerings') create_maps(parsed_obs, off, secondsGranularity) return 0
def main(): parsed_obs = dict() service = SensorObservationService(options['url'], version=options['version'], username=options['username'], password=options['password']) if any(value is True and key in ['o', 'v', 'p', 't'] for key, value in flags.items()): soslib.get_description(service, options, flags) soslib.check_missing_params(options['offering'], options['output']) if options['granularity'] != '': import grass.temporal as tgis tgis.init() seconds_granularity = int( tgis.gran_to_gran(options['granularity'], '1 second', True)) else: seconds_granularity = 1 if options['resolution'] == '': a = grass.read_command('g.region', flags='gf') resolution = float(a.split('nsres=')[1].split(' ')[0]) run_command('g.message', flags='w', message='No resolution was setted. Using the resolution ' '{} (nres of your current setting).'.format(resolution)) else: resolution = float(options['resolution']) if options['bbox'] != '': bbox = options['bbox'].split(',') run_command('g.region', n=float(bbox[0]), e=float(bbox[1]), s=float(bbox[2]), w=float(bbox[3]), res=resolution) else: grass.warning('You have not setted the bounding box. Bounding box will' ' be automatically based on procedure geometries for ' 'every map.') target = soslib.get_target_crs() for off in options['offering'].split(','): # TODO: Find better way than iteration (at best OWSLib upgrade) out = soslib.handle_not_given_options(service, off, options['procedure'], options['observed_properties'], options['event_time']) procedure, observed_properties, event_time = out if flags['s']: create_maps(_, off, _, resolution, _, service, target, procedure) else: try: obs = service.get_observation( offerings=[off], responseFormat=options['response_format'], observedProperties=observed_properties, procedure=procedure, eventTime=event_time, username=options['username'], password=options['password']) except: # TODO: catch errors properly (e.g. timeout) grass.fatal('Request did not succeed!') try: if options['version'] in ['1.0.0', '1.0'] and \ options['response_format'] == 'text/xml;subtype="om/1.0.0"': for prop in observed_properties: parsed_obs.update( {prop: soslib.xml2geojson(obs, prop)}) elif str(options['response_format']) == 'application/json': for prop in observed_properties: parsed_obs.update( {prop: soslib.json2geojson(obs, prop)}) except AttributeError: if sys.version_info[0] >= 3: sys.tracebacklimit = None else: sys.tracebacklimit = 0 raise AttributeError( 'There is no data for at least one of your procedures, ' 'could you change the time parameter, observed ' 'properties, procedures or offerings') except ValueError as e: if sys.version_info[0] >= 3: sys.tracebacklimit = None else: sys.tracebacklimit = 0 raise e create_maps(parsed_obs, off, seconds_granularity, resolution, event_time, service, target) return 0