def main(): group = options['group'] raster = options['raster'] output = options['output'] coords = options['east_north'] label = flags['c'] gnuplot = flags['g'] if not group and not raster: grass.fatal(_("Either group= or raster= is required")) if group and raster: grass.fatal(_("group= and raster= are mutually exclusive")) #check if present if gnuplot and not grass.find_program('gnuplot', ['-V']): grass.fatal(_("gnuplot required, please install first")) tmp1 = grass.tempfile() tmp2 = grass.tempfile() # get y-data for gnuplot-data file # get data from group files and set the x-axis labels if group: # ## PARSES THE GROUP FILES - gets rid of ugly header info from group list output s = grass.read_command('i.group', flags='g', group = group, quiet = True) rastermaps = s.splitlines() else: # ## get data from list of files and set the x-axis labels rastermaps = raster.split(',') xlabels = ["'%s' %d" % (n, i + 1) for i, n in enumerate(rastermaps)] xlabels = ','.join(xlabels) numbands = len(rastermaps) what = [] s = grass.read_command('r.what', input = rastermaps, east_north = coords, quiet = True) for l in s.splitlines(): f = l.split('|') for i, v in enumerate(f): if v in ['', '*']: f[i] = 0 else: f[i] = float(v) what.append(f) # build data files if gnuplot: draw_gnuplot(what, xlabels, output, label) else: draw_linegraph(what)
def main(): test_file = options['test'] expected = grass.tempfile() result = grass.tempfile() dbconn = grassdb.db_connection() grass.message(_("Using DB driver: %s") % dbconn['driver']) infile = os.path.join(os.environ['GISBASE'], 'etc', 'db.test', test_file) inf = file(infile) while True: type = inf.readline() if not type: break type = type.rstrip('\r\n') sql = inf.readline().rstrip('\r\n') sys.stdout.write(sql + '\n') # Copy expected result to temp file try: if type == 'X': grass.write_command('db.execute', input = '-', stdin = sql + '\n') else: resf = file(result, 'w') grass.write_command('db.select', input = '-', flags = 'c', stdin = sql + '\n', stdout = resf) resf.close() except CalledModuleError: grass.error("EXECUTE: ******** ERROR ********") else: grass.message(_("EXECUTE: OK")) expf = file(expected, 'w') while True: res = inf.readline().rstrip('\r\n') if not res: break expf.write(res + '\n') expf.close() if type == 'S': if grass.call(['diff', result, expected]) != 0: grass.error("RESULT: ******** ERROR ********") else: grass.message(_("RESULT: OK"))
def main(): out = options['output'] wfs_url = options['url'] tmp = grass.tempfile() tmpxml = tmp + '.xml' grass.message(_("Retrieving data...")) inf = urllib.urlopen(wfs_url) outf = file(tmpxml, 'wb') while True: s = inf.read() if not s: break outf.write(s) inf.close() outf.close() grass.message(_("Importing data...")) ret = grass.run_command('v.in.ogr', flags = 'o', dsn = tmpxml, out = out) grass.try_remove(tmpxml) if ret == 0: grass.message(_("Vector points map <%s> imported from WFS.") % out) else: grass.message(_("WFS import failed"))
def __init__(self, layer, mapfile, maskfile): if not haveGdal: sys.stderr.write(_("Unable to load GDAL Python bindings.\n"\ "WMS layers can not be displayed without the bindings.\n")) self.layer = layer wx.EvtHandler.__init__(self) # thread for d.wms commands self.thread = CmdThread(self) self.Bind(EVT_CMD_DONE, self.OnDataFetched) self.downloading = False self.renderedRegion = None self.updateMap = True self.fetched_data_cmd = None self.cmdStdErr = GStderr(self) self.mapfile = mapfile self.maskfile = maskfile self.tempMap = grass.tempfile() self.dstSize = {} self.Bind(EVT_CMD_OUTPUT, self.OnCmdOutput) self.dataFetched = Signal('RenderWMSMgr.dataFetched') self.updateProgress = Signal('RenderWMSMgr.updateProgress')
def __init__(self, gisrc = None, cmdfile = None, mapfile = None, envfile = None, monitor = None): """!Map composition (stack of map layers and overlays) @param gisrc alternative gisrc (used eg. by georectifier) @param cmdline full path to the cmd file (defined by d.mon) @param mapfile full path to the map file (defined by d.mon) @param envfile full path to the env file (defined by d.mon) @param monitor name of monitor (defined by d.mon) """ # region/extent settigns self.wind = dict() # WIND settings (wind file) self.region = dict() # region settings (g.region) self.width = 640 # map width self.height = 480 # map height # list of layers self.layers = list() # stack of available GRASS layer self.overlays = list() # stack of available overlays self.ovlookup = dict() # lookup dictionary for overlay items and overlays # environment settings self.env = dict() # path to external gisrc self.gisrc = gisrc self.cmdfile = cmdfile self.envfile = envfile self.monitor = monitor if mapfile: self.mapfileCmd = mapfile self.maskfileCmd = os.path.splitext(mapfile)[0] + '.pgm' # generated file for g.pnmcomp output for rendering the map self.mapfile = grass.tempfile(create = False) + '.ppm' # setting some initial env. variables self._initGisEnv() # g.gisenv self.GetWindow() # GRASS environment variable (for rendering) env = {"GRASS_BACKGROUNDCOLOR" : "FFFFFF", "GRASS_COMPRESSION" : "0", "GRASS_TRUECOLOR" : "TRUE", "GRASS_TRANSPARENT" : "TRUE", "GRASS_PNG_READ" : "FALSE", } self._writeEnvFile(env) self._writeEnvFile({"GRASS_PNG_READ" : "TRUE"}) for k, v in env.iteritems(): os.environ[k] = v # projection info self.projinfo = self._projInfo()
def _updateTtbByGlobalCosts(self, vectMapName, tlayer): #TODO get layer number do not use it directly intervals = self.turnsData["global"].GetData() cmdUpdGlob = [ "v.db.update", "map=", self.inputData["input"].GetValue(), "layer=%d" % tlayer, "column=cost", ] dbInfo = VectorDBInfo(vectMapName) table = dbInfo.GetTable(tlayer) driver, database = dbInfo.GetDbSettings(tlayer) sqlFile = grass.tempfile() sqlFile_f = open(sqlFile, 'w') for ival in intervals: from_angle = ival[0] to_angle = ival[1] cost = ival[2] if to_angle < from_angle: to_angle = math.pi * 2 + to_angle #if angle < from_angle: # angle = math.pi * 2 + angle where = " WHERE (((angle < {0}) AND ({2} + angle >= {0} AND {2} + angle < {1})) OR \ ((angle >= {0}) AND (angle >= {0} AND angle < {1}))) AND cost==0.0 ".format(str(from_angle), str(to_angle), str(math.pi * 2)) stm = ("UPDATE %s SET cost=%f " % (table, cost)) + where + ";\n"; sqlFile_f.write(stm) sqlFile_f.close() #TODO imporve parser and run in thread ret, msg, err = RunCommand('db.execute', getErrorMsg = True, input = sqlFile, read = True, driver = driver, database = database) try_remove(sqlFile)
def __init__(self, gisrc = None): """Map composition (stack of map layers and overlays) :param gisrc: alternative gisrc (used eg. by georectifier) """ # region/extent settigns self.wind = dict() # WIND settings (wind file) self.region = dict() # region settings (g.region) self.width = 640 # map width self.height = 480 # map height # list of layers self.layers = list() # stack of available GRASS layer self.overlays = list() # stack of available overlays self.ovlookup = dict() # lookup dictionary for overlay items and overlays # path to external gisrc self.gisrc = gisrc # generated file for g.pnmcomp output for rendering the map self.mapfile = grass.tempfile(create = False) + '.ppm' # setting some initial env. variables if not self.GetWindow(): sys.stderr.write(_("Trying to recover from default region...")) RunCommand('g.region', flags='d') # info to report progress self.progressInfo = None # GRASS environment variable (for rendering) self.default_env = {"GRASS_RENDER_BACKGROUNDCOLOR" : "000000", "GRASS_RENDER_FILE_COMPRESSION" : "0", "GRASS_RENDER_TRUECOLOR" : "TRUE", "GRASS_RENDER_TRANSPARENT" : "TRUE" } # projection info self.projinfo = self._projInfo() # is some layer being downloaded? self.downloading = False self.layerChanged = Signal('Map.layerChanged') self.updateProgress = Signal('Map.updateProgress') self.layerRemoved = Signal('Map:layerRemoved') self.layerAdded = Signal('Map:layerAdded')
def __array_finalize__(self, obj): if hasattr(obj, "_mmap"): self._mmap = obj._mmap self.filename = grasscore.tempfile() self.offset = obj.offset self.mode = obj.mode self._rows = obj._rows self._cols = obj._cols self._name = None self.mapset = "" self.reg = obj.reg self.overwrite = obj.overwrite self.mtype = obj.mtype self._fd = obj._fd else: self._mmap = None
def test_resampling_to_QImg_1(self): region = Region() region.from_rast(self.name) region.cols = 320 region.rows = 240 region.adjust() tmpfile = tempfile(False) tmpfile = tmpfile + ".png" a = raster2numpy_img(self.name, region) image = QImage(a.data, region.cols, region.rows, QImage.Format_ARGB32) #image.save("data/a.png") image.save(tmpfile) self.assertFilesEqualMd5(tmpfile, "data/a.png")
def test_resampling_to_QImg_4(self): region = Region() region.from_rast(self.name) region.cols = 400 region.rows = 300 region.adjust() tmpfile = tempfile(False) tmpfile = tmpfile + ".png" array = raster2numpy_img(rastname=self.name, region=region, color="RGB") image = QImage(array.data, region.cols, region.rows, QImage.Format_RGB32) #image.save("data/e.png") image.save(tmpfile) self.assertFilesEqualMd5(tmpfile, "data/e.png")
def __init__(self): # max number of steps in history (zero based) self.maxHistSteps = 3 # current history step self.currHistStep = 0 # number of steps saved in history self.histStepsNum = 0 # dict contains data saved in history for current history step self.currHistStepData = {} # buffer for data to be saved into history self.newHistStepData = {} self.histFile = grass.tempfile() # key/value separator self.sep = ';'
def __new__(cls, name, mapset="", mtype="CELL", mode="r+", overwrite=False): reg = Region() shape = (reg.rows, reg.cols) mapset = libgis.G_find_raster(name, mapset) gtype = None if mapset: # map exist, set the map type gtype = libraster.Rast_map_type(name, mapset) mtype = RTYPE_STR[gtype] filename = grasscore.tempfile() obj = np.memmap.__new__(cls, filename=filename, dtype=RTYPE[mtype]["numpy"], mode=mode, shape=shape) obj.mtype = mtype.upper() obj.gtype = gtype if gtype else RTYPE[mtype]["grass type"] obj._rows = reg.rows obj._cols = reg.cols obj.filename = filename obj._name = name obj.mapset = mapset obj.reg = reg obj.overwrite = overwrite return obj
def __init__(self, flags, options): self.flags = flags self.options = options self.tmp = grass.tempfile() self.suffixes = [] self.patches = [] self.maplist = [] self.tiler = 0 if not flags['p']: # todo: check if gdalwarp is available pass # flags for r.in.gdal self.gdal_flags = '' if flags['e']: self.gdal_flags += 'e' if flags['k']: self.gdal_flags += 'k'
def test_resampling_to_QImg_large(self): region = Region() region.from_rast(self.name) region.cols = 4000 region.rows = 3000 region.adjust() tmpfile = tempfile(False) tmpfile = tmpfile + ".png" # With array as argument array = np.ndarray((region.rows*region.cols*4), np.uint8) raster2numpy_img(rastname=self.name, region=region, color="ARGB", array=array) image = QImage(array.data, region.cols, region.rows, QImage.Format_ARGB32) #image.save("data/c.png") image.save(tmpfile) self.assertFilesEqualMd5(tmpfile, "data/c.png")
def __init__(self, layer, env): if not haveGdal: sys.stderr.write(_("Unable to load GDAL Python bindings.\n" "WMS layers can not be displayed without the bindings.\n")) self.layer = layer wx.EvtHandler.__init__(self) # thread for d.wms commands self.thread = gThread() self._startTime = None self.downloading = False self.renderedRegion = None self.updateMap = True self.fetched_data_cmd = None self.tempMap = grass.tempfile() self.dstSize = {} self.dataFetched = Signal('RenderWMSMgr.dataFetched') self.updateProgress = Signal('RenderWMSMgr.updateProgress')
def SaveHistStep(self): """Create new history step with data in buffer""" self.maxHistSteps = UserSettings.Get(group='vnet', key='other', subkey='max_hist_steps') self.currHistStep = 0 newHistFile = grass.tempfile() newHist = open(newHistFile, "w") self._saveNewHistStep(newHist) oldHist = open(self.histFile) removedHistData = self._savePreviousHist(newHist, oldHist) oldHist.close() newHist.close() try_remove(self.histFile) self.histFile = newHistFile self.newHistStepData.clear() return removedHistData
def __init__(self, gisrc = None): """Map composition (stack of map layers and overlays) :param gisrc: alternative gisrc (used eg. by georectifier) """ Debug.msg (1, "Map.__init__(): gisrc=%s" % gisrc) # region/extent settigns self.wind = dict() # WIND settings (wind file) self.region = dict() # region settings (g.region) self.width = 640 # map width self.height = 480 # map height # list of layers self.layers = list() # stack of available GRASS layer self.overlays = list() # stack of available overlays self.ovlookup = dict() # lookup dictionary for overlay items and overlays # path to external gisrc self.gisrc = gisrc # generated file for g.pnmcomp output for rendering the map self.mapfile = grass.tempfile(create = False) + '.ppm' # setting some initial env. variables if not self.GetWindow(): sys.stderr.write(_("Trying to recover from default region...")) RunCommand('g.region', flags='d') # projection info self.projinfo = self._projInfo() self.layerChanged = Signal('Map.layerChanged') self.layerRemoved = Signal('Map:layerRemoved') self.layerAdded = Signal('Map:layerAdded') self.renderMgr = RenderMapMgr(self)
def main(): out = options['output'] wfs_url = options['url'] request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0' wfs_url += request_base if options['name']: wfs_url += '&TYPENAME=' + options['name'] if options['srs']: wfs_url += '&SRS=' + options['srs'] if options['maximum_features']: wfs_url += '&MAXFEATURES=' + options['maximum_features'] if int(options['maximum_features']) < 1: grass.fatal('Invalid maximum number of features') if options['start_index']: wfs_url += '&STARTINDEX=' + options['start_index'] if int(options['start_index']) < 1: grass.fatal('Features begin with index "1"') if flags['r']: bbox = grass.read_command("g.region", flags = 'w').split('=')[1] wfs_url += '&BBOX=' + bbox if flags['l']: wfs_url = options['url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0' tmp = grass.tempfile() tmpxml = tmp + '.xml' grass.debug(wfs_url) grass.message(_("Retrieving data...")) inf = urllib.urlopen(wfs_url) outf = file(tmpxml, 'wb') while True: s = inf.read() if not s: break outf.write(s) inf.close() outf.close() if flags['l']: import shutil if os.path.exists('wms_capabilities.xml'): grass.fatal('A file called "wms_capabilities.xml" already exists here') # os.move() might fail if the temp file is on another volume, so we copy instead shutil.copy(tmpxml, 'wms_capabilities.xml') try_remove(tmpxml) sys.exit(0) grass.message(_("Importing data...")) ret = grass.run_command('v.in.ogr', flags = 'o', dsn = tmpxml, out = out) try_remove(tmpxml) if ret == 0: grass.message(_("Vector points map <%s> imported from WFS.") % out) else: grass.message(_("WFS import failed"))
def main(): size = int(options['size']) gamma = scale = None if options['gamma']: gamma = float(options['gamma']) if options['scaling_factor']: scale = float(options['scaling_factor']) input_dev = options['input'] output = options['output'] method = options['method'] if method in ('gravity', 'kernel') and (gamma is None or scale is None): gcore.fatal( _("Methods gravity and kernel require options scaling_factor and gamma" )) temp_map = 'tmp_futures_devPressure_' + str(os.getpid()) + '_copy' temp_map_out = 'tmp_futures_devPressure_' + str(os.getpid()) + '_out' temp_map_nulls = 'tmp_futures_devPressure_' + str(os.getpid()) + '_nulls' global TMP, TMPFILE if flags['n']: gcore.message(_("Preparing data...")) region = gcore.region() gcore.use_temp_region() gcore.run_command('g.region', n=region['n'] + size * region['nsres'], s=region['s'] - size * region['nsres'], e=region['e'] + size * region['ewres'], w=region['w'] - size * region['ewres']) TMP.append(temp_map) TMP.append(temp_map_nulls) TMP.append(temp_map_out) exp = "{temp_map_nulls} = if(isnull({inp}), 1, null())".format( temp_map_nulls=temp_map_nulls, inp=input_dev) grast.mapcalc(exp=exp) grast.mapcalc(exp="{temp} = if(isnull({inp}), 0, {inp})".format( temp=temp_map, inp=input_dev)) rmfilter_inp = temp_map rmfilter_out = temp_map_out else: rmfilter_inp = input_dev rmfilter_out = output matrix = distance_matrix(size) if method == 'occurrence': matrix[matrix > 0] = 1 elif method == 'gravity': with np.errstate(divide='ignore'): denom = np.power(matrix, gamma) matrix = scale / denom matrix[denom == 0] = 0 else: matrix_ = scale * np.exp(-2 * matrix / gamma) matrix = np.where(matrix > 0, matrix_, 0) path = gcore.tempfile() global TMPFILE TMPFILE = path with open(path, 'w') as f: f.write(write_filter(matrix)) gcore.message(_("Running development pressure filter...")) gcore.run_command('r.mfilter', input=rmfilter_inp, output=rmfilter_out, filter=path) if flags['n']: gcore.run_command( 'g.region', n=region['n'], s=region['s'], e=region['e'], w=region['w'], ) grast.mapcalc( exp="{out} = if(isnull({temp_null}), {rmfilter_out}, null())". format(temp_null=temp_map_nulls, rmfilter_out=rmfilter_out, out=output)) gcore.del_temp_region() grast.raster_history(output)
def _runAn(self, analysis, output, params, flags, catPts): """Called for all v.net.* analysis (except v.net.path)""" # Creates part of cmd fro analysis cmdParams = [analysis] cmdParams.extend(self._setInputParams(analysis, params, flags)) cmdParams.append("output=" + output) cats = self.data.GetAnalysisProperties()["cmdParams"]["cats"] if len(cats) > 1: for cat in cats: if len(catPts[cat[0]]) < 1: GMessage(parent=self, message=_("Please choose '%s' and '%s' point.") % (cats[0][1], cats[1][1])) return False else: for cat in cats: if len(catPts[cat[0]]) < 2: GMessage(parent=self, message=_("Please choose at least two points.")) return False # TODO add also to thread for analysis? vcatResult = RunCommand("v.category", input=params['input'], option="report", flags="g", read=True) vcatResult = vcatResult.splitlines() for cat in vcatResult: # TODO cat = cat.split() if "all" in cat: maxCat = int(cat[4]) break layerNum = params["node_layer"] pt_ascii, catsNums = self._getAsciiPts(catPts=catPts, maxCat=maxCat, layerNum=layerNum) # TODO better tmp files cleanup (make class for managing tmp files) self.tmpPtsAsciiFile = grass.tempfile() tmpPtsAsciiFileOpened = open(self.tmpPtsAsciiFile, 'w') tmpPtsAsciiFileOpened.write(pt_ascii) tmpPtsAsciiFileOpened.close() self.tmpInPts = AddTmpMapAnalysisMsg("vnet_tmp_in_pts", self.tmp_maps) if not self.tmpInPts: return False self.tmpInPtsConnected = AddTmpMapAnalysisMsg( "vnet_tmp_in_pts_connected", self.tmp_maps) if not self.tmpInPtsConnected: return False cmdParams.append("input=" + self.tmpInPtsConnected.GetVectMapName()) cmdParams.append("--overwrite") self._setCmdForSpecificAn(cmdParams) for catName, catNum in six.iteritems(catsNums): if catNum[0] == catNum[1]: cmdParams.append(catName + "=" + str(catNum[0])) else: cmdParams.append(catName + "=" + str(catNum[0]) + "-" + str(catNum[1])) # create and run commands which goes to analysis thread cmdVEdit = [ "v.edit", "map=" + self.tmpInPts.GetVectMapName(), "input=" + self.tmpPtsAsciiFile, "tool=create", "--overwrite", "-n" ] self._prepareCmd(cmdVEdit) self.goutput.RunCmd(command=cmdVEdit) cmdVNet = [ "v.net", "points=" + self.tmpInPts.GetVectMapName(), "input=" + params['input'], "output=" + self.tmpInPtsConnected.GetVectMapName(), "arc_layer=" + params["arc_layer"], "node_layer=" + params["node_layer"], "operation=connect", "thresh=" + str(params["max_dist"]), "--overwrite" ] # TODO snapping to nodes optimization self._prepareCmd(cmdVNet) self.goutput.RunCmd(command=cmdVNet) self._prepareCmd(cmdParams) self.goutput.RunCmd(command=cmdParams, onDone=self._runAnDone)
def main(): layers = options['map'].split(',') if len(layers) < 2: gcore.error(_("At least 2 maps are required")) tmpfile = gcore.tempfile() for map in layers: if not gcore.find_file(map, element='cell')['file']: gcore.fatal(_("Raster map <%s> not found") % map) try: gcore.write_command('d.text', color='black', size=4, line=1, stdin="CORRELATION") except CalledModuleError: return 1 os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE' colors = "red black blue green gray violet".split() line = 2 iloop = 0 jloop = 0 for iloop, i in enumerate(layers): for jloop, j in enumerate(layers): if i != j and iloop <= jloop: color = colors[0] colors = colors[1:] colors.append(color) gcore.write_command('d.text', color=color, size=4, line=line, stdin="%s %s" % (i, j)) line += 1 ofile = open(tmpfile, 'w') gcore.run_command('r.stats', flags='cnA', input=(i, j), stdout=ofile) ofile.close() ifile = open(tmpfile, 'r') first = True for l in ifile: f = l.rstrip('\r\n').split(' ') x = float(f[0]) y = float(f[1]) if first: minx = maxx = x miny = maxy = y first = False if minx > x: minx = x if maxx < x: maxx = x if miny > y: miny = y if maxy < y: maxy = y ifile.close() kx = 100.0 / (maxx - minx + 1) ky = 100.0 / (maxy - miny + 1) p = gcore.feed_command('d.graph', color=color) ofile = p.stdin ifile = open(tmpfile, 'r') for l in ifile: f = l.rstrip('\r\n').split(' ') x = float(f[0]) y = float(f[1]) ofile.write(b"icon + 0.1 %f %f\n" % ((x - minx + 1) * kx, (y - miny + 1) * ky)) ifile.close() ofile.close() p.wait() try_remove(tmpfile) return 0
def main(): coords = options['coordinates'] input = options['input'] output = options['output'] fs = options['separator'] proj_in = options['proj_in'] proj_out = options['proj_out'] ll_in = flags['i'] ll_out = flags['o'] decimal = flags['d'] copy_input = flags['e'] include_header = flags['c'] #### check for cs2cs if not grass.find_program('cs2cs'): grass.fatal(_("cs2cs program not found, install PROJ.4 first: http://proj.maptools.org")) #### check for overenthusiasm if proj_in and ll_in: grass.fatal(_("Choose only one input parameter method")) if proj_out and ll_out: grass.fatal(_("Choose only one output parameter method")) if ll_in and ll_out: grass.fatal(_("Choise only one auto-projection parameter method")) if output and not grass.overwrite() and os.path.exists(output): grass.fatal(_("Output file already exists")) if not coords and not input: grass.fatal(_("One of <coordinates> and <input> must be given")) if coords and input: grass.fatal(_("Options <coordinates> and <input> are mutually exclusive")) #### parse field separator # FIXME: input_x,y needs to split on multiple whitespace between them if fs == ',': ifs = ofs = ',' else: try: ifs, ofs = fs.split(',') except ValueError: ifs = ofs = fs ifs = separator(ifs) ofs = separator(ofs) #### set up projection params s = grass.read_command("g.proj", flags='j') kv = parse_key_val(s) if "XY location" in kv['+proj'] and (ll_in or ll_out): grass.fatal(_("Unable to project to or from a XY location")) in_proj = None if ll_in: in_proj = "+proj=longlat +datum=WGS84" grass.verbose("Assuming LL WGS84 as input, current projection as output ") if ll_out: in_proj = grass.read_command('g.proj', flags = 'jf') if proj_in: in_proj = proj_in if not in_proj: grass.verbose("Assuming current location as input") in_proj = grass.read_command('g.proj', flags = 'jf') in_proj = in_proj.strip() grass.verbose("Input parameters: '%s'" % in_proj) out_proj = None if ll_out: out_proj = "+proj=longlat +datum=WGS84" grass.verbose("Assuming current projection as input, LL WGS84 as output ") if ll_in: out_proj = grass.read_command('g.proj', flags = 'jf') if proj_out: out_proj = proj_out if not out_proj: grass.fatal(_("Missing output projection parameters ")) out_proj = out_proj.strip() grass.verbose("Output parameters: '%s'" % out_proj) #### set up input file if coords: x, y = coords.split(',') tmpfile = grass.tempfile() fd = open(tmpfile, "w") fd.write("%s%s%s\n" % (x, ifs, y)) fd.close() inf = file(tmpfile) else: if input == '-': infile = None inf = sys.stdin else: infile = input if not os.path.exists(infile): grass.fatal(_("Unable to read input data")) inf = file(infile) grass.debug("input file=[%s]" % infile) #### set up output file if not output: outfile = None outf = sys.stdout else: outfile = output outf = open(outfile, 'w') grass.debug("output file=[%s]" % outfile) #### set up output style if not decimal: outfmt = ["-w5"] else: outfmt = ["-f", "%.8f"] if not copy_input: copyinp = [] else: copyinp = ["-E"] #### do the conversion # Convert cs2cs DMS format to GRASS DMS format: # cs2cs | sed -e 's/d/:/g' -e "s/'/:/g" -e 's/"//g' cmd = ['cs2cs'] + copyinp + outfmt + in_proj.split() + ['+to'] + out_proj.split() p = grass.Popen(cmd, stdin = grass.PIPE, stdout = grass.PIPE) tr = TrThread(ifs, inf, p.stdin) tr.start() if not copy_input: if include_header: outf.write("x%sy%sz\n" % (ofs, ofs)) for line in p.stdout: try: xy, z = line.split(' ', 1) x, y = xy.split('\t') except ValueError: grass.fatal(line) outf.write('%s%s%s%s%s\n' % \ (x.strip(), ofs, y.strip(), ofs, z.strip())) else: if include_header: outf.write("input_x%sinput_y%sx%sy%sz\n" % (ofs, ofs, ofs, ofs)) for line in p.stdout: inXYZ, x, rest = line.split('\t') inX, inY = inXYZ.split(' ')[:2] y, z = rest.split(' ', 1) outf.write('%s%s%s%s%s%s%s%s%s\n' % \ (inX.strip(), ofs, inY.strip(), ofs, x.strip(), \ ofs, y.strip(), ofs, z.strip())) p.wait() if p.returncode != 0: grass.warning(_("Projection transform probably failed, please investigate"))
def main(): handler = options['handler'] if options['tempfile']: img_tmp = options['tempfile'] #TODO: add option for GRASS_RENDER_FILE_COMPRESSION=0,1-9 else: img_tmp = grass.tempfile() os.remove(img_tmp) img_tmp += ".bmp" if flags['b']: print('GRASS_RENDER_FILE="%s"' % img_tmp) if not os.environ.has_key("GRASS_RENDER_WIDTH"): print('GRASS_RENDER_WIDTH=%s' % options['width']) if not os.environ.has_key("GRASS_RENDER_HEIGHT"): print('GRASS_RENDER_HEIGHT=%s' % options['height']) if flags['c']: print('GRASS_RENDER_IMMEDIATE=cairo') else: print('GRASS_RENDER_IMMEDIATE=PNG') print('GRASS_RENDER_FILE_MAPPED=TRUE') print('GRASS_RENDER_FILE_READ=TRUE') print( 'export GRASS_RENDER_FILE GRASS_RENDER_WIDTH GRASS_RENDER_HEIGHT GRASS_RENDER_IMMEDIATE GRASS_RENDER_FILE_MAPPED GRASS_RENDER_FILE_READ;' ) print('d.erase bgcolor=%s;' % options['color']) if handler == "none": grass.message("Image file is '%s'" % img_tmp) elif handler == "qiv": print('qiv -e -T "%s" &' % img_tmp) # add --center ? else: print('%s image="%s" percent=%s &' % (handler, img_tmp, options['percent'])) sys.exit(0) if flags['d']: print('rem DOS export not yet implemented') sys.exit(0) ## rest of this won't work, as parent can't inherit from the child.. ## (unless we do some ugly g.gisenv) ## ... any ideas? end by running grass.call(['bash'])? if not grass.find_program(handler, '--help'): grass.fatal(_("'%s' not found.") % handler) os.environ['GRASS_RENDER_FILE'] = img_tmp if not os.environ.has_key("GRASS_RENDER_WIDTH"): os.environ['GRASS_RENDER_WIDTH'] = options['width'] if not os.environ.has_key("GRASS_RENDER_HEIGHT"): os.environ['GRASS_RENDER_HEIGHT'] = options['height'] if flags['c']: os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo' os.environ['GRASS_RENDER_FILE_MAPPED'] = 'TRUE' os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE' #? os.environ['GRASS_PNG_AUTO_WRITE'] = 'FALSE' grass.run_command('d.erase', bgcolor=options['color']) if handler == "qiv": ret = grass.call(['qiv', '-e', '-T', img_tmp]) else: ret = grass.exec_command(handler, image=img_tmp, percent=options['percent']) os.remove(img_tmp) sys.exit(ret)
def main(): global tmp tmp = grass.tempfile() extend = flags['e'] shellstyle = flags['g'] table = options['table'] column = options['column'] database = options['database'] driver = options['driver'] where = options['where'] perc = options['percentile'] perc = [float(p) for p in perc.split(',')] if not shellstyle: grass.message( _("Calculation for column <%s> of table <%s>...") % (column, table)) grass.message(_("Reading column values...")) sql = "SELECT %s FROM %s" % (column, table) if where: sql += " WHERE " + where if not database: database = None if not driver: driver = None tmpf = file(tmp, 'w') grass.run_command('db.select', flags='c', table=table, database=database, driver=driver, sql=sql, stdout=tmpf) tmpf.close() # check if result is empty tmpf = file(tmp) if tmpf.read(1) == '': grass.fatal(_("Table <%s> contains no data.") % table) tmpf.close() # calculate statistics if not shellstyle: grass.message(_("Calculating statistics...")) N = 0 sum = 0.0 sum2 = 0.0 sum3 = 0.0 minv = 1e300 maxv = -1e300 tmpf = file(tmp) for line in tmpf: if len(line.rstrip('\r\n')) == 0: continue x = float(line.rstrip('\r\n')) N += 1 sum += x sum2 += x * x sum3 += abs(x) maxv = max(maxv, x) minv = min(minv, x) tmpf.close() if N <= 0: grass.fatal(_("No non-null values found")) if not shellstyle: print "" print "Number of values: %d" % N print "Minimum: %.15g" % minv print "Maximum: %.15g" % maxv print "Range: %.15g" % (maxv - minv) print "-----" print "Mean: %.15g" % (sum / N) print "Arithmetic mean of absolute values: %.15g" % (sum3 / N) print "Variance: %.15g" % ((sum2 - sum * sum / N) / N) print "Standard deviation: %.15g" % (math.sqrt( (sum2 - sum * sum / N) / N)) print "Coefficient of variation: %.15g" % ((math.sqrt( (sum2 - sum * sum / N) / N)) / (math.sqrt(sum * sum) / N)) print "Sum: %.15g" % sum print "-----" else: print "n=%d" % N print "min=%.15g" % minv print "max=%.15g" % maxv print "range=%.15g" % (maxv - minv) print "mean=%.15g" % (sum / N) print "mean_abs=%.15g" % (sum3 / N) print "variance=%.15g" % ((sum2 - sum * sum / N) / N) print "stddev=%.15g" % (math.sqrt((sum2 - sum * sum / N) / N)) print "coeff_var=%.15g" % ((math.sqrt( (sum2 - sum * sum / N) / N)) / (math.sqrt(sum * sum) / N)) print "sum=%.15g" % sum if not extend: return # preparations: sortfile(tmp, tmp + ".sort") number = N odd = N % 2 eostr = ['even', 'odd'][odd] q25pos = round(N * 0.25) if q25pos == 0: q25pos = 1 q50apos = round(N * 0.50) if q50apos == 0: q50apos = 1 q50bpos = q50apos + (1 - odd) q75pos = round(N * 0.75) if q75pos == 0: q75pos = 1 ppos = {} pval = {} for i in range(len(perc)): ppos[i] = round(N * perc[i] / 100) if ppos[i] == 0: ppos[i] = 1 pval[i] = 0 inf = file(tmp + ".sort") l = 1 for line in inf: if l == q25pos: q25 = float(line.rstrip('\r\n')) if l == q50apos: q50a = float(line.rstrip('\r\n')) if l == q50bpos: q50b = float(line.rstrip('\r\n')) if l == q75pos: q75 = float(line.rstrip('\r\n')) for i in range(len(ppos)): if l == ppos[i]: pval[i] = float(line.rstrip('\r\n')) l += 1 q50 = (q50a + q50b) / 2 if not shellstyle: print "1st Quartile: %.15g" % q25 print "Median (%s N): %.15g" % (eostr, q50) print "3rd Quartile: %.15g" % q75 for i in range(len(perc)): if perc[i] == int(perc[i]): # integer if int(perc[i]) % 10 == 1 and int(perc[i]) != 11: print "%dst Percentile: %.15g" % (int(perc[i]), pval[i]) elif int(perc[i]) % 10 == 2 and int(perc[i]) != 12: print "%dnd Percentile: %.15g" % (int(perc[i]), pval[i]) elif int(perc[i]) % 10 == 3 and int(perc[i]) != 13: print "%drd Percentile: %.15g" % (int(perc[i]), pval[i]) else: print "%dth Percentile: %.15g" % (int(perc[i]), pval[i]) else: print "%.15g Percentile: %.15g" % (perc[i], pval[i]) else: print "first_quartile=%.15g" % q25 print "median=%.15g" % q50 print "third_quartile=%.15g" % q75 for i in range(len(perc)): percstr = "%.15g" % perc[i] percstr = percstr.replace('.', '_') print "percentile_%s=%.15g" % (percstr, pval[i])
def main(): out = options['output'] wfs_url = options['url'] request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0' wfs_url += request_base if options['name']: wfs_url += '&TYPENAME=' + options['name'] if options['srs']: wfs_url += '&SRS=' + options['srs'] if options['maximum_features']: wfs_url += '&MAXFEATURES=' + options['maximum_features'] if int(options['maximum_features']) < 1: # GTC Invalid WFS maximum features parameter grass.fatal(_("Invalid maximum number of features")) if options['start_index']: wfs_url += '&STARTINDEX=' + options['start_index'] if int(options['start_index']) < 1: # GTC Invalid WFS start index parameter grass.fatal(_('Features begin with index "1"')) if flags['r']: bbox = grass.read_command("g.region", flags='w').split('=')[1] wfs_url += '&BBOX=' + bbox if flags['l']: wfs_url = options['url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0' tmp = grass.tempfile() tmpxml = tmp + '.xml' grass.debug(wfs_url) # Set user and password if given if options['username'] and options['password']: grass.message(_("Setting username and password...")) if os.path.isfile(options['username']): with open(options['username']) as f: filecontent = f.read() user = filecontent.strip() elif options['username'] in os.environ: user = os.environ[options['username']] else: user = options['username'] if os.path.isfile(options['password']): with open(options['password']) as f: filecontent = f.read() pw = filecontent.strip() elif options['password'] in os.environ: pw = os.environ[options['password']] else: pw = options['password'] passmgr = HTTPPasswordMgrWithDefaultRealm() passmgr.add_password(None, wfs_url,user, pw) authhandler = HTTPBasicAuthHandler(passmgr) opener = build_opener(authhandler) install_opener(opener) # GTC Downloading WFS features grass.message(_("Retrieving data...")) try: inf = urlopen(wfs_url) except HTTPError as e: # GTC WFS request HTTP failure grass.fatal(_("The server couldn't fulfill the request.\nError code: %s") % e.code) except URLError as e: # GTC WFS request network failure grass.fatal(_("Failed to reach the server.\nReason: %s") % e.reason) outf = open(tmpxml, 'wb') while True: s = inf.read() if not s: break outf.write(s) inf.close() outf.close() if flags['l']: import shutil if os.path.exists('wms_capabilities.xml'): grass.fatal(_('A file called "wms_capabilities.xml" already exists here')) # os.move() might fail if the temp file is on another volume, so we copy instead shutil.copy(tmpxml, 'wms_capabilities.xml') try_remove(tmpxml) sys.exit(0) grass.message(_("Importing data...")) try: grass.run_command('v.in.ogr', flags='o', input=tmpxml, output=out) grass.message(_("Vector map <%s> imported from WFS.") % out) except: grass.message(_("WFS import failed")) finally: try_remove(tmpxml)
def _stream_direction(self, stream): """ Compute elevation of start/end point of stream parts. Add code of ascending stream part into attribute table. :param stream: vector stream features """ # calculate start_elev (point_x, point_y) + end_elev (point_x_1, point_y_1) columns = ['point_x', 'point_y'] for what in ('start', 'end'): # compute start/end elevation Module( 'v.to.points', input=stream, use=what, output=what, ) column = '{}_elev'.format(what) Module('v.what.rast', map=what, raster=self.dem_clip, column=self._data[column]) self._join_table(stream, self._primary_key, '{}_1'.format(what), self._primary_key, [column]) # start/end coordinates needs to be stored also in attribute table if what == 'end': columns = list(map(lambda x: x + '_1', columns)) Module('v.to.db', map=stream, option=what, columns=columns) # flip segments if end_elev > start_elev fields = [ self._primary_key, "start_elev", "end_elev", "point_x", "point_y", "point_x_1", "point_y_1" ] # TODO: rewrite using pygrass ret = Module('v.db.select', flags='c', map=stream, columns=fields, stdout_=PIPE) cats = {} for line in ret.outputs.stdout.splitlines(): row = line.split('|') if float(row[1]) < float(row[2]): cats[row[0]] = (row[1], row[2], row[3], row[4], row[5], row[6]) if cats: # flip stream direction (end_elev > start_elev) Module('v.edit', map=stream, tool='flip', cats=','.join(cats)) # update also attributes tmpfile = tempfile(create=False) with open(tmpfile, 'w') as fd: for k, v in cats.items(): # v (0:start_elev, 1:end_elev, # 2:point_x, 3:point_y, # 4:point_x_1, 5:point_y_1) # start_elev -> end_elev fd.write('UPDATE {} SET {} = {} WHERE {} = {};\n'.format( stream, fields[1], v[1], fields[0], k)) # end_elev -> start_elev fd.write('UPDATE {} SET {} = {} WHERE {} = {};\n'.format( stream, fields[2], v[0], fields[0], k)) # point_x -> point_x_1 fd.write('UPDATE {} SET {} = {} WHERE {} = {};\n'.format( stream, fields[3], v[4], fields[0], k)) # point_y -> point_y_1 fd.write('UPDATE {} SET {} = {} WHERE {} = {};\n'.format( stream, fields[4], v[5], fields[0], k)) # point_x_1 -> point_x fd.write('UPDATE {} SET {} = {} WHERE {} = {};\n'.format( stream, fields[5], v[2], fields[0], k)) # point_y_1 -> point_y fd.write('UPDATE {} SET {} = {} WHERE {} = {};\n'.format( stream, fields[6], v[3], fields[0], k)) Module('db.execute', input=tmpfile) # calculates to_node (fid of preceding segment) self._add_field(stream, "to_node", "DOUBLE", -9999) to_node = {} with VectorTopo(stream) as stream_vect: for line in stream_vect: start, end = line.nodes() cat = line.cat for start_line in start.lines(): if start_line.cat != cat: # category starts with 1, convert to indices to_node[cat - 1] = start_line.cat - 1 if to_node: # TODO: rewrite using pygrass tmpfile = tempfile(create=False) with open(tmpfile, 'w') as fd: for c, n in to_node.items(): fd.write( 'UPDATE {} SET to_node = {} WHERE {} = {};\n'.format( stream, n, self._primary_key, c)) Module('db.execute', input=tmpfile)
def main(): layers = options["map"].split(",") if len(layers) < 2: gcore.error(_("At least 2 maps are required")) tmpfile = gcore.tempfile() for map in layers: if not gcore.find_file(map, element="cell")["file"]: gcore.fatal(_("Raster map <%s> not found") % map) gcore.write_command("d.text", color="black", size=4, line=1, stdin="CORRELATION") os.environ["GRASS_RENDER_FILE_READ"] = "TRUE" colors = "red black blue green gray violet".split() line = 2 iloop = 0 jloop = 0 for iloop, i in enumerate(layers): for jloop, j in enumerate(layers): if i != j and iloop <= jloop: color = colors[0] colors = colors[1:] colors.append(color) gcore.write_command("d.text", color=color, size=4, line=line, stdin="%s %s" % (i, j)) line += 1 ofile = file(tmpfile, "w") gcore.run_command("r.stats", flags="cnA", input=(i, j), stdout=ofile) ofile.close() ifile = file(tmpfile, "r") first = True for l in ifile: f = l.rstrip("\r\n").split(" ") x = float(f[0]) y = float(f[1]) if first: minx = maxx = x miny = maxy = y first = False if minx > x: minx = x if maxx < x: maxx = x if miny > y: miny = y if maxy < y: maxy = y ifile.close() kx = 100.0 / (maxx - minx + 1) ky = 100.0 / (maxy - miny + 1) p = gcore.feed_command("d.graph", color=color) ofile = p.stdin ifile = file(tmpfile, "r") for l in ifile: f = l.rstrip("\r\n").split(" ") x = float(f[0]) y = float(f[1]) ofile.write("icon + 0.1 %f %f\n" % ((x - minx + 1) * kx, (y - miny + 1) * ky)) ifile.close() ofile.close() p.wait() try_remove(tmpfile)
def main(): fileorig = options['input'] filevect = options['output'] if not filevect: filevect = basename(fileorig, 'txt') #are we in LatLong location? s = grass.read_command("g.proj", flags='j') kv = parse_key_val(s) if kv['+proj'] != 'longlat': grass.fatal(_("This module only operates in LatLong/WGS84 locations")) #### setup temporary file tmpfile = grass.tempfile() coldescs = [("RC", "rc integer"), ("UFI", "uf1 integer"), ("UNI", "uni integer"), ("LAT", "lat double precision"), ("LONG", "lon double precision"), ("DMS_LAT", "dms_lat varchar(6)"), ("DMS_LONG", "dms_long varchar(7)"), ("UTM", "utm varchar(4)"), ("JOG", "jog varchar(7)"), ("FC", "fc varchar(1)"), ("DSG", "dsg varchar(5)"), ("PC", "pc integer"), ("CC1", "cci varchar(2)"), ("ADM1", "adm1 varchar(2)"), ("ADM2", "adm2 varchar(200)"), ("DIM", "dim integer"), ("CC2", "cc2 varchar(2)"), ("NT", "nt varchar(1)"), ("LC", "lc varchar(3)"), ("SHORT_FORM", "shortform varchar(128)"), ("GENERIC", "generic varchar(128)"), ("SORT_NAME", "sortname varchar(200)"), ("FULL_NAME", "fullname varchar(200)"), ("FULL_NAME_ND","funamesd varchar(200)"), ("MODIFY_DATE", "mod_date date")] colnames = [desc[0] for desc in coldescs] coltypes = dict([(desc[0], 'integer' in desc[1]) for desc in coldescs]) header = None num_places = 0 inf = file(fileorig) outf = file(tmpfile, 'wb') for line in inf: fields = line.rstrip('\r\n').split('\t') if not header: header = fields continue vars = dict(zip(header, fields)) fields2 = [] for col in colnames: if col in vars: if coltypes[col] and vars[col] == '': fields2.append('0') else: fields2.append(vars[col]) else: if coltypes[col]: fields2.append('0') else: fields2.append('') line2 = ';'.join(fields2) + '\n' outf.write(line2) num_places += 1 outf.close() inf.close() grass.message(_("Converted %d place names.") % num_places) #TODO: fix dms_lat,dms_long DDMMSS -> DD:MM:SS # Solution: # IN=DDMMSS # DEG=`echo $IN | cut -b1,2` # MIN=`echo $IN | cut -b3,4` # SEC=`echo $IN | cut -b5,6` # DEG_STR="$DEG:$MIN:$SEC" #modifications (to match DBF 10 char column name limit): # short_form -> shortform # sort_name -> sortname # full_name -> fullname # full_name_sd -> funamesd # pump data into GRASS: columns = [desc[1] for desc in coldescs] grass.run_command('v.in.ascii', cat = 0, x = 5, y = 4, sep = ';', input = tmpfile, output = filevect, columns = columns) try_remove(tmpfile) # write cmd history: vgrass.vector_history(filevect)
def testWrite(self): tmpfile = tempfile(False) cats = Category(self.name) cats.read() cats.write_rules(tmpfile) self.assertFilesEqualMd5(tmpfile, "data/geology_cats")
def main(): global tmp global sine_cosine_replic, outercircle, vector global totalvalidnumber, totalnumber, maxradius map = options['map'] undef = options['undef'] eps = options['eps'] xgraph = flags['x'] tmp = grass.tempfile() if eps and xgraph: grass.fatal(_("Please select only one output method")) #### check if we have xgraph (if no EPS output requested) if xgraph and not grass.find_program('xgraph'): grass.fatal(_("xgraph required, please install first (www.xgraph.org)")) ################################# # this file contains everthing: rawfile = tmp + "_raw" rawf = file(rawfile, 'w') grass.run_command('r.stats', flags = '1', input = map, stdout = rawf) rawf.close() rawf = file(rawfile) totalnumber = 0 for line in rawf: totalnumber += 1 rawf.close() grass.message(_("Calculating statistics for polar diagram... (be patient)")) #wipe out NULL data and undef data if defined by user # - generate degree binned to integer, eliminate NO DATA (NULL): # change 360 to 0 to close polar diagram: rawf = file(rawfile) nvals = 0 sumcos = 0 sumsin = 0 freq = {} for line in rawf: line = line.rstrip('\r\n') if line in ['*', undef]: continue nvals += 1 x = float(line) rx = math.radians(x) sumcos += math.cos(rx) sumsin += math.sin(rx) ix = round(x) if ix == 360: ix = 0 if ix in freq: freq[ix] += 1 else: freq[ix] = 1 rawf.close() totalvalidnumber = nvals if totalvalidnumber == 0: grass.fatal(_("No data pixel found")) ################################# # unit vector on raw data converted to radians without no data: unitvector = (sumcos / nvals, sumsin / nvals) ################################# # how many are there?: occurrences = [(math.radians(x), freq[x]) for x in freq] occurrences.sort() # find the maximum value maxradius = max([f for a, f in occurrences]) # now do cos() sin() sine_cosine = [(math.cos(a) * f, math.sin(a) * f) for a, f in occurrences] sine_cosine_replic = ['"Real data angles'] + sine_cosine + sine_cosine[0:1] if eps or xgraph: outercircle = [] outercircle.append('"All Data incl. NULLs') scale = 1.0 * totalnumber / totalvalidnumber * maxradius for i in range(0, 361): a = math.radians(i) x = math.cos(a) * scale y = math.sin(a) * scale outercircle.append((x, y)) # fix vector length to become visible (x? of $MAXRADIUS): vector = [] vector.append('"Avg. Direction\n') vector.append((0, 0)) vector.append((unitvector[0] * maxradius, unitvector[1] * maxradius)) ########################################################### # Now output: if eps: psout = basename(eps, 'eps') + '.eps' plot_eps(psout) elif xgraph: plot_xgraph() else: plot_dgraph() grass.message(_("Average vector:")) grass.message(_("direction: %.1f degrees CCW from East") % math.degrees(math.atan2(unitvector[1], unitvector[0]))) grass.message(_("magnitude: %.1f percent of fullscale") % (100 * math.hypot(unitvector[0], unitvector[1])))
def main(): filename = options['input'] type = options['type'] vect = options['output'] e00tmp = str(os.getpid()) # check for avcimport if not gcore.find_program('avcimport'): gcore.fatal( _("'avcimport' program not found, install it first") + "\n" + "http://avce00.maptools.org") # check for e00conv if not gcore.find_program('e00conv'): gcore.fatal( _("'e00conv' program not found, install it first") + "\n" + "http://avce00.maptools.org") # check that the user didn't use all three, which gets past the parser. if type not in ['point', 'line', 'area']: gcore.fatal(_('Must specify one of "point", "line", or "area".')) e00name = basename(filename, 'e00') # avcimport only accepts 13 chars: e00shortname = e00name[:13] # check if this is a split E00 file (.e01, .e02 ...): merging = False if os.path.exists(e00name + '.e01') or os.path.exists(e00name + '.E01'): gcore.message( _("Found that E00 file is split into pieces (.e01, ...)." " Merging...")) merging = True if vect: name = vect else: name = e00name # do import # make a temporary directory tmpdir = gcore.tempfile() try_remove(tmpdir) os.mkdir(tmpdir) files = glob.glob(e00name + '.e[0-9][0-9]') + glob.glob(e00name + '.E[0-9][0-9]') for f in files: shutil.copy(f, tmpdir) # change to temporary directory to later avoid removal problems (rm -r ...) os.chdir(tmpdir) # check for binay E00 file (we can just check if import fails): # avcimport doesn't set exist status :-( if merging: files.sort() filename = "%s.cat.%s.e00" % (e00name, e00tmp) outf = file(filename, 'wb') for f in files: inf = file(f, 'rb') shutil.copyfileobj(inf, outf) inf.close() outf.close() nuldev = file(os.devnull, 'w+') gcore.message(_("An error may appear next which will be ignored...")) if gcore.call(['avcimport', filename, e00shortname], stdout=nuldev, stderr=nuldev) == 1: gcore.message( _("E00 ASCII found and converted to Arc Coverage in " "current directory")) else: gcore.message( _("E00 Compressed ASCII found. Will uncompress first...")) try_remove(e00shortname) gcore.call(['e00conv', filename, e00tmp + '.e00']) gcore.message(_("...converted to Arc Coverage in current directory")) gcore.call(['avcimport', e00tmp + '.e00', e00shortname], stderr=nuldev) # SQL name fix: name = name.replace('-', '_') # let's import... gcore.message(_("Importing %ss...") % type) layer = dict(point='LAB', line='ARC', area=['LAB', 'ARC']) itype = dict(point='point', line='line', area='centroid') try: gcore.run_command('v.in.ogr', flags='o', input=e00shortname, layer=layer[type], type=itype[type], output=name) except CalledModuleError: gcore.fatal(_("An error occurred while running v.in.ogr")) gcore.message(_("Imported <%s> vector map <%s>.") % (type, name)) # clean up the mess for root, dirs, files in os.walk('.', False): for f in files: path = os.path.join(root, f) try_remove(path) for d in dirs: path = os.path.join(root, d) try_rmdir(path) os.chdir('..') os.rmdir(tmpdir) # end gcore.message(_("Done.")) # write cmd history: gvect.vector_history(name)
def main(): global tmp tmp = grass.tempfile() extend = flags['e'] shellstyle = flags['g'] table = options['table'] column = options['column'] database = options['database'] driver = options['driver'] where = options['where'] perc = options['percentile'] perc = [float(p) for p in perc.split(',')] if not shellstyle: grass.message(_("Calculation for column <%s> of table <%s>...") % (column, table)) grass.message(_("Reading column values...")) sql = "SELECT %s FROM %s" % (column, table) if where: sql += " WHERE " + where if not database: database = None if not driver: driver = None tmpf = file(tmp, 'w') grass.run_command('db.select', flags = 'c', table = table, database = database, driver = driver, sql = sql, stdout = tmpf) tmpf.close() # check if result is empty tmpf = file(tmp) if tmpf.read(1) == '': grass.fatal(_("Table <%s> contains no data.") % table) tmpf.close() # calculate statistics if not shellstyle: grass.message(_("Calculating statistics...")) N = 0 sum = 0.0 sum2 = 0.0 sum3 = 0.0 minv = 1e300 maxv = -1e300 tmpf = file(tmp) for line in tmpf: if len(line.rstrip('\r\n')) == 0: continue x = float(line.rstrip('\r\n')) N += 1 sum += x sum2 += x * x sum3 += abs(x) maxv = max(maxv, x) minv = min(minv, x) tmpf.close() if N <= 0: grass.fatal(_("No non-null values found")) if not shellstyle: print "" print "Number of values: %d" % N print "Minimum: %.15g" % minv print "Maximum: %.15g" % maxv print "Range: %.15g" % (maxv - minv) print "-----" print "Mean: %.15g" % (sum/N) print "Arithmetic mean of absolute values: %.15g" % (sum3/N) print "Variance: %.15g" % ((sum2 - sum*sum/N)/N) print "Standard deviation: %.15g" % (math.sqrt((sum2 - sum*sum/N)/N)) print "Coefficient of variation: %.15g" % ((math.sqrt((sum2 - sum*sum/N)/N))/(math.sqrt(sum*sum)/N)) print "Sum: %.15g" % sum print "-----" else: print "n=%d" % N print "min=%.15g" % minv print "max=%.15g" % maxv print "range=%.15g" % (maxv - minv) print "mean=%.15g" % (sum/N) print "mean_abs=%.15g" % (sum3/N) print "variance=%.15g" % ((sum2 - sum*sum/N)/N) print "stddev=%.15g" % (math.sqrt((sum2 - sum*sum/N)/N)) print "coeff_var=%.15g" % ((math.sqrt((sum2 - sum*sum/N)/N))/(math.sqrt(sum*sum)/N)) print "sum=%.15g" % sum if not extend: return # preparations: sortfile(tmp, tmp + ".sort") number = N odd = N % 2 eostr = ['even','odd'][odd] q25pos = round(N * 0.25) if q25pos == 0: q25pos = 1 q50apos = round(N * 0.50) if q50apos == 0: q50apos = 1 q50bpos = q50apos + (1 - odd) q75pos = round(N * 0.75) if q75pos == 0: q75pos = 1 ppos = {} pval = {} for i in range(len(perc)): ppos[i] = round(N * perc[i] / 100) if ppos[i] == 0: ppos[i] = 1 pval[i] = 0 inf = file(tmp + ".sort") l = 1 for line in inf: if l == q25pos: q25 = float(line.rstrip('\r\n')) if l == q50apos: q50a = float(line.rstrip('\r\n')) if l == q50bpos: q50b = float(line.rstrip('\r\n')) if l == q75pos: q75 = float(line.rstrip('\r\n')) for i in range(len(ppos)): if l == ppos[i]: pval[i] = float(line.rstrip('\r\n')) l += 1 q50 = (q50a + q50b) / 2 if not shellstyle: print "1st Quartile: %.15g" % q25 print "Median (%s N): %.15g" % (eostr, q50) print "3rd Quartile: %.15g" % q75 for i in range(len(perc)): if perc[i] == int(perc[i]): # integer if int(perc[i]) % 10 == 1 and int(perc[i]) != 11: print "%dst Percentile: %.15g" % (int(perc[i]), pval[i]) elif int(perc[i]) % 10 == 2 and int(perc[i]) != 12: print "%dnd Percentile: %.15g" % (int(perc[i]), pval[i]) elif int(perc[i]) % 10 == 3 and int(perc[i]) != 13: print "%drd Percentile: %.15g" % (int(perc[i]), pval[i]) else: print "%dth Percentile: %.15g" % (int(perc[i]), pval[i]) else: print "%.15g Percentile: %.15g" % (perc[i], pval[i]) else: print "first_quartile=%.15g" % q25 print "median=%.15g" % q50 print "third_quartile=%.15g" % q75 for i in range(len(perc)): percstr = "%.15g" % perc[i] percstr = percstr.replace('.','_') print "percentile_%s=%.15g" % (percstr, pval[i])
def main(): fileorig = options['input'] filevect = options['output'] if not filevect: filevect = basename(fileorig, 'txt') #are we in LatLong location? s = grass.read_command("g.proj", flags='j') kv = parse_key_val(s) if kv['+proj'] != 'longlat': grass.fatal(_("This module only operates in LatLong/WGS84 locations")) #### setup temporary file tmpfile = grass.tempfile() coldescs = [("RC", "rc integer"), ("UFI", "uf1 integer"), ("UNI", "uni integer"), ("LAT", "lat double precision"), ("LONG", "lon double precision"), ("DMS_LAT", "dms_lat varchar(6)"), ("DMS_LONG", "dms_long varchar(7)"), ("UTM", "utm varchar(4)"), ("JOG", "jog varchar(7)"), ("FC", "fc varchar(1)"), ("DSG", "dsg varchar(5)"), ("PC", "pc integer"), ("CC1", "cci varchar(2)"), ("ADM1", "adm1 varchar(2)"), ("ADM2", "adm2 varchar(200)"), ("DIM", "dim integer"), ("CC2", "cc2 varchar(2)"), ("NT", "nt varchar(1)"), ("LC", "lc varchar(3)"), ("SHORT_FORM", "shortform varchar(128)"), ("GENERIC", "generic varchar(128)"), ("SORT_NAME", "sortname varchar(200)"), ("FULL_NAME", "fullname varchar(200)"), ("FULL_NAME_ND", "funamesd varchar(200)"), ("MODIFY_DATE", "mod_date date")] colnames = [desc[0] for desc in coldescs] coltypes = dict([(desc[0], 'integer' in desc[1]) for desc in coldescs]) header = None num_places = 0 inf = file(fileorig) outf = file(tmpfile, 'wb') for line in inf: fields = line.rstrip('\r\n').split('\t') if not header: header = fields continue vars = dict(zip(header, fields)) fields2 = [] for col in colnames: if col in vars: if coltypes[col] and vars[col] == '': fields2.append('0') else: fields2.append(vars[col]) else: if coltypes[col]: fields2.append('0') else: fields2.append('') line2 = ';'.join(fields2) + '\n' outf.write(line2) num_places += 1 outf.close() inf.close() grass.message(_("Converted %d place names.") % num_places) #TODO: fix dms_lat,dms_long DDMMSS -> DD:MM:SS # Solution: # IN=DDMMSS # DEG=`echo $IN | cut -b1,2` # MIN=`echo $IN | cut -b3,4` # SEC=`echo $IN | cut -b5,6` # DEG_STR="$DEG:$MIN:$SEC" #modifications (to match DBF 10 char column name limit): # short_form -> shortform # sort_name -> sortname # full_name -> fullname # full_name_sd -> funamesd # pump data into GRASS: columns = [desc[1] for desc in coldescs] grass.run_command('v.in.ascii', cat=0, x=5, y=4, sep=';', input=tmpfile, output=filevect, columns=columns) try_remove(tmpfile) # write cmd history: vgrass.vector_history(filevect)
def testWrite(self): tmpfile = tempfile(False) cats = Category(self.name) cats.read() cats.write_rules(tmpfile) self.assertFilesEqualMd5(tmpfile, 'data/geology_cats')
def _runAn(self, analysis, output, params, flags, catPts): """Called for all v.net.* analysis (except v.net.path)""" # Creates part of cmd fro analysis cmdParams = [analysis] cmdParams.extend(self._setInputParams(analysis, params, flags)) cmdParams.append("output=" + output) cats = self.data.GetAnalysisProperties()["cmdParams"]["cats"] if len(cats) > 1: for cat in cats: if len(catPts[cat[0]]) < 1: GMessage(parent = self, message = _("Please choose '%s' and '%s' point.") \ % (cats[0][1], cats[1][1])) return False else: for cat in cats: if len(catPts[cat[0]]) < 2: GMessage(parent = self, message = _("Please choose at least two points.")) return False # TODO add also to thread for analysis? vcatResult = RunCommand("v.category", input = params['input'], option = "report", flags = "g", read = True) vcatResult = vcatResult.splitlines() for cat in vcatResult:#TODO cat = cat.split() if "all" in cat: maxCat = int(cat[4]) break layerNum = params["nlayer"] pt_ascii, catsNums = self._getAsciiPts (catPts = catPts, maxCat = maxCat, layerNum = layerNum) self.tmpPtsAsciiFile = grass.tempfile()#TODO better tmp files cleanup (make class for managing tmp files) tmpPtsAsciiFileOpened = open(self.tmpPtsAsciiFile, 'w') tmpPtsAsciiFileOpened.write(pt_ascii) tmpPtsAsciiFileOpened.close() self.tmpInPts = AddTmpMapAnalysisMsg("vnet_tmp_in_pts", self.tmp_maps) if not self.tmpInPts: return False self.tmpInPtsConnected = AddTmpMapAnalysisMsg("vnet_tmp_in_pts_connected", self.tmp_maps) if not self.tmpInPtsConnected: return False cmdParams.append("input=" + self.tmpInPtsConnected.GetVectMapName()) cmdParams.append("--overwrite") self._setCmdForSpecificAn(cmdParams) for catName, catNum in catsNums.iteritems(): if catNum[0] == catNum[1]: cmdParams.append(catName + "=" + str(catNum[0])) else: cmdParams.append(catName + "=" + str(catNum[0]) + "-" + str(catNum[1])) # create and run commands which goes to analysis thread cmdVEdit = [ "v.edit", "map=" + self.tmpInPts.GetVectMapName(), "input=" + self.tmpPtsAsciiFile, "tool=create", "--overwrite", "-n" ] self._prepareCmd(cmdVEdit) self.goutput.RunCmd(command = cmdVEdit) cmdVNet = [ "v.net", "points=" + self.tmpInPts.GetVectMapName(), "input=" + params['input'], "output=" + self.tmpInPtsConnected.GetVectMapName(), "alayer=" + params["alayer"], "nlayer=" + params["nlayer"], "operation=connect", "thresh=" + str(params["max_dist"]), "--overwrite" ] #TODO snapping to nodes optimization self._prepareCmd(cmdVNet) self.goutput.RunCmd(command = cmdVNet) self._prepareCmd(cmdParams) self.goutput.RunCmd(command = cmdParams, onDone = self._runAnDone)
def main(): fileorig = options["input"] filevect = options["output"] if not filevect: filevect = basename(fileorig, "txt") # are we in LatLong location? s = grass.read_command("g.proj", flags="j") kv = parse_key_val(s) if kv["+proj"] != "longlat": grass.fatal(_("This module only operates in LatLong/WGS84 locations")) #### setup temporary file tmpfile = grass.tempfile() coldescs = [ ("RC", "rc integer"), ("UFI", "uf1 integer"), ("UNI", "uni integer"), ("LAT", "lat double precision"), ("LONG", "lon double precision"), ("DMS_LAT", "dms_lat integer"), ("DMS_LONG", "dms_long integer"), ("MGRS", "mgrs varchar(15)"), ("JOG", "jog varchar(7)"), ("FC", "fc varchar(1)"), ("DSG", "dsg varchar(6)"), ("PC", "pc integer"), ("CC1", "cci varchar(255)"), ("ADM1", "adm1 varchar(2)"), ("POP", "pop integer"), ("ELEV", "elev double precision"), ("CC2", "cc2 varchar(255)"), ("NT", "nt varchar(2)"), ("LC", "lc varchar(3)"), ("SHORT_FORM", "shortform varchar(128)"), ("GENERIC", "generic varchar(128)"), ("SORT_NAME_RO", "sortnamero varchar(255)"), ("FULL_NAME_RO", "fullnamero varchar(255)"), ("FULL_NAME_ND_RO", "funamesdro varchar(255)"), ("SORT_NAME_RG", "sortnamerg varchar(255)"), ("FULL_NAME_RG", "fullnamerg varchar(255)"), ("FULL_NAME_ND_RG", "funamesdrg varchar(255)"), ("NOTE", "note varchar(4000)"), ("MODIFY_DATE", "mod_date date"), ("DISPLAY", "display varchar(255)"), ("NAME_RANK", "namerank integer"), ("NAME_LINK", "namelink integer"), ("TRANSL_CD", "translcd varchar(32)"), ("NM_MODIFY_DATE", "nmmodifydate varchar(10)"), ] colnames = [desc[0] for desc in coldescs] coltypes = dict([(desc[0], "integer" in desc[1]) for desc in coldescs]) header = None num_places = 0 inf = open(fileorig) outf = open(tmpfile, "wb") for line in inf: fields = line.rstrip("\r\n").split("\t") if not header: header = fields continue vars = dict(zip(header, fields)) fields2 = [] for col in colnames: if col in vars: if coltypes[col] and vars[col] == "": fields2.append("0") else: fields2.append(vars[col]) else: if coltypes[col]: fields2.append("0") else: fields2.append("") line2 = ";".join(fields2) + "\n" outf.write(line2) num_places += 1 outf.close() inf.close() grass.message(_("Converted %d place names.") % num_places) # TODO: fix dms_lat,dms_long DDMMSS -> DD:MM:SS # Solution: # IN=DDMMSS # DEG=`echo $IN | cut -b1,2` # MIN=`echo $IN | cut -b3,4` # SEC=`echo $IN | cut -b5,6` # DEG_STR="$DEG:$MIN:$SEC" # modifications (to match DBF 10 char column name limit): # short_form -> shortform # sort_name -> sortname # full_name -> fullname # full_name_sd -> funamesd # pump data into GRASS: columns = [desc[1] for desc in coldescs] grass.run_command( "v.in.ascii", cat=0, x=5, y=4, sep=";", input=tmpfile, output=filevect, columns=columns, ) try_remove(tmpfile) # write cmd history: vgrass.vector_history(filevect)
def main(): out = options['output'] wfs_url = options['url'] request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0' wfs_url += request_base if options['name']: wfs_url += '&TYPENAME=' + options['name'] if options['srs']: wfs_url += '&SRS=' + options['srs'] if options['maximum_features']: wfs_url += '&MAXFEATURES=' + options['maximum_features'] if int(options['maximum_features']) < 1: # GTC Invalid WFS maximum features parameter grass.fatal(_("Invalid maximum number of features")) if options['start_index']: wfs_url += '&STARTINDEX=' + options['start_index'] if int(options['start_index']) < 1: # GTC Invalid WFS start index parameter grass.fatal(_('Features begin with index "1"')) if flags['r']: bbox = grass.read_command("g.region", flags='w').split('=')[1] wfs_url += '&BBOX=' + bbox if flags['l']: wfs_url = options[ 'url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0' tmp = grass.tempfile() tmpxml = tmp + '.xml' grass.debug(wfs_url) # GTC Downloading WFS features grass.message(_("Retrieving data...")) try: inf = urlopen(wfs_url) except HTTPError as e: # GTC WFS request HTTP failure grass.fatal( _("The server couldn't fulfill the request.\nError code: %s") % e.code) except URLError as e: # GTC WFS request network failure grass.fatal(_("Failed to reach the server.\nReason: %s") % e.reason) outf = file(tmpxml, 'wb') while True: s = inf.read() if not s: break outf.write(s) inf.close() outf.close() if flags['l']: import shutil if os.path.exists('wms_capabilities.xml'): grass.fatal( _('A file called "wms_capabilities.xml" already exists here')) # os.move() might fail if the temp file is on another volume, so we copy instead shutil.copy(tmpxml, 'wms_capabilities.xml') try_remove(tmpxml) sys.exit(0) grass.message(_("Importing data...")) try: grass.run_command('v.in.ogr', flags='o', input=tmpxml, output=out) grass.message(_("Vector map <%s> imported from WFS.") % out) except: grass.message(_("WFS import failed")) finally: try_remove(tmpxml)
def maps_without_observations(offering, resolution, service, procedures, target): """Import just pixels/sensors without their observations. :param offering: A collection of sensors used to conveniently group them up :param resolution: 2D grid resolution for rasterization :param service: SensorObservationService() type object of request :param procedures: List of queried procedures (observation providors) :param target: """ offs = [o.id for o in service.offerings] off_idx = offs.index(offering) output_format = service.get_operation_by_name( 'DescribeSensor').parameters['outputFormat']['values'][0] if procedures: procedures = procedures.split(',') else: procedures = service.offerings[off_idx].procedures tempfile_path = grass.tempfile() n = None s = None e = None w = None with open(tempfile_path, 'w') as tempFile: for proc in procedures: response = service.describe_sensor(procedure=proc, output_format=output_format) root = SensorML(response) system = root.members[0] crs = int(system.location[0].attrib['srsName'].split(':')[-1]) coords = system.location[0][0].text.replace('\n', '') sx = float(coords.split(',')[0]) sy = float(coords.split(',')[1]) sz = float(coords.split(',')[2]) transform = soslib.get_transformation(crs, target) point = ogr.CreateGeometryFromWkt('POINT ({} {} {})'.format( sx, sy, sz)) point.Transform(transform) x = point.GetX() y = point.GetY() z = point.GetZ() tempFile.write('{} {} {}\n'.format(x, y, z)) if not n: n = y + resolution / 2 s = y - resolution / 2 e = x + resolution / 2 w = x - resolution / 2 else: if y >= n: n = y + resolution / 2 if y <= s: s = y - resolution / 2 if x >= e: e = x + resolution / 2 if x <= w: w = x - resolution / 2 run_command('g.region', n=n, s=s, w=w, e=e, res=resolution) run_command('r.in.xyz', input=tempfile_path, separator='space', output='{}_{}'.format(options['output'], offering))
def OnGetMaps(self, event): # wxGlade: wmsFrame.<event_handler> """ @description: called on press of getMaps button. Performs fetching of the Maps for the selected layers of a WMS Service. @todo:None @param self: reference variable @param event: event associated. @return: None """ if (self.selectedURL == "No server selected"): message = 'No server selected' grass.warning(message) self.ShowMessage(message, 'Warning') StatusBar_fields = [message] self.StatusBar.SetStatusText(StatusBar_fields[0], 0) return if (self.selectedEPSG is None): message = 'No EPSG code selected' grass.warning(message) StatusBar_fields = [message] self.StatusBar.SetStatusText(StatusBar_fields[0], 0) self.ShowMessage(message, 'Warning') return if (not self.selectedEPSG.isdigit()): message = 'EPSG code selected is not a number' grass.warning(message) StatusBar_fields = [message] self.StatusBar.SetStatusText(StatusBar_fields[0], 0) self.ShowMessage(message, 'Warning') return bbox = self.getBBOXParameters() #bbox = '584344,397868,585500,398500' self.url_in = self.selectedURL getMap_request_url = self.url_in getMap_request_url += '?service=WMS&request=GetMap&version=1.1.1&format=image/png&width=800&height=600&srs=EPSG:' + self.selectedEPSG + '&layers=' getMap_request_url += self.layerName + '&bbox=' + bbox print getMap_request_url req = Request(getMap_request_url) try: message = 'GetMaps request sent. Waiting for response...' StatusBar_fields = [message] self.StatusBar.SetStatusText(StatusBar_fields[0], 0) response = urlopen(req, None, self.timeoutValueSeconds) image = response.read() if (isServiceException(image)): message = 'Service Exception has occured' self.ShowMessage(message, 'Warning') grass.warning(message) StatusBar_fields = [message] self.StatusBar.SetStatusText(StatusBar_fields[0], 0) else: TMP = grass.tempfile() if TMP is None: grass.fatal("Unable to create temporary files") outfile = open(TMP, 'wb') outfile.write(image) outfile.close() if (imghdr.what(TMP) != 'png'): message = 'Not a valid PNG Image, Unable to display Map' self.ShowMessage(message, 'Warning') grass.warning(message) StatusBar_fields = [message] self.StatusBar.SetStatusText(StatusBar_fields[0], 0) return message = 'GetMap response obtained' grass.message(message) StatusBar_fields = [message] self.StatusBar.SetStatusText(StatusBar_fields[0], 0) NewImageFrame(TMP) except HTTPError, e: message = 'The server couldn\'t fulfill the request.' message = str(e)
def _vnetPathRunAn(self, analysis, output, params, flags, catPts): """Called when analysis is run for v.net.path module""" if self.pts_data.GetPointsCount() < 1: return False cats = self.data.GetAnalysisProperties()["cmdParams"]["cats"] # Creates part of cmd fro analysis cmdParams = [analysis] cmdParams.extend(self._setInputParams(analysis, params, flags)) cmdParams.append("output=" + output) cmdPts = [] for cat in cats: if len(catPts[cat[0]]) < 1: # TODO GMessage(message=_("Please choose '%s' and '%s' point.") % (cats[0][1], cats[1][1])) return False cmdPts.append(catPts[cat[0]][0]) resId = 1 inpPoints = str(resId) + " " + str(cmdPts[0][0]) + " " + str( cmdPts[0][1]) + " " + str(cmdPts[1][0]) + " " + str(cmdPts[1][1]) self.coordsTmpFile = grass.tempfile() coordsTmpFileOpened = open(self.coordsTmpFile, 'w') coordsTmpFileOpened.write(inpPoints) coordsTmpFileOpened.close() if flags["t"]: cmdParams.append("-t") self.tmpTurnAn = AddTmpMapAnalysisMsg("vnet_tunr_an_tmp", self.tmp_maps) if not self.tmpTurnAn: return False mapName, mapSet = ParseMapStr(self.tmpTurnAn.GetVectMapName()) cmdCopy = [ "g.copy", "vector=%s,%s" % (params["input"], mapName), "--overwrite", ] cmdParams.append("input=" + self.tmpTurnAn.GetVectMapName()) ret, msg, err = RunCommand('g.copy', getErrorMsg=True, vector="%s,%s" % (params['input'], mapName), read=True, overwrite=True) self._updateTtbByGlobalCosts(self.tmpTurnAn.GetVectMapName(), int(params["turn_layer"])) # self._prepareCmd(cmdCopy) #self.goutput.RunCmd(command = cmdCopy) else: cmdParams.append("input=" + params["input"]) cmdParams.append("file=" + self.coordsTmpFile) cmdParams.append("dmax=" + str(params["max_dist"])) cmdParams.append("--overwrite") self._prepareCmd(cmd=cmdParams) if flags["t"]: self.goutput.RunCmd(command=cmdParams, onDone=self._vnetPathRunTurnsAnDone) else: self.goutput.RunCmd(command=cmdParams, onDone=self._vnetPathRunAnDone)
def main(): indb = options["database"] prefix = options["basename"] env = grass.gisenv() # fix sqlite3 db field string multibyte character problem sys.setdefaultencoding("utf-8") # check if 3d or not if flags["z"]: d3 = "z" else: d3 = "" owrite = grass.overwrite() # check if location it is latlong if grass.locn_is_latlong(): locn = True else: locn = False # connection to sqlite geopaparazzi database import sqlite3 conn = sqlite3.connect(indb) curs = conn.cursor() # if it is not a latlong location create a latlong location on the fly if not locn: # create new location and move to it creating new gisrc file new_loc = basename(grass.tempfile(create=False)) new_loc_name = "geopaparazzi_%s" % new_loc grass.create_location( dbase=env["GISDBASE"], epsg="4326", location=new_loc_name, desc="Temporary location for v.in.geopaparazzi", ) grc = os.getenv("GISRC") shutil.copyfile(grc, grc + ".old") newrc = open(grc, "w") newrc.write("GISDBASE: %s\n" % env["GISDBASE"]) newrc.write("LOCATION_NAME: %s\n" % new_loc_name) newrc.write("MAPSET: PERMANENT\n") newrc.write("GRASS_GUI: text\n") newrc.close() grass.run_command("db.connect", flags="d", quiet=True) # load bookmarks if flags["b"]: # check if elements in bookmarks table are more the 0 if checkEle(curs, "bookmarks") != 0: bookname = prefix + "_book" pois = importGeom(bookname, "bookmarks", curs, owrite, "") sql = "CREATE TABLE %s (cat int, text text)" % bookname grass.write_command("db.execute", input="-", stdin=sql) # select attributes sql = "select text from bookmarks order by _id" allattri = returnClear(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%s'" % (idcat, str(row)) sql = "insert into %s values(%s)" % (bookname, values) grass.write_command("db.execute", input="-", stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command("v.db.connect", map=bookname, table=bookname, quiet=True) else: grass.warning(_("No bookmarks found, escape them")) # load images if flags["i"]: # check if elements in images table are more the 0 if checkEle(curs, "images") != 0: imagename = prefix + "_image" pois = importGeom(imagename, "images", curs, owrite, d3) sql = "CREATE TABLE %s (cat int, azim int, " % imagename sql += "path text, ts text, text text)" grass.write_command("db.execute", input="-", stdin=sql) # select attributes sql = "select azim, path, ts, text from images order by _id" allattri = returnAll(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%d','%s','%s','%s'" % ( idcat, row[0], str(row[1]), str(row[2]), str(row[3]), ) sql = "insert into %s values(%s)" % (imagename, values) grass.write_command("db.execute", input="-", stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command( "v.db.connect", map=imagename, table=imagename, quiet=True ) else: grass.warning(_("No images found, escape them")) # if tracks or nodes should be imported create a connection with sqlite3 # load notes if flags["n"]: # check if elements in notes table are more the 0 if checkEle(curs, "notes") != 0: # select each categories categories = returnClear(curs, "select cat from notes group by cat") # for each category for cat in categories: # select lat, lon for create point layer catname = prefix + "_notes_" + cat pois = importGeom(catname, "notes", curs, owrite, d3, cat) # select form to understand the number forms = returnClear( curs, "select _id from notes where cat = '%s' " "and form is not null order by _id" % cat, ) # if number of form is different from 0 and number of point # remove the vector because some form it is different if len(forms) != 0 and len(forms) != len(pois): grass.run_command( "g.remove", flags="f", type="vector", name=catname, quiet=True ) grass.warning( _( "Vector %s not imported because number" " of points and form is different" ) ) # if form it's 0 there is no form elif len(forms) == 0: # create table without form sql = "CREATE TABLE %s (cat int, ts text, " % catname sql += "text text, geopap_cat text)" grass.write_command("db.execute", input="-", stdin=sql) # select attributes sql = ( "select ts, text, cat from notes where " "cat='%s' order by _id" % cat ) allattri = returnAll(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%s','%s','%s'" % ( idcat, str(row[0]), str(row[1]), str(row[2]), ) sql = "insert into %s values(%s)" % (catname, values) grass.write_command("db.execute", input="-", stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command( "v.db.connect", map=catname, table=catname, quiet=True ) # create table with form else: # select all the attribute sql = ( "select ts, text, cat, form from notes where " "cat='%s' order by _id" % cat ) allattri = returnAll(curs, sql) # return string of form's categories too create table keys = returnFormKeys(allattri) sql = "CREATE TABLE %s (cat int, ts text, " % catname sql += "text text, geopap_cat text %s)" % keys grass.write_command("db.execute", input="-", stdin=sql) # it's for the number of categories idcat = 1 # for each feature insert value for row in allattri: values = "%d,'%s','%s','%s'," % ( idcat, str(row[0]), str(row[1]), str(row[2]), ) values += returnFormValues(row[3]) sql = "insert into %s values(%s)" % (catname, values) grass.write_command("db.execute", input="-", stdin=sql) idcat += 1 # at the end connect table with vector grass.run_command( "v.db.connect", map=catname, table=catname, quiet=True ) else: grass.warning(_("No notes found, escape them")) # load tracks if flags["t"]: # check if elements in bookmarks table are more the 0 if checkEle(curs, "gpslogs") != 0: tracksname = prefix + "_tracks" # define string for insert data at the end tracks = "" # return ids of tracks ids = returnClear(curs, "select _id from gpslogs") # for each track for i in ids: # select all the points coordinates tsel = "select lon, lat" if flags["z"]: tsel += ", altim" tsel += " from gpslog_data where logid=%s order by _id" % i trackpoints = returnAll(curs, tsel) wpoi = "\n".join( ["|".join([str(col) for col in row]) for row in trackpoints] ) tracks += "%s\n" % wpoi if flags["z"]: tracks += "NaN|NaN|Nan\n" else: tracks += "NaN|Nan\n" # import lines try: grass.write_command( "v.in.lines", flags=d3, input="-", out=tracksname, stdin=tracks, overwrite=owrite, quiet=True, ) except CalledModuleError: grass.fatal(_("Error importing %s" % tracksname)) # create table for line sql = "CREATE TABLE %s (cat int, startts text, " % tracksname sql += "endts text, text text, color text, width int)" grass.write_command("db.execute", input="-", stdin=sql) sql = ( "select logid, startts, endts, text, color, width from" " gpslogs, gpslogsproperties where gpslogs._id=" "gpslogsproperties.logid" ) # return attributes allattri = returnAll(curs, sql) # for each line insert attribute for row in allattri: values = "%d,'%s','%s','%s','%s',%d" % ( row[0], str(row[1]), str(row[2]), str(row[3]), str(row[4]), row[5], ) sql = "insert into %s values(%s)" % (tracksname, values) grass.write_command("db.execute", input="-", stdin=sql) # at the end connect map with table grass.run_command( "v.db.connect", map=tracksname, table=tracksname, quiet=True ) else: grass.warning(_("No tracks found, escape them")) # if location it's not latlong reproject it if not locn: # copy restore the original location shutil.copyfile(grc + ".old", grc) # reproject bookmarks if flags["b"] and checkEle(curs, "bookmarks") != 0: grass.run_command( "v.proj", quiet=True, input=bookname, location="geopaparazzi_%s" % new_loc, mapset="PERMANENT", ) # reproject images if flags["i"] and checkEle(curs, "images") != 0: grass.run_command( "v.proj", quiet=True, input=imagename, location="geopaparazzi_%s" % new_loc, mapset="PERMANENT", ) # reproject notes if flags["n"] and checkEle(curs, "notes") != 0: for cat in categories: catname = prefix + "_node_" + cat grass.run_command( "v.proj", quiet=True, input=catname, location="geopaparazzi_%s" % new_loc, mapset="PERMANENT", ) # reproject track if flags["t"] and checkEle(curs, "gpslogs") != 0: grass.run_command( "v.proj", quiet=True, input=tracksname, location="geopaparazzi_%s" % new_loc, mapset="PERMANENT", )
def main(): handler = options["handler"] if options["tempfile"]: img_tmp = options["tempfile"] # TODO: add option for GRASS_RENDER_FILE_COMPRESSION=0,1-9 else: img_tmp = grass.tempfile() os.remove(img_tmp) img_tmp += ".bmp" if flags["b"]: print('GRASS_RENDER_FILE="%s"' % img_tmp) if "GRASS_RENDER_WIDTH" not in os.environ: print("GRASS_RENDER_WIDTH=%s" % options["width"]) if "GRASS_RENDER_HEIGHT" not in os.environ: print("GRASS_RENDER_HEIGHT=%s" % options["height"]) if flags["c"]: print("GRASS_RENDER_IMMEDIATE=cairo") else: print("GRASS_RENDER_IMMEDIATE=PNG") print("GRASS_RENDER_FILE_MAPPED=TRUE") print("GRASS_RENDER_FILE_READ=TRUE") print( "export GRASS_RENDER_FILE GRASS_RENDER_WIDTH GRASS_RENDER_HEIGHT GRASS_RENDER_IMMEDIATE GRASS_RENDER_FILE_MAPPED GRASS_RENDER_FILE_READ;" ) print("d.erase bgcolor=%s;" % options["color"]) if handler == "none": grass.message("Image file is '%s'" % img_tmp) elif handler == "qiv": print('qiv -e -T "%s" &' % img_tmp) # add --center ? else: print('%s image="%s" percent=%s &' % (handler, img_tmp, options["percent"])) sys.exit(0) if flags["d"]: print("rem DOS export not yet implemented") sys.exit(0) ## rest of this won't work, as parent can't inherit from the child.. ## (unless we do some ugly g.gisenv) ## ... any ideas? end by running grass.call(['bash'])? if not grass.find_program(handler, "--help"): grass.fatal(_("'%s' not found.") % handler) os.environ["GRASS_RENDER_FILE"] = img_tmp if "GRASS_RENDER_WIDTH" not in os.environ: os.environ["GRASS_RENDER_WIDTH"] = options["width"] if "GRASS_RENDER_HEIGHT" not in os.environ: os.environ["GRASS_RENDER_HEIGHT"] = options["height"] if flags["c"]: os.environ["GRASS_RENDER_IMMEDIATE"] = "cairo" os.environ["GRASS_RENDER_FILE_MAPPED"] = "TRUE" os.environ["GRASS_RENDER_FILE_READ"] = "TRUE" # ? os.environ['GRASS_PNG_AUTO_WRITE'] = 'FALSE' grass.run_command("d.erase", bgcolor=options["color"]) if handler == "qiv": ret = grass.call(["qiv", "-e", "-T", img_tmp]) else: ret = grass.exec_command(handler, image=img_tmp, percent=options["percent"]) os.remove(img_tmp) sys.exit(ret)
def main(): global tmp fs = separator(options["separator"]) threeD = flags["z"] if threeD: do3D = "z" else: do3D = "" tmp = grass.tempfile() # set up input file if options["input"] == "-": infile = None inf = sys.stdin else: infile = options["input"] if not os.path.exists(infile): grass.fatal(_("Unable to read input file <%s>") % infile) grass.debug("input file=[%s]" % infile) if not infile: # read from stdin and write to tmpfile (v.in.mapgen wants a real file) outf = open(tmp, "w") for line in inf: if len(line.lstrip()) == 0 or line[0] == "#": continue outf.write(line.replace(fs, " ")) outf.close() runfile = tmp else: # read from a real file if fs == " ": runfile = infile else: inf = open(infile) outf = open(tmp, "w") for line in inf: if len(line.lstrip()) == 0 or line[0] == "#": continue outf.write(line.replace(fs, " ")) inf.close() outf.close() runfile = tmp # check that there are at least two columns (three if -z is given) inf = open(runfile) for line in inf: if len(line.lstrip()) == 0 or line[0] == "#": continue numcols = len(line.split()) break inf.close() if (do3D and numcols < 3) or (not do3D and numcols < 2): grass.fatal(_("Not enough data columns. (incorrect fs setting?)")) grass.run_command( "v.in.mapgen", flags="f" + do3D, input=runfile, output=options["output"] )
def main(): layers = options['map'].split(',') if len(layers) < 2: grass.error(_("At least 2 maps are required")) tmpfile = grass.tempfile() for map in layers: if not grass.find_file(map, element = 'cell')['file']: grass.fatal(_("Raster map <%s> not found") % map) grass.write_command('d.text', color = 'black', size = 4, line = 1, stdin = "CORRELATION") os.environ['GRASS_PNG_READ'] = 'TRUE' colors = "red black blue green gray violet".split() line = 2 iloop = 0 jloop = 0 for iloop, i in enumerate(layers): for jloop, j in enumerate(layers): if i != j and iloop <= jloop: color = colors[0] colors = colors[1:] colors.append(color) grass.write_command('d.text', color = color, size = 4, line = line, stdin = "%s %s" % (i, j)) line += 1 ofile = file(tmpfile, 'w') grass.run_command('r.stats', flags = 'cnA', input = (i, j), stdout = ofile) ofile.close() ifile = file(tmpfile, 'r') first = True for l in ifile: f = l.rstrip('\r\n').split(' ') x = float(f[0]) y = float(f[1]) if first: minx = maxx = x miny = maxy = y first = False if minx > x: minx = x if maxx < x: maxx = x if miny > y: miny = y if maxy < y: maxy = y ifile.close() kx = 100.0/(maxx-minx+1) ky = 100.0/(maxy-miny+1) p = grass.feed_command('d.graph', color = color) ofile = p.stdin ifile = file(tmpfile, 'r') for l in ifile: f = l.rstrip('\r\n').split(' ') x = float(f[0]) y = float(f[1]) ofile.write("icon + 0.1 %f %f\n" % ((x-minx+1) * kx, (y-miny+1) * ky)) ifile.close() ofile.close() p.wait() grass.try_remove(tmpfile)
def main(): global tmp fs = separator(options['separator']) threeD = flags['z'] prog = 'v.in.lines' if threeD: do3D = 'z' else: do3D = '' tmp = grass.tempfile() #### set up input file if options['input'] == '-': infile = None inf = sys.stdin else: infile = options['input'] if not os.path.exists(infile): grass.fatal(_("Unable to read input file <%s>") % infile) grass.debug("input file=[%s]" % infile) if not infile: # read from stdin and write to tmpfile (v.in.mapgen wants a real file) outf = file(tmp, 'w') for line in inf: if len(line.lstrip()) == 0 or line[0] == '#': continue outf.write(line.replace(fs, ' ')) outf.close() runfile = tmp else: # read from a real file if fs == ' ': runfile = infile else: inf = file(infile) outf = file(tmp, 'w') for line in inf: if len(line.lstrip()) == 0 or line[0] == '#': continue outf.write(line.replace(fs, ' ')) inf.close() outf.close() runfile = tmp ##### check that there are at least two columns (three if -z is given) inf = file(runfile) for line in inf: if len(line.lstrip()) == 0 or line[0] == '#': continue numcols = len(line.split()) break inf.close() if (do3D and numcols < 3) or (not do3D and numcols < 2): grass.fatal(_("Not enough data columns. (incorrect fs setting?)")) grass.run_command('v.in.mapgen', flags='f' + do3D, input=runfile, output=options['output'])
def main(): global tmp fs = separator(options['separator']) threeD = flags['z'] prog = 'v.in.lines' if threeD: do3D = 'z' else: do3D = '' tmp = grass.tempfile() #### set up input file if options['input'] == '-': infile = None inf = sys.stdin else: infile = options['input'] if not os.path.exists(infile): grass.fatal(_("Unable to read input file <%s>") % infile) grass.debug("input file=[%s]" % infile) if not infile: # read from stdin and write to tmpfile (v.in.mapgen wants a real file) outf = file(tmp, 'w') for line in inf: if len(line.lstrip()) == 0 or line[0] == '#': continue outf.write(line.replace(fs, ' ')) outf.close() runfile = tmp else: # read from a real file if fs == ' ': runfile = infile else: inf = file(infile) outf = file(tmp, 'w') for line in inf: if len(line.lstrip()) == 0 or line[0] == '#': continue outf.write(line.replace(fs, ' ')) inf.close() outf.close() runfile = tmp ##### check that there are at least two columns (three if -z is given) inf = file(runfile) for line in inf: if len(line.lstrip()) == 0 or line[0] == '#': continue numcols = len(line.split()) break inf.close() if (do3D and numcols < 3) or (not do3D and numcols < 2): grass.fatal(_("Not enough data columns. (incorrect fs setting?)")) grass.run_command('v.in.mapgen', flags = 'f' + do3D, input = runfile, output = options['output'])
def main(): coords = options['coordinates'] input = options['input'] output = options['output'] fs = options['separator'] proj_in = options['proj_in'] proj_out = options['proj_out'] ll_in = flags['i'] ll_out = flags['o'] decimal = flags['d'] copy_input = flags['e'] include_header = flags['c'] # check for cs2cs if not gcore.find_program('cs2cs'): gcore.fatal( _("cs2cs program not found, install PROJ.4 first: \ http://proj.maptools.org")) # parse field separator # FIXME: input_x,y needs to split on multiple whitespace between them if fs == ',': ifs = ofs = ',' else: try: ifs, ofs = fs.split(',') except ValueError: ifs = ofs = fs ifs = separator(ifs) ofs = separator(ofs) # set up projection params s = gcore.read_command("g.proj", flags='j') kv = parse_key_val(s) if "XY location" in kv['+proj'] and (ll_in or ll_out): gcore.fatal(_("Unable to project to or from a XY location")) in_proj = None if ll_in: in_proj = "+proj=longlat +datum=WGS84" gcore.verbose( "Assuming LL WGS84 as input, current projection as output ") if ll_out: in_proj = gcore.read_command('g.proj', flags='jf') if proj_in: if '+' in proj_in: in_proj = proj_in else: gcore.fatal(_("Invalid PROJ.4 input specification")) if not in_proj: gcore.verbose("Assuming current location as input") in_proj = gcore.read_command('g.proj', flags='jf') in_proj = in_proj.strip() gcore.verbose("Input parameters: '%s'" % in_proj) out_proj = None if ll_out: out_proj = "+proj=longlat +datum=WGS84" gcore.verbose( "Assuming current projection as input, LL WGS84 as output ") if ll_in: out_proj = gcore.read_command('g.proj', flags='jf') if proj_out: if '+' in proj_out: out_proj = proj_out else: gcore.fatal(_("Invalid PROJ.4 output specification")) if not out_proj: gcore.fatal(_("Missing output projection parameters ")) out_proj = out_proj.strip() gcore.verbose("Output parameters: '%s'" % out_proj) # set up input file if coords: x, y = coords.split(',') tmpfile = gcore.tempfile() fd = open(tmpfile, "w") fd.write("%s%s%s\n" % (x, ifs, y)) fd.close() inf = open(tmpfile) else: if input == '-': infile = None inf = sys.stdin else: infile = input if not os.path.exists(infile): gcore.fatal(_("Unable to read input data")) inf = open(infile) gcore.debug("input file=[%s]" % infile) # set up output file if not output: outfile = None outf = sys.stdout else: outfile = output outf = open(outfile, 'w') gcore.debug("output file=[%s]" % outfile) # set up output style if not decimal: outfmt = ["-w5"] else: outfmt = ["-f", "%.8f"] if not copy_input: copyinp = [] else: copyinp = ["-E"] # do the conversion # Convert cs2cs DMS format to GRASS DMS format: # cs2cs | sed -e 's/d/:/g' -e "s/'/:/g" -e 's/"//g' cmd = ['cs2cs'] + copyinp + outfmt + \ in_proj.split() + ['+to'] + out_proj.split() p = gcore.Popen(cmd, stdin=gcore.PIPE, stdout=gcore.PIPE) tr = TrThread(ifs, inf, p.stdin) tr.start() if not copy_input: if include_header: outf.write("x%sy%sz\n" % (ofs, ofs)) for line in p.stdout: try: xy, z = decode(line).split(' ', 1) x, y = xy.split('\t') except ValueError: gcore.fatal(line) outf.write('%s%s%s%s%s\n' % (x.strip(), ofs, y.strip(), ofs, z.strip())) else: if include_header: outf.write("input_x%sinput_y%sx%sy%sz\n" % (ofs, ofs, ofs, ofs)) for line in p.stdout: inXYZ, x, rest = decode(line).split('\t') inX, inY = inXYZ.split(' ')[:2] y, z = rest.split(' ', 1) outf.write('%s%s%s%s%s%s%s%s%s\n' % (inX.strip(), ofs, inY.strip(), ofs, x.strip(), ofs, y.strip(), ofs, z.strip())) p.wait() if p.returncode != 0: gcore.warning( _("Projection transform probably failed, please investigate"))
def main(): out = options['output'] wfs_url = options['url'] request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0' wfs_url += request_base if options['name']: wfs_url += '&TYPENAME=' + options['name'] if options['srs']: wfs_url += '&SRS=' + options['srs'] if options['maximum_features']: wfs_url += '&MAXFEATURES=' + options['maximum_features'] if int(options['maximum_features']) < 1: grass.fatal('Invalid maximum number of features') if options['start_index']: wfs_url += '&STARTINDEX=' + options['start_index'] if int(options['start_index']) < 1: grass.fatal('Features begin with index "1"') if flags['r']: bbox = grass.read_command("g.region", flags='w').split('=')[1] wfs_url += '&BBOX=' + bbox if flags['l']: wfs_url = options[ 'url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0' tmp = grass.tempfile() tmpxml = tmp + '.xml' grass.debug(wfs_url) grass.message(_("Retrieving data...")) inf = urllib.urlopen(wfs_url) outf = file(tmpxml, 'wb') while True: s = inf.read() if not s: break outf.write(s) inf.close() outf.close() if flags['l']: import shutil if os.path.exists('wms_capabilities.xml'): grass.fatal( 'A file called "wms_capabilities.xml" already exists here') # os.move() might fail if the temp file is on another volume, so we copy instead shutil.copy(tmpxml, 'wms_capabilities.xml') try_remove(tmpxml) sys.exit(0) grass.message(_("Importing data...")) try: grass.run_command('v.in.ogr', flags='o', input=tmpxml, output=out) grass.message(_("Vector points map <%s> imported from WFS.") % out) except: grass.message(_("WFS import failed")) finally: try_remove(tmpxml)
def main(): global tmp infile = options['input'] output = options['output'] matlab = flags['f'] threeD = flags['z'] prog = 'v.in.mapgen' opts = "" if not os.path.isfile(infile): grass.fatal(_("Input file <%s> not found") % infile) if output: name = output else: name = '' if threeD: matlab = True if threeD: do3D = 'z' else: do3D = '' tmp = grass.tempfile() # create ascii vector file inf = open(infile) outf = open(tmp, 'w') grass.message(_("Importing data...")) cat = 1 if matlab: # HB: OLD v.in.mapgen.sh Matlab import command follows. # I have no idea what it's all about, so "new" matlab format will be # a series of x y with "nan nan" breaking lines. (as NOAA provides) # Old command: # tac $infile | $AWK 'BEGIN { FS="," ; R=0 } # $1~/\d*/ { printf("L %d\n", R) } # $1~/ .*/ { printf(" %lf %lf\n", $2, $1) ; ++R } # $1~/END/ { }' | tac > "$TMP" # matlab format. points = [] for line in inf: f = line.split() if f[0].lower() == 'nan': if points != []: outf.write("L %d 1\n" % len(points)) for point in points: outf.write(" %.15g %.15g %.15g\n" % tuple(map(float, point))) outf.write(" 1 %d\n" % cat) cat += 1 points = [] else: if len(f) == 2: f.append('0') points.append(f) if points != []: outf.write("L %d 1\n" % len(points)) for point in points: try: outf.write(" %.15g %.15g %.15g\n" % tuple(map(float, point))) except ValueError: grass.fatal( _("An error occurred on line '%s', exiting.") % line.strip()) outf.write(" 1 %d\n" % cat) cat += 1 else: # mapgen format. points = [] for line in inf: if line[0] == '#': if points != []: outf.write("L %d 1\n" % len(points)) for point in points: outf.write(" %.15g %.15g\n" % tuple(map(float, point))) outf.write(" 1 %d\n" % cat) cat += 1 points = [] else: points.append(line.rstrip('\r\n').split('\t')) if points != []: outf.write("L %d 1\n" % len(points)) for point in points: outf.write(" %.15g %.15g\n" % tuple(map(float, point))) outf.write(" 1 %d\n" % cat) cat += 1 outf.close() inf.close() # create digit header digfile = tmp + '.dig' outf = open(digfile, 'w') t = string.Template("""ORGANIZATION: GRASSroots organization DIGIT DATE: $date DIGIT NAME: $user@$host MAP NAME: $name MAP DATE: $year MAP SCALE: 1 OTHER INFO: Imported with $prog ZONE: 0 MAP THRESH: 0 VERTI: """) date = time.strftime("%m/%d/%y") year = time.strftime("%Y") user = os.getenv('USERNAME') or os.getenv('LOGNAME') host = os.getenv('COMPUTERNAME') or os.uname()[1] s = t.substitute(prog=prog, name=name, date=date, year=year, user=user, host=host) outf.write(s) # process points list to ascii vector file (merge in vertices) inf = open(tmp) shutil.copyfileobj(inf, outf) inf.close() outf.close() if not name: # if no name for vector file given, cat to stdout inf = open(digfile) shutil.copyfileobj(inf, sys.stdout) inf.close() else: # import to binary vector file grass.message(_("Importing with v.in.ascii...")) try: grass.run_command('v.in.ascii', flags=do3D, input=digfile, output=name, format='standard') except CalledModuleError: grass.fatal( _('An error occurred on creating "%s", please check') % name)
def main(): global tmp infile = options['input'] output = options['output'] matlab = flags['f'] threeD = flags['z'] prog = 'v.in.mapgen' opts = "" if not os.path.isfile(infile): grass.fatal(_("Input file <%s> not found") % infile) if output: name = output else: name = '' if threeD: matlab = True if threeD: do3D = 'z' else: do3D = '' tmp = grass.tempfile() #### create ascii vector file inf = file(infile) outf = file(tmp, 'w') grass.message(_("Importing data...")) cat = 1 if matlab: ## HB: OLD v.in.mapgen.sh Matlab import command follows. ## I have no idea what it's all about, so "new" matlab format will be ## a series of x y with "nan nan" breaking lines. (as NOAA provides) ## Old command: # tac $infile | $AWK 'BEGIN { FS="," ; R=0 } # $1~/\d*/ { printf("L %d\n", R) } # $1~/ .*/ { printf(" %lf %lf\n", $2, $1) ; ++R } # $1~/END/ { }' | tac > "$TMP" ## matlab format. points = [] for line in inf: f = line.split() if f[0].lower() == 'nan': if points != []: outf.write("L %d 1\n" % len(points)) for point in points: outf.write(" %.15g %.15g %.15g\n" % tuple(map(float,point))) outf.write(" 1 %d\n" % cat) cat += 1 points = [] else: if len(f) == 2: f.append('0') points.append(f) if points != []: outf.write("L %d 1\n" % len(points)) for point in points: try: outf.write(" %.15g %.15g %.15g\n" % tuple(map(float, point))) except ValueError: grass.fatal(_("An error occurred on line '%s', exiting.") % line.strip()) outf.write(" 1 %d\n" % cat) cat += 1 else: ## mapgen format. points = [] for line in inf: if line[0] == '#': if points != []: outf.write("L %d 1\n" % len(points)) for point in points: outf.write(" %.15g %.15g\n" % tuple(map(float,point))) outf.write(" 1 %d\n" % cat) cat += 1 points = [] else: points.append(line.rstrip('\r\n').split('\t')) if points != []: outf.write("L %d 1\n" % len(points)) for point in points: outf.write(" %.15g %.15g\n" % tuple(map(float,point))) outf.write(" 1 %d\n" % cat) cat += 1 outf.close() inf.close() #### create digit header digfile = tmp + '.dig' outf = file(digfile, 'w') t = string.Template(\ """ORGANIZATION: GRASSroots organization DIGIT DATE: $date DIGIT NAME: $user@$host MAP NAME: $name MAP DATE: $year MAP SCALE: 1 OTHER INFO: Imported with $prog ZONE: 0 MAP THRESH: 0 VERTI: """) date = time.strftime("%m/%d/%y") year = time.strftime("%Y") user = os.getenv('USERNAME') or os.getenv('LOGNAME') host = os.getenv('COMPUTERNAME') or os.uname()[1] s = t.substitute(prog = prog, name = name, date = date, year = year, user = user, host = host) outf.write(s) #### process points list to ascii vector file (merge in vertices) inf = file(tmp) shutil.copyfileobj(inf, outf) inf.close() outf.close() if not name: #### if no name for vector file given, cat to stdout inf = file(digfile) shutil.copyfileobj(inf, sys.stdout) inf.close() else: #### import to binary vector file grass.message(_("Importing with v.in.ascii...")) try: grass.run_command('v.in.ascii', flags=do3D, input=digfile, output=name, format='standard') except CalledModuleError: grass.fatal(_('An error occurred on creating "%s", please check') % name)
def main(): indb = options['database'] prefix = options['basename'] env = grass.gisenv() #fix sqlite3 db field string multibyte character problem sys.setdefaultencoding('utf-8') # check if 3d or not if flags['z']: d3 = 'z' else: d3 = '' owrite = grass.overwrite() # check if location it is latlong if grass.locn_is_latlong(): locn = True else: locn = False # connection to sqlite geopaparazzi database import sqlite3 conn = sqlite3.connect(indb) curs = conn.cursor() # if it is not a latlong location create a latlong location on the fly if not locn: # create new location and move to it creating new gisrc file new_loc = basename(grass.tempfile(create=False)) new_loc_name = 'geopaparazzi_%s' % new_loc grass.create_location(dbase=env['GISDBASE'], epsg='4326', location=new_loc_name, desc='Temporary location for v.in.geopaparazzi') grc = os.getenv('GISRC') shutil.copyfile(grc, grc + '.old') newrc = open(grc, 'w') newrc.write('GISDBASE: %s\n' % env['GISDBASE']) newrc.write('LOCATION_NAME: %s\n' % new_loc_name) newrc.write('MAPSET: PERMANENT\n') newrc.write('GRASS_GUI: text\n') newrc.close() grass.run_command('db.connect', flags="d", quiet=True) # load bookmarks if flags['b']: # check if elements in bookmarks table are more the 0 if checkEle(curs, 'bookmarks') != 0: bookname = prefix + '_book' pois = importGeom(bookname, 'bookmarks', curs, owrite, '') sql = 'CREATE TABLE %s (cat int, text text)' % bookname grass.write_command('db.execute', input='-', stdin=sql) # select attributes sql = "select text from bookmarks order by _id" allattri = returnClear(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%s'" % (idcat, str(row)) sql = "insert into %s values(%s)" % (bookname, values) grass.write_command('db.execute', input='-', stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command('v.db.connect', map=bookname, table=bookname, quiet=True) else: grass.warning(_("No bookmarks found, escape them")) # load images if flags['i']: # check if elements in images table are more the 0 if checkEle(curs, 'images') != 0: imagename = prefix + '_image' pois = importGeom(imagename, 'images', curs, owrite, d3) sql = 'CREATE TABLE %s (cat int, azim int, ' % imagename sql += 'path text, ts text, text text)' grass.write_command('db.execute', input='-', stdin=sql) # select attributes sql = "select azim, path, ts, text from images order by _id" allattri = returnAll(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%d','%s','%s','%s'" % (idcat, row[0], str( row[1]), str(row[2]), str(row[3])) sql = "insert into %s values(%s)" % (imagename, values) grass.write_command('db.execute', input='-', stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command('v.db.connect', map=imagename, table=imagename, quiet=True) else: grass.warning(_("No images found, escape them")) # if tracks or nodes should be imported create a connection with sqlite3 # load notes if flags['n']: # check if elements in notes table are more the 0 if checkEle(curs, 'notes') != 0: # select each categories categories = returnClear(curs, "select cat from notes group by cat") # for each category for cat in categories: # select lat, lon for create point layer catname = prefix + '_notes_' + cat pois = importGeom(catname, 'notes', curs, owrite, d3, cat) # select form to understand the number forms = returnClear(curs, "select _id from notes where cat = '%s' " \ "and form is not null order by _id" % cat) # if number of form is different from 0 and number of point # remove the vector because some form it is different if len(forms) != 0 and len(forms) != len(pois): grass.run_command('g.remove', flags='f', type='vector', name=catname, quiet=True) grass.warning(_("Vector %s not imported because number" \ " of points and form is different")) # if form it's 0 there is no form elif len(forms) == 0: # create table without form sql = 'CREATE TABLE %s (cat int, ts text, ' % catname sql += 'text text, geopap_cat text)' grass.write_command('db.execute', input='-', stdin=sql) # select attributes sql = "select ts, text, cat from notes where "\ "cat='%s' order by _id" % cat allattri = returnAll(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%s','%s','%s'" % (idcat, str( row[0]), str(row[1]), str(row[2])) sql = "insert into %s values(%s)" % (catname, values) grass.write_command('db.execute', input='-', stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command('v.db.connect', map=catname, table=catname, quiet=True) # create table with form else: # select all the attribute sql = "select ts, text, cat, form from notes where "\ "cat='%s' order by _id" % cat allattri = returnAll(curs, sql) # return string of form's categories too create table keys = returnFormKeys(allattri) sql = 'CREATE TABLE %s (cat int, ts text, ' % catname sql += 'text text, geopap_cat text %s)' % keys grass.write_command('db.execute', input='-', stdin=sql) # it's for the number of categories idcat = 1 # for each feature insert value for row in allattri: values = "%d,'%s','%s','%s'," % (idcat, str( row[0]), str(row[1]), str(row[2])) values += returnFormValues(row[3]) sql = "insert into %s values(%s)" % (catname, values) grass.write_command('db.execute', input='-', stdin=sql) idcat += 1 # at the end connect table with vector grass.run_command('v.db.connect', map=catname, table=catname, quiet=True) else: grass.warning(_("No notes found, escape them")) # load tracks if flags['t']: # check if elements in bookmarks table are more the 0 if checkEle(curs, 'gpslogs') != 0: tracksname = prefix + '_tracks' # define string for insert data at the end tracks = '' # return ids of tracks ids = returnClear(curs, "select _id from gpslogs") # for each track for i in ids: # select all the points coordinates tsel = "select lon, lat" if flags['z']: tsel += ", altim" tsel += " from gpslog_data where logid=%s order by _id" % i trackpoints = returnAll(curs, tsel) wpoi = '\n'.join([ '|'.join([str(col) for col in row]) for row in trackpoints ]) tracks += "%s\n" % wpoi if flags['z']: tracks += 'NaN|NaN|Nan\n' else: tracks += 'NaN|Nan\n' # import lines try: grass.write_command('v.in.lines', flags=d3, input='-', out=tracksname, stdin=tracks, overwrite=owrite, quiet=True) except CalledModuleError: grass.fatal(_("Error importing %s" % tracksname)) # create table for line sql = 'CREATE TABLE %s (cat int, startts text, ' % tracksname sql += 'endts text, text text, color text, width int)' grass.write_command('db.execute', input='-', stdin=sql) sql = "select logid, startts, endts, text, color, width from" \ " gpslogs, gpslogsproperties where gpslogs._id=" \ "gpslogsproperties.logid" # return attributes allattri = returnAll(curs, sql) # for each line insert attribute for row in allattri: values = "%d,'%s','%s','%s','%s',%d" % (row[0], str( row[1]), str(row[2]), str(row[3]), str(row[4]), row[5]) sql = "insert into %s values(%s)" % (tracksname, values) grass.write_command('db.execute', input='-', stdin=sql) # at the end connect map with table grass.run_command('v.db.connect', map=tracksname, table=tracksname, quiet=True) else: grass.warning(_("No tracks found, escape them")) # if location it's not latlong reproject it if not locn: # copy restore the original location shutil.copyfile(grc + '.old', grc) # reproject bookmarks if flags['b'] and checkEle(curs, 'bookmarks') != 0: grass.run_command('v.proj', quiet=True, input=bookname, location='geopaparazzi_%s' % new_loc, mapset='PERMANENT') # reproject images if flags['i'] and checkEle(curs, 'images') != 0: grass.run_command('v.proj', quiet=True, input=imagename, location='geopaparazzi_%s' % new_loc, mapset='PERMANENT') # reproject notes if flags['n'] and checkEle(curs, 'notes') != 0: for cat in categories: catname = prefix + '_node_' + cat grass.run_command('v.proj', quiet=True, input=catname, location='geopaparazzi_%s' % new_loc, mapset='PERMANENT') # reproject track if flags['t'] and checkEle(curs, 'gpslogs') != 0: grass.run_command('v.proj', quiet=True, input=tracksname, location='geopaparazzi_%s' % new_loc, mapset='PERMANENT')
def main(): global tmp fs = separator(options["separator"]) threeD = flags["z"] prog = "v.in.lines" if threeD: do3D = "z" else: do3D = "" tmp = grass.tempfile() # set up input file if options["input"] == "-": infile = None inf = sys.stdin else: infile = options["input"] if not os.path.exists(infile): grass.fatal(_("Unable to read input file <%s>") % infile) grass.debug("input file=[%s]" % infile) if not infile: # read from stdin and write to tmpfile (v.in.mapgen wants a real file) outf = file(tmp, "w") for line in inf: if len(line.lstrip()) == 0 or line[0] == "#": continue outf.write(line.replace(fs, " ")) outf.close() runfile = tmp else: # read from a real file if fs == " ": runfile = infile else: inf = file(infile) outf = file(tmp, "w") for line in inf: if len(line.lstrip()) == 0 or line[0] == "#": continue outf.write(line.replace(fs, " ")) inf.close() outf.close() runfile = tmp # check that there are at least two columns (three if -z is given) inf = file(runfile) for line in inf: if len(line.lstrip()) == 0 or line[0] == "#": continue numcols = len(line.split()) break inf.close() if (do3D and numcols < 3) or (not do3D and numcols < 2): grass.fatal(_("Not enough data columns. (incorrect fs setting?)")) grass.run_command("v.in.mapgen", flags="f" + do3D, input=runfile, output=options["output"])
def main(): global tmp global sine_cosine_replic, outercircle, vector global totalvalidnumber, totalnumber, maxradius map = options['map'] undef = options['undef'] eps = options['output'] xgraph = flags['x'] tmp = gcore.tempfile() if eps and xgraph: gcore.fatal(_("Please select only one output method")) if eps: if os.sep in eps and not os.path.exists(os.path.dirname(eps)): gcore.fatal( _("EPS output file path <{}>, doesn't exists. " "Set new output file path.".format(eps))) else: eps = basename(eps, 'eps') + '.eps' if not eps.endswith('.eps'): eps += '.eps' if os.path.exists(eps) and not os.getenv('GRASS_OVERWRITE'): gcore.fatal( _("option <output>: <{}> exists. To overwrite, " "use the --overwrite flag.".format(eps))) # check if we have xgraph (if no EPS output requested) if xgraph and not gcore.find_program('xgraph'): gcore.fatal( _("xgraph required, please install first (www.xgraph.org)")) raster_map_required(map) ################################# # this file contains everything: rawfile = tmp + "_raw" rawf = open(rawfile, 'w') gcore.run_command('r.stats', flags='1', input=map, stdout=rawf) rawf.close() rawf = open(rawfile) totalnumber = 0 for line in rawf: totalnumber += 1 rawf.close() gcore.message( _("Calculating statistics for polar diagram... (be patient)")) # wipe out NULL data and undef data if defined by user # - generate degree binned to integer, eliminate NO DATA (NULL): # change 360 to 0 to close polar diagram: rawf = open(rawfile) nvals = 0 sumcos = 0 sumsin = 0 freq = {} for line in rawf: line = line.rstrip('\r\n') if line in ['*', undef]: continue nvals += 1 x = float(line) rx = math.radians(x) sumcos += math.cos(rx) sumsin += math.sin(rx) ix = round(x) if ix == 360: ix = 0 if ix in freq: freq[ix] += 1 else: freq[ix] = 1 rawf.close() totalvalidnumber = nvals if totalvalidnumber == 0: gcore.fatal(_("No data pixel found")) ################################# # unit vector on raw data converted to radians without no data: unitvector = (sumcos / nvals, sumsin / nvals) ################################# # how many are there?: occurrences = sorted([(math.radians(x), freq[x]) for x in freq]) # find the maximum value maxradius = max([f for a, f in occurrences]) # now do cos() sin() sine_cosine = [(math.cos(a) * f, math.sin(a) * f) for a, f in occurrences] sine_cosine_replic = ['"Real data angles'] + sine_cosine + sine_cosine[0:1] if eps or xgraph: outercircle = [] outercircle.append('"All Data incl. NULLs') scale = 1.0 * totalnumber / totalvalidnumber * maxradius for i in range(0, 361): a = math.radians(i) x = math.cos(a) * scale y = math.sin(a) * scale outercircle.append((x, y)) # fix vector length to become visible (x? of $MAXRADIUS): vector = [] vector.append('"Avg. Direction\n') vector.append((0, 0)) vector.append((unitvector[0] * maxradius, unitvector[1] * maxradius)) ########################################################### # Now output: if eps: plot_eps(psout=eps) elif xgraph: plot_xgraph() else: plot_dgraph() gcore.message(_("Average vector:")) gcore.message( _("direction: %.1f degrees CCW from East") % math.degrees(math.atan2(unitvector[1], unitvector[0]))) gcore.message( _("magnitude: %.1f percent of fullscale") % (100 * math.hypot(unitvector[0], unitvector[1])))
def main(): size = int(options["size"]) gamma = scale = None if options["gamma"]: gamma = float(options["gamma"]) if options["scaling_factor"]: scale = float(options["scaling_factor"]) input_dev = options["input"] output = options["output"] method = options["method"] if method in ("gravity", "kernel") and (gamma is None or scale is None): gcore.fatal( _("Methods gravity and kernel require options scaling_factor and gamma" )) temp_map = "tmp_futures_devPressure_" + str(os.getpid()) + "_copy" temp_map_out = "tmp_futures_devPressure_" + str(os.getpid()) + "_out" temp_map_nulls = "tmp_futures_devPressure_" + str(os.getpid()) + "_nulls" global TMP, TMPFILE if flags["n"]: gcore.message(_("Preparing data...")) region = gcore.region() gcore.use_temp_region() gcore.run_command( "g.region", n=region["n"] + size * region["nsres"], s=region["s"] - size * region["nsres"], e=region["e"] + size * region["ewres"], w=region["w"] - size * region["ewres"], ) TMP.append(temp_map) TMP.append(temp_map_nulls) TMP.append(temp_map_out) exp = "{temp_map_nulls} = if(isnull({inp}), 1, null())".format( temp_map_nulls=temp_map_nulls, inp=input_dev) grast.mapcalc(exp=exp) grast.mapcalc(exp="{temp} = if(isnull({inp}), 0, {inp})".format( temp=temp_map, inp=input_dev)) rmfilter_inp = temp_map rmfilter_out = temp_map_out else: rmfilter_inp = input_dev rmfilter_out = output matrix = distance_matrix(size) if method == "occurrence": matrix[matrix > 0] = 1 elif method == "gravity": with np.errstate(divide="ignore"): denom = np.power(matrix, gamma) matrix = scale / denom matrix[denom == 0] = 0 else: matrix_ = scale * np.exp(-2 * matrix / gamma) matrix = np.where(matrix > 0, matrix_, 0) path = gcore.tempfile() global TMPFILE TMPFILE = path with open(path, "w") as f: f.write(write_filter(matrix)) gcore.message(_("Running development pressure filter...")) gcore.run_command("r.mfilter", input=rmfilter_inp, output=rmfilter_out, filter=path) if flags["n"]: gcore.run_command( "g.region", n=region["n"], s=region["s"], e=region["e"], w=region["w"], ) grast.mapcalc( exp="{out} = if(isnull({temp_null}), {rmfilter_out}, null())". format(temp_null=temp_map_nulls, rmfilter_out=rmfilter_out, out=output)) gcore.del_temp_region() grast.raster_history(output)
def main(): filename = options['file'] type = options['type'] vect = options['vect'] e00tmp = str(os.getpid()) #### check for avcimport if not grass.find_program('avcimport'): grass.fatal(_("'avcimport' program not found, install it first") + "\n" + "http://avce00.maptools.org") #### check for e00conv if not grass.find_program('e00conv'): grass.fatal(_("'e00conv' program not found, install it first") + "\n" + "http://avce00.maptools.org") # check that the user didn't use all three, which gets past the parser. if type not in ['point','line','area']: grass.fatal(_('Must specify one of "point", "line", or "area".')) e00name = grass.basename(filename, 'e00') # avcimport only accepts 13 chars: e00shortname = e00name[:13] #check if this is a split E00 file (.e01, .e02 ...): merging = False if os.path.exists(e00name + '.e01') or os.path.exists(e00name + '.E01'): grass.message(_("Found that E00 file is split into pieces (.e01, ...). Merging...")) merging = True if vect: name = vect else: name = e00name ### do import #make a temporary directory tmpdir = grass.tempfile() grass.try_remove(tmpdir) os.mkdir(tmpdir) files = glob.glob(e00name + '.e[0-9][0-9]') + glob.glob(e00name + '.E[0-9][0-9]') for f in files: shutil.copy(f, tmpdir) #change to temporary directory to later avoid removal problems (rm -r ...) os.chdir(tmpdir) #check for binay E00 file (we can just check if import fails): #avcimport doesn't set exist status :-( if merging: files.sort() filename = "%s.cat.%s.e00" % (e00name, e00tmp) outf = file(filename, 'wb') for f in files: inf = file(f, 'rb') shutil.copyfileobj(inf, outf) inf.close() outf.close() nuldev = file(os.devnull, 'w+') grass.message(_("An error may appear next which will be ignored...")) if grass.call(['avcimport', filename, e00shortname], stdout = nuldev, stderr = nuldev) == 1: grass.message(_("E00 ASCII found and converted to Arc Coverage in current directory")) else: grass.message(_("E00 Compressed ASCII found. Will uncompress first...")) grass.try_remove(e00shortname) grass.try_remove(info) grass.call(['e00conv', filename, e00tmp + '.e00']) grass.message(_("...converted to Arc Coverage in current directory")) grass.call(['avcimport', e00tmp + '.e00', e00shortname], stderr = nuldev) #SQL name fix: name = name.replace('-', '_') ## let's import... grass.message(_("Importing %ss...") % type) layer = dict(point = 'LAB', line = 'ARC', area = ['LAB','ARC']) itype = dict(point = 'point', line = 'line', area = 'centroid') if grass.run_command('v.in.ogr', flags = 'o', dsn = e00shortname, layer = layer[type], type = itype[type], output = name) != 0: grass.fatal(_("An error occurred while running v.in.ogr")) grass.message(_("Imported <%s> vector map <%s>.") % (type, name)) #### clean up the mess for root, dirs, files in os.walk('.', False): for f in files: path = os.path.join(root, f) grass.try_remove(path) for d in dirs: path = os.path.join(root, d) grass.try_rmdir(path) os.chdir('..') os.rmdir(tmpdir) #### end grass.message(_("Done.")) # write cmd history: grass.vector_history(name)