def main():
    host = options['host']
    port = options['port']
    database = options['database']
    schema = options['schema']
    user = options['user']
    password = options['password']

    # Test connection
    conn = "dbname=" + database
    if host: conn += ",host=" + host
    if port: conn += ",port=" + port

    # Unfortunately we cannot test untill user/password is set 
    if user or password:
        print "Setting login (db.login) ... "
        sys.stdout.flush()
        if grass.run_command('db.login', driver = "pg", database = conn, user = user, password = password) != 0:
	    grass.fatal("Cannot login")

    # Try to connect
    print "Testing connection ..."
    sys.stdout.flush()
    if grass.run_command('db.select', quiet = True, flags='c', driver= "pg", database=conn, sql="select version()" ) != 0:
	if user or password:
	    print "Deleting login (db.login) ..."
	    sys.stdout.flush()
	    if grass.run_command('db.login', quiet = True, driver = "pg", database = conn, user = "", password = "") != 0:
		print "Cannot delete login."
		sys.stdout.flush()
        grass.fatal("Cannot connect to database.")
  
    if grass.run_command('db.connect', driver = "pg", database = conn, schema = schema) != 0:
        grass.fatal("Cannot connect to database.")
Exemplo n.º 2
0
def main():
    mon = grass.gisenv().get('MONITOR', None)
    if not mon:
        grass.fatal(_("No graphics device selected. Use d.mon to select graphics device."))

    monCmd = grass.parse_command('d.mon', flags='g').get('cmd', None)
    if not monCmd or not os.path.isfile(monCmd):
        grass.fatal(_("Unable to open file '%s'") % monCmd)

    try:
        fd = open(monCmd, 'r')
        cmdList = fd.readlines()

        grass.run_command('d.erase')

        for cmd in cmdList:
            grass.call(split(cmd))
    except IOError as e:
        grass.fatal(_("Unable to open file '%s' for reading. Details: %s") %
                    (monCmd, e))

    fd.close()

    # restore cmd file
    try:
        fd = open(monCmd, "w")
        fd.writelines(cmdList)
    except IOError as e:
        grass.fatal(_("Unable to open file '%s' for writing. Details: %s") %
                    (monCmd, e))

    return 0
Exemplo n.º 3
0
def main():
    env = grass.gisenv()
    mapset = env['MAPSET']
    ret = 0

    vectors = grass.list_grouped('vect')[mapset]
    num_vectors = len(vectors)
    
    if grass.verbosity() < 2:
        quiet = True
    else:
        quiet = False
        
    i = 1
    for vect in vectors:
        map = "%s@%s" % (vect, mapset)
        grass.message(_("%s\nBuilding topology for vector map <%s> (%d of %d)...\n%s") % \
                          ('-' * 80, map, i, num_vectors, '-' * 80))
        grass.verbose(_("v.build map=%s") % map)
        try:
            grass.run_command("v.build", map=map, quiet=quiet)
        except CalledModuleError:
            grass.error(_("Building topology for vector map <%s> failed") % map)
            ret = 1
        i += 1

    return ret
Exemplo n.º 4
0
def draw_linegraph(what):
    yfiles = []

    xfile = os.path.join(tmp_dir, 'data_x')

    xf = open(xfile, 'w')
    for j, val in enumerate(what[0][3:]):
        xf.write("%d\n" % (j + 1))
    xf.close()

    for i, row in enumerate(what):
        yfile = os.path.join(tmp_dir, 'data_y_%d' % i)
        yf = open(yfile, 'w')
        for j, val in enumerate(row[3:]):
            yf.write("%s\n" % val)
        yf.close()
        yfiles.append(yfile)

    sienna = '#%02x%02x%02x' % (160,  82, 45)
    coral = '#%02x%02x%02x' % (255, 127, 80)
    gp_colors = ['red', 'green', 'blue', 'magenta', 'cyan', sienna, 'orange',
                 coral]

    colors = gp_colors
    while len(what) > len(colors):
        colors += gp_colors
    colors = colors[0:len(what)]

    grass.run_command('d.linegraph', x_file=xfile, y_file=yfiles,
                      y_color=colors, title='Spectral signatures',
                      x_title='Bands', y_title='DN Value')
def generate_points(name, probability_map, count, overwrite=False):
    gcore.run_command('v.random.probability', output=name, probability=probability_map,
                      count=count, overwrite=overwrite)
    gcore.run_command('v.db.addtable', map=name,
                      columns="xshift double precision,"
                              "yshift double precision,"
                              "t integer")
Exemplo n.º 6
0
def main():
    """Main function, called at execution time"""

    # parse arguments
    input = options['input']
    var = options['var']
    prefix = options['prefix']

    # read NetCDF data
    nc = Dataset(input, 'r')
    data = nc.variables[var][:]
    nc.close()

    # set temporary region
    res = 32463.0
    rows = 277
    cols = 349
    grass.run_command('g.region',
                      n=res*(rows-0.5), s=res*-0.5,
                      e=res*(cols-0.5), w=res*-0.5,
                      rows=rows, cols=cols)

    # for each month
    a = garray.array()
    for (i, timeslice) in enumerate(data):
        mapname = prefix + '%02i' % (i+1)
        grass.message("Importing <%s> ..." % mapname)

        # import data with grass array
        a[:] = flipud(timeslice)
        a.write(mapname=mapname, overwrite=True, null=-32767)
Exemplo n.º 7
0
    def CleanUp(self, restore=True):
        """
        :param restore: if restore previous cursor, mouse['use']
        """
        try:
            gcore.run_command('g.remove', type='rast', flags='f', name=self._backupRasterName, quiet=True)
        except CalledModuleError:
            pass

        self._mapWindow.ClearLines(pdc=self._mapWindow.pdcTmp)
        self._mapWindow.mouse['end'] = self._mapWindow.mouse['begin']
        # disconnect mouse events
        if self._graphicsType:
            self._disconnectAll()
        # unregister
        self._mapWindow.UnregisterGraphicsToDraw(self._areas)
        self._mapWindow.UnregisterGraphicsToDraw(self._lines)
        self._mapWindow.UnregisterGraphicsToDraw(self._points)
        #self._registeredGraphics = None
        self._mapWindow.UpdateMap(render=False)

        if restore:
            # restore mouse['use'] and cursor to the state before measuring starts
            self._mapWindow.SetNamedCursor(self._oldCursor)
            self._mapWindow.mouse['use'] = self._oldMouseUse
def polylines(points_map, output, env):
    """Cluster points and connect points by line in each cluster"""
    tmp_cluster = 'tmp_cluster'
    gcore.run_command('v.cluster', flags='t', input=points_map, min=3,  layer='3', output=tmp_cluster, method='optics', env=env)
    cats = gcore.read_command('v.category', input=tmp_cluster, layer=3, option='print', env=env).strip()
    cats = list(set(cats.split()))
    line = ''
    for cat in cats:
        point_list = []
        distances = {}
        points = gcore.read_command('v.out.ascii', input=tmp_cluster, layer=3,
                                    type='point', cats=cat, format='point', env=env).strip().split()
        for point in points:
            point = point.split('|')[:2]
            point_list.append((float(point[0]), float(point[1])))
        for i, point1 in enumerate(point_list[:-1]):
            for point2 in point_list[i + 1:]:
                distances[(point1, point2)] = sqrt((point1[0] - point2[0]) * (point1[0] - point2[0]) +
                                                   (point1[1] - point2[1]) * (point1[1] - point2[1]))
        ordered = sorted(distances.items(), key=lambda x: x[1])[:len(points) - 1]
        for key, value in ordered:
            line += 'L 2 1\n'
            line += '{x} {y}\n'.format(x=key[0][0], y=key[0][1])
            line += '{x} {y}\n'.format(x=key[1][0], y=key[1][1])
            line += '1 {cat}\n\n'.format(cat=cat)
    gcore.write_command('v.in.ascii', input='-', stdin=line, output=output, format='standard', flags='n', env=env)
    gcore.run_command('v.to.rast', input=output, output=output, type='line', use='cat', env=env)
Exemplo n.º 9
0
def main():
    env = grass.gisenv()
    mon = env.get('MONITOR', None)
    if not mon:
        grass.fatal(_("No monitor selected. Run `d.mon` to select monitor."))
    
    monCmd = env.get('MONITOR_%s_CMDFILE' % mon.upper())
    if not monCmd:
        grass.fatal(_("No cmd file found for monitor <%s>") % mon)

    try:
        fd = open(monCmd, 'r')
        cmdList = fd.readlines()
        
        grass.run_command('d.erase')
        
        for cmd in cmdList:
            grass.call(split(cmd))
    except IOError as e:
        grass.fatal(_("Unable to open file '%s' for reading. Details: %s") % \
                        (monCmd, e))
    
    fd.close()
    
    # restore cmd file
    try:
        fd = open(monCmd, "w")
        fd.writelines(cmdList)
    except IOError as e:
        grass.fatal(_("Unable to open file '%s' for writing. Details: %s") % \
                        (monCmd, e))
    
    return 0
def cleanup():
    if len(TMP_RAST + TMP_VECT):
        gcore.info(_("Cleaning %d temporary maps...") % len(TMP_RAST + TMP_VECT))

    gcore.run_command('g.remove', rast=','.join(TMP_RAST), quiet=True)
    gcore.run_command('g.remove', vect=','.join(TMP_VECT), quiet=True)
    gcore.del_temp_region()
Exemplo n.º 11
0
def main():
    """Main function, called at execution time"""

    # parse arguments
    inputfile = options['input']
    outputmap = options['output']
    var = options['var']

    # read NetCDF data
    nc = Dataset(inputfile, 'r')
    lon = nc.variables['longitude'][:]
    lat = nc.variables['latitude'][:]
    z = nc.variables[var][:]
    nc.close()

    # check that coordinates are regular
    dlon = np.diff(lon)
    dlat = np.diff(lat)
    dlon0 = dlon[0]
    dlat0 = dlat[0]
    assert (dlon == dlon0).all()
    assert (dlat == dlat0).all()

    # crop illegal latitudes
    lat = lat[np.abs(lat) < 90-np.abs(dlat0)/2]

    # rotate longitudes and sort
    lon -= (lon > 180)*360
    lon_args = lon.argsort()
    lat_args = lat.argsort()[::-1]

    # crop and rotate data
    lat = lat[lat_args]
    lon = lon[lon_args]
    z = z[:, lat_args, :][:, :, lon_args]

    # set temporary region
    w = lon[-1] + dlon0/2
    e = lon[0] - dlon0/2
    s = lat[-1] - dlat0/2
    n = lat[0] + dlat0/2
    rows = len(lat)
    cols = len(lon)
    grass.run_command('g.region', w=w, e=e, s=s, n=n, rows=rows, cols=cols)

    # import time-independent data as such
    a = garray.array()
    if z.shape[0] == 1:
        a[:] = z[0]
        grass.message("Importing <%s> ..." % outputmap)
        a.write(mapname=outputmap, overwrite=True, null=-32767)

    # otherwise import each time slice
    else:
        for (i, data) in enumerate(z):
            mapname = outputmap + '%02i' % (i+1)
            grass.message("Importing <%s> ..." % mapname)
            a[:] = data
            a.write(mapname=mapname, overwrite=True, null=-32767)
def setColor(mapa):
    grass.run_command('v.colors',
                      map=mapa,
                      use='attr',
                      layer=2,
                      column='precip_mm_h',
                      rules=options['color']
                      )
def trail(scanned_elev, friction, walk_coeff, _lambda, slope_factor,
          walk, walk_dir, point_from, points_to, raster_route, vector_routes, env):
    gcore.run_command('r.walk',overwrite=True, flags='k', elevation=scanned_elev,
                      friction=friction, output=walk, start_coordinates=point_from, outdir=walk_dir,
                      stop_coordinates=points_to, walk_coeff=walk_coeff, _lambda=_lambda, slope_factor=slope_factor, env=env)
    for i in range(len(points_to)):
        gcore.run_command('r.drain', overwrite=True, input=walk, direction=walk_dir, flags='d', drain=vector_routes[i],
                          output=raster_route, start_coordinates=points_to[i], env=env)
def trail_salesman(trails, points, output, env):
    net_tmp = 'net_tmp'
    gcore.run_command('v.net', input=trails, points=points, output=net_tmp,
                      operation='connect', threshold=10, overwrite=True, env=env)
    cats = gcore.read_command('v.category', input=net_tmp, layer=2,
                              option='print', env=env).strip().split(os.linesep)
    gcore.run_command('v.net.salesman', input=net_tmp, output=output,
                      ccats=','.join(cats), alayer=1, nlayer=2, overwrite=True, env=env)
def subsurface_borehole(points, voxel, new, size, offset, axes, unit, env):
    coordinates = gcore.read_command('v.out.ascii', input=points, format='point', separator=',', env=env).strip()
    coords_list = []

    for coords in coordinates.split(os.linesep):
        coords_list.extend(coords.split(',')[:2])
    gcore.run_command('r3.borehole', overwrite=True, input=voxel, output=new,
                      coordinates=','.join(coords_list), size=size, offset_size=offset, axes=axes, unit=unit, env=env)
Exemplo n.º 16
0
def render(cmd, mapfile):
    env = os.environ.copy()
    if mapfile:
        env['GRASS_RENDER_FILE'] = mapfile
    try:
        grass.run_command(cmd[0], env=env, **cmd[1])
    except Exception as e:
        grass.debug(1, "Unable to render: {}".format(e))
Exemplo n.º 17
0
 def writeCircle(self, circle, rasterName):
     coords = self.mapWindow.Pixel2Cell(circle.point)
     RunCommand('r.circle', output=rasterName, max=circle.radius,
                coordinate=coords, flags="b")
     grass.use_temp_region()
     grass.run_command('g.region', zoom=rasterName)
     region = grass.region()
     marea = MaskedArea(region, rasterName, circle.radius)
     return marea
Exemplo n.º 18
0
    def _backupRaster(self, name):
        name = name.split('@')[0]
        backup = name + '_backupcopy_' + str(os.getpid())
        try:
            gcore.run_command('g.copy', rast=[name, backup], quiet=True)
        except CalledModuleError:
            raise ScriptError

        self._backupRasterName = backup
Exemplo n.º 19
0
    def OnOK(self, event):
        """Button 'OK' pressed"""
        mapsets = ','.join(self.GetMapsets())

        grass.run_command('g.mapsets',
                          quiet = True,
                          mapset = mapsets)
        
        self.Close()
Exemplo n.º 20
0
def copy(existingmap, newmap, maptype, **kwargs):
    """Copy a map

    >>> copy('census', 'mycensus', 'vect')
    >>> rename('mycensus', 'mynewcensus', 'vect')
    >>> remove('mynewcensus', 'vect')

    """
    kwargs.update({maptype: '{old},{new}'.format(old=existingmap, new=newmap)})
    grasscore.run_command('g.copy', quiet=True, **kwargs)
Exemplo n.º 21
0
def main():
    input = options['input']
    layer = options['layer']
    format = options['format']
    output = options['output']
    table = options['table']

    if format.lower() == 'dbf':
	format = "ESRI_Shapefile"

    if format.lower() == 'csv':
	olayer = basename(output, 'csv')
    else:
	olayer = None

    #is there a simpler way of testing for --overwrite?
    dbffile = input + '.dbf'
    if os.path.exists(dbffile) and not grass.overwrite():
	grass.fatal(_("File <%s> already exists") % dbffile)

    if olayer:
        try:
            grass.run_command('v.out.ogr', quiet=True, input=input, layer=layer,
                              output=output,
                              format=format, type='point,line,area',
                              olayer=olayer)
        except CalledModuleError:
            grass.fatal(_("Module <%s> failed") % 'v.out.ogr')

    else:
        try:
            grass.run_command('v.out.ogr', quiet=True, input=input,
                              layer=layer, output=output,
                              format=format, type='point,line,area')
        except CalledModuleError:
            grass.fatal(_("Module <%s> failed") % 'v.out.ogr')

    if format == "ESRI_Shapefile":
	exts = ['shp', 'shx', 'prj']
	if output.endswith('.dbf'):
	    outname = basename(output, 'dbf')
	    for ext in exts:
		try_remove("%s.%s" % (outname, ext))
	    outname += '.dbf'
	else:
	    for ext in exts:
		try_remove(os.path.join(output, "%s.%s" % (input, ext)))
	    outname = os.path.join(output, input + ".dbf")
    elif format.lower() == 'csv':
	outname = output + '.csv'
    else:
	outname = input

    grass.message(_("Exported table <%s>") % outname)
def rlake(scanned_elev, new, base, env, seed, level, **kwargs):
    suffix = str(uuid.uuid4()).replace('-', '')[:5]
    match = 'tmp_match' + suffix
    params = {}
    if isinstance(seed, list):
        params['coordinates'] = ','.join(str(each) for each in seed)
    else:
        params['seed'] = seed
    match_scan(base=base, scan=scanned_elev, matched=match, env=env)
    gcore.run_command('r.lake', elevation=match, water_level=level, lake=new, env=env, **params)
    gcore.run_command('g.remove', flags='f', type='raster', name=[match])
Exemplo n.º 23
0
def erase(monitor):
    # remove frames
    lines = []
    for line in read_monitor_file(monitor):
        if 'FRAME' not in line:
            lines.append(line)

    write_monitor_file(monitor, lines)

    # erase screen
    run_command('d.erase')
Exemplo n.º 24
0
def registerPid(pid):
    """Register process id as GUI_PID GRASS variable

    :param: pid process id
    """
    env = grass.gisenv()
    guiPid = []
    if 'GUI_PID' in env:
        guiPid = env['GUI_PID'].split(',')
    guiPid.append(str(pid))
    grass.run_command('g.gisenv', set='GUI_PID={}'.format(','.join(guiPid)))
Exemplo n.º 25
0
def main():
    options, unused = gcore.parser()

    drape_map = options['color']
    relief_map = options['shade']
    brighten = options['brighten']

    try:
        gcore.run_command('d.his', hue=drape_map, intensity=relief_map,
                          brighten=brighten)
    except CalledModuleError:
        gcore.fatal(_("Module %s failed. Check the above error messages.") % 'd.his')
def depression(scanned_elev, new, env, filter_depth=0, repeat=2):
    """Run r.fill.dir to compute depressions"""
    suffix = str(uuid.uuid4()).replace('-', '')[:5]
    input_dem = scanned_elev
    output = "tmp_filldir" + suffix
    tmp_dir = "tmp_dir" + suffix
    for i in range(repeat):
        gcore.run_command('r.fill.dir', input=input_dem, output=output, direction=tmp_dir, env=env)
        input_dem = output
    grast.mapcalc('{new} = if({out} - {scan} > {depth}, {out} - {scan}, null())'.format(new=new, out=output, scan=scanned_elev, depth=filter_depth), env=env)
    gcore.write_command('r.colors', map=new, rules='-', stdin='0% aqua\n100% blue', env=env)
    gcore.run_command('g.remove', flags='f', type='raster', name=[output, tmp_dir], env=env)
Exemplo n.º 27
0
    def _backupRaster(self, name):
        """Creates a temporary backup raster necessary for undo behavior.

        :param str name: name of raster map for which we create backup
        """
        name = name.split('@')[0]
        backup = name + '_backupcopy_' + str(os.getpid())
        try:
            gcore.run_command('g.copy', raster=[name, backup], quiet=True)
        except CalledModuleError:
            raise ScriptError

        self._backupRasterName = backup
def viewshed(scanned_elev, output, vector, visible_color, invisible_color, obs_elev=1.7, env=None):
    coordinates = gcore.read_command('v.out.ascii', input=vector, separator=',', env=env).strip()
    coordinate = None
    for line in coordinates.split(os.linesep):
        try:
            coordinate = [float(c) for c in line.split(',')[0:2]]
        except ValueError:  # no points in map
            pass
        break
    if coordinate:
        gcore.run_command('r.viewshed', flags='b', input=scanned_elev, output=output, coordinates=coordinate, observer_elevation=obs_elev, env=env)
        gcore.run_command('r.null', map=output, null=0, env=env)
        gcore.write_command('r.colors', map=output,  rules='-', stdin='0 {invis}\n1 {vis}'.format(vis=visible_color, invis=invisible_color), env=env)
def main():
    options, flags = gcore.parser()
    probability = options['probability']
    output = options['output']
    count = int(options['count'])

    gcore.use_temp_region()

    # probability map
    probab_01 = 'probability_01_' + str(os.getpid())
    TMP_RAST.append(probab_01)
    info = grast.raster_info(probability)
    gcore.write_command('r.recode', flags='d', input=probability, output=probab_01,
                        title="Recoded probability map to 0 to 1",
                        rules='-', stdin='{minim}:{maxim}:0:1'.format(minim=info['min'], maxim=info['max']))
    mean = gcore.parse_key_val(gcore.read_command('r.univar', map=probab_01, flags='g'),
                               val_type=float)['mean']
    resolution = count / (mean * (info['north'] - info['south'] + info['east'] - info['west']))
    resolution = sqrt((mean * (info['north'] - info['south']) * (info['east'] - info['west'])) / count)
    gcore.run_command('g.region', res=resolution)

    random_name = 'random_' + str(os.getpid())
    point_map = 'points_' + str(os.getpid())
    point_grid = 'points_' + str(os.getpid())
    TMP_RAST.append(random_name)
    TMP_RAST.append(point_map)
    TMP_VECT.append(point_grid)

    gcore.run_command('r.surf.random', output=random_name, min=0, max=1)
    grast.mapcalc(exp='{point_map} = if({rand} <= {prob}, 1, null())'.format(rand=random_name,
                                                                             prob=probab_01,
                                                                             point_map=point_map))
    gcore.run_command('r.to.vect', flags='t', input=point_map, output=point_grid, type='point')
    gcore.run_command('v.perturb', input=point_grid, output=output,
                      parameter=resolution / 2., seed=os.getpid())
Exemplo n.º 30
0
def main():
    # see https://grasswiki.osgeo.org/wiki/Convert_all_GRASS_6_vector_maps_to_GRASS_7
    grass.message('Building topology')
    if grass.run_command('v.build.all') != 0:
        grass.warning('Cannot build topology')

    grass.message('Creating new DB connection')
    if grass.run_command('db.connect', flags='d') != 0:
        grass.warning('Cannot create new DB connection')
        return

    grass.message('Transferring tables to the new DB')
    if grass.run_command('v.db.reconnect.all', flags='cd') != 0:
        grass.warning('Cannot transfer tables')
Exemplo n.º 31
0
def main():
    input = options['input']
    layer = options['layer']
    format = options['format']
    output = options['output']
    table = options['table']

    if format.lower() == 'dbf':
        format = "ESRI_Shapefile"

    if format.lower() == 'csv':
        olayer = basename(output, 'csv')
    else:
        olayer = None

    # is there a simpler way of testing for --overwrite?
    dbffile = input + '.dbf'
    if os.path.exists(dbffile) and not gcore.overwrite():
        gcore.fatal(_("File <%s> already exists") % dbffile)

    if olayer:
        try:
            gcore.run_command('v.out.ogr',
                              quiet=True,
                              input=input,
                              layer=layer,
                              output=output,
                              format=format,
                              type='point,line,area',
                              olayer=olayer)
        except CalledModuleError:
            gcore.fatal(_("Module <%s> failed") % 'v.out.ogr')

    else:
        try:
            gcore.run_command('v.out.ogr',
                              quiet=True,
                              input=input,
                              layer=layer,
                              output=output,
                              format=format,
                              type='point,line,area')
        except CalledModuleError:
            gcore.fatal(_("Module <%s> failed") % 'v.out.ogr')

    if format == "ESRI_Shapefile":
        exts = ['shp', 'shx', 'prj']
        if output.endswith('.dbf'):
            outname = basename(output, 'dbf')
            for ext in exts:
                try_remove("%s.%s" % (outname, ext))
            outname += '.dbf'
        else:
            for ext in exts:
                try_remove(os.path.join(output, "%s.%s" % (input, ext)))
            outname = os.path.join(output, input + ".dbf")
    elif format.lower() == 'csv':
        outname = output + '.csv'
    else:
        outname = input

    gcore.message(_("Exported table <%s>") % outname)
def slope(scanned_elev, new, env, zfactor=1.):
    gcore.run_command('r.slope.aspect', elevation=scanned_elev, zfactor=zfactor, slope=new, overwrite=True, env=env)
def shaded_relief(scanned_elev, new, zmult=10, env=None):
    gcore.run_command('r.shaded.relief', overwrite=True, input=scanned_elev, output=new, zmult=zmult, env=env)
def max_curv(scanned_elev, new, size=15, zscale=5, env=None):
    gcore.run_command('r.param.scale', overwrite=True, input=scanned_elev, output=new, size=size, param='maxic', zscale=zscale, env=env)
    gcore.run_command('r.colors', map=new, color='byr', env=env)
def geomorphon(scanned_elev, new, search=22, skip=12, flat=1, dist=0, env=None):
    gcore.run_command('r.geomorphon', overwrite=True, dem=scanned_elev, forms=new, search=search, skip=skip, flat=flat, dist=dist, env=env)
Exemplo n.º 36
0
def rename(oldname, newname, maptype, **kwargs):
    """Rename a map"""
    kwargs.update({maptype: '{old},{new}'.format(old=oldname, new=newname), })
    grasscore.run_command('g.rename', quiet=True, **kwargs)
Exemplo n.º 37
0
        streams.write(l, cat=10, attrs=(10,))

        streams.organization = 'Thuenen Institut'
        streams.person = 'Soeren Gebbert'
        streams.title = 'Test dataset for stream networks'
        streams.comment = 'This is a comment'

        streams.table.conn.commit()
        streams.close()

if __name__ == "__main__":

    import doctest
    from grass.pygrass import utils
    from grass.script.core import run_command

    utils.create_test_vector_map(test_vector_name)
    run_command("g.region", n=50, s=0, e=60, w=0, res=1)
    run_command("r.mapcalc", expression="%s = 1"%(test_raster_name),
                             overwrite=True)

    doctest.testmod()

    """Remove the generated vector map, if exist"""
    mset = utils.get_mapset_vector(test_vector_name, mapset='')
    if mset:
        run_command("g.remove", flags='f', type='vector', name=test_vector_name)
    mset = utils.get_mapset_raster(test_raster_name, mapset='')
    if mset:
        run_command("g.remove", flags='f', type='raster', name=test_raster_name)
Exemplo n.º 38
0
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 = 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])))
Exemplo n.º 39
0
    def _set_grass(self, wps_request):
        """Handle given grass_location parameter of the constructor

        location is either directory name, 'epsg:1234' form or a georeferenced
        file

        in the first case, new temporary mapset within the location will be
        created

        in the second case, location will be created in self.workdir

        the mapset should be deleted automatically using self.clean() method
        """

        if not PY2:
            LOGGER.warning('Seems PyWPS is running in Python-3 '
                           'environment, but GRASS GIS supports Python-2 only')
            return

        if self.grass_location:

            import random
            import string
            from grass.script import core as grass
            from grass.script import setup as gsetup

            # HOME needs to be set - and that is usually not the case for httpd
            # server
            os.environ['HOME'] = self.workdir

            # GISRC envvariable needs to be set
            gisrc = open(os.path.join(self.workdir, 'GISRC'), 'w')
            gisrc.write("GISDBASE: {}\n".format(self.workdir))
            gisrc.write("GUI: txt\n")
            gisrc.close()
            os.environ['GISRC'] = gisrc.name

            new_loc_args = dict()
            mapset_name = 'pywps_ms_{}'.format(''.join(
                random.sample(string.ascii_letters, 5)))

            if self.grass_location.startswith('complexinput:'):
                # create new location from a georeferenced file
                ref_file_parameter = self.grass_location.split(':')[1]
                ref_file = wps_request.inputs[ref_file_parameter][0].file
                new_loc_args.update({'filename': ref_file})
            elif self.grass_location.lower().startswith('epsg:'):
                # create new location from epsg code
                epsg = self.grass_location.lower().replace('epsg:', '')
                new_loc_args.update({'epsg': epsg})

            if new_loc_args:
                dbase = self.workdir
                location = str()
                while os.path.isdir(os.path.join(dbase, location)):
                    location = 'pywps_loc_{}'.format(''.join(
                        random.sample(string.ascii_letters, 5)))

                gsetup.init(self.workdir, dbase, location, 'PERMANENT')

                grass.create_location(dbase=dbase,
                                      location=location,
                                      **new_loc_args)
                LOGGER.debug('GRASS location based on {} created'.format(
                    list(new_loc_args.keys())[0]))
                grass.run_command('g.mapset',
                                  mapset=mapset_name,
                                  flags='c',
                                  dbase=dbase,
                                  location=location)

            # create temporary mapset within existing location
            elif os.path.isdir(self.grass_location):
                from grass.pygrass.gis import make_mapset

                LOGGER.debug('Temporary mapset will be created')

                dbase = os.path.dirname(self.grass_location)
                location = os.path.basename(self.grass_location)

                grass.run_command('g.gisenv', set="GISDBASE={}".format(dbase))
                grass.run_command('g.gisenv',
                                  set="LOCATION_NAME=%s" % location)

                while os.path.isdir(os.path.join(dbase, location,
                                                 mapset_name)):
                    mapset_name = 'pywps_ms_{}'.format(''.join(
                        random.sample(string.ascii_letters, 5)))
                make_mapset(mapset=mapset_name,
                            location=location,
                            gisdbase=dbase)
                grass.run_command('g.gisenv', set="MAPSET=%s" % mapset_name)

            else:
                raise NoApplicableCode(
                    'Location does exists or does not seem '
                    'to be in "EPSG:XXXX" form nor is it existing directory: {}'
                    .format(location))

            # set _grass_mapset attribute - will be deleted once handler ends
            self._grass_mapset = mapset_name

            # final initialization
            LOGGER.debug('GRASS Mapset set to {}'.format(mapset_name))

            LOGGER.debug('GRASS environment initialised')
            LOGGER.debug(
                'GISRC {}, GISBASE {}, GISDBASE {}, LOCATION {}, MAPSET {}'.
                format(os.environ.get('GISRC'), os.environ.get('GISBASE'),
                       dbase, location, os.path.basename(mapset_name)))
Exemplo n.º 40
0
def main(opts, flgs):
    ow = overwrite()

    output = opts["output_basename"]

    forest = opts["forest"]
    boundaries = opts["boundaries"]
    yield_ = opts["forest_column_yield"]
    management = opts["forest_column_management"]
    treatment = opts["forest_column_treatment"]
    yield_surface = opts["forest_column_yield_surface"]
    roughness = opts["forest_column_roughness"]
    forest_roads = opts["forest_roads"]

    rivers = opts["rivers"]
    lakes = opts["lakes"]

    vector_forest = opts["forest"]

    tech_bioenergyHF = output + "_tech_bioenergyHF"
    tech_bioenergyC = output + "_tech_bioenergyC"
    tech_bioenergy = output + "_tech_bioenergy"

    ######## start import and convert ########

    run_command("g.region", vect=boundaries)
    run_command(
        "v.to.rast",
        input=forest,
        output="yield",
        use="attr",
        attrcolumn=yield_,
        overwrite=True,
    )
    run_command(
        "v.to.rast",
        input=forest,
        output="yield_surface",
        use="attr",
        attrcolumn=yield_surface,
        overwrite=True,
    )
    run_command(
        "v.to.rast",
        input=forest,
        output="treatment",
        use="attr",
        attrcolumn=treatment,
        overwrite=True,
    )
    run_command(
        "v.to.rast",
        input=forest,
        output="management",
        use="attr",
        attrcolumn=management,
        overwrite=True,
    )

    run_command(
        "v.to.rast",
        input=forest_roads,
        output="forest_roads",
        use="val",
        overwrite=True,
    )

    run_command("r.null", map="yield", null=0)
    run_command("r.null", map="yield_surface", null=0)
    run_command("r.null", map="treatment", null=0)
    run_command("r.null", map="management", null=0)

    ######## end import and convert ########

    ######## temp patch to link map and fields ######

    management = "management"
    treatment = "treatment"
    yield_surface = "yield_surface"
    yield_ = "yield"
    forest_roads = "forest_roads"

    ######## end temp patch to link map and fields ######

    if roughness == "":
        run_command("r.mapcalc", overwrite=ow, expression="roughness=0")
        roughness = "roughness"
    else:
        run_command(
            "v.to.rast",
            input=forest,
            output="roughness",
            use="attr",
            attrcolumn=roughness,
            overwrite=True,
        )
        run_command("r.null", map="roughness", null=0)

    CCEXTR = ("cable_crane_extraction = if(" + yield_ + ">0 && slope>" +
              opts["slp_min_cc"] + " && slope<=" + opts["slp_max_cc"] +
              " && extr_dist<" + opts["dist_max_cc"] + ", 1)")

    FWEXTR = ("forwarder_extraction = if(" + yield_ + ">0 && slope<=" +
              opts["slp_max_fw"] + " && " + management + "==1 && (" +
              roughness + "==0 || " + roughness + "==1 || " + roughness +
              "==99999) && extr_dist<" + opts["dist_max_fw"] + ", 1)")

    OEXTR = ("other_extraction = if(" + yield_ + ">0 && slope<=" +
             opts["slp_max_cop"] + " && " + management + "==2 && (" +
             roughness + "==0 || " + roughness + "==1 || " + roughness +
             "==99999) && extr_dist<" + opts["dist_max_cop"] + ", 1)")

    EHF = (tech_bioenergyHF + " = technical_surface*(if(" + management +
           "==1 && " + treatment + "==1 || " + management + "==1 && " +
           treatment + "==99999, yield_pix*" + opts["energy_tops_hf"] +
           ", if(" + management + "==1 && " + treatment + "==2, yield_pix *" +
           opts["energy_tops_hf"] + " + yield_pix * " +
           opts["energy_cormometric_vol_hf"] + ")))")

    ECC = (tech_bioenergyC + " = technical_surface*(if(" + management +
           " == 2, yield_pix*" + opts["energy_tops_cop"] + "))")

    ET = tech_bioenergy + " = (" + tech_bioenergyC + " + " + tech_bioenergyHF + ")"

    run_command(
        "r.param.scale",
        overwrite=ow,
        input=opts["dtm"],
        output="morphometric_features",
        size=3,
        method="feature",
    )
    run_command("r.slope.aspect",
                overwrite=ow,
                elevation=opts["dtm"],
                slope="slope_deg")
    run_command(
        "r.mapcalc",
        overwrite=ow,
        expression="pix_cross = ((ewres()+nsres())/2)/ cos(slope_deg)",
    )
    run_command(
        "r.mapcalc",
        overwrite=ow,
        expression="yield_pix1 = (" + yield_ + "/" + yield_surface +
        ")*((ewres()*nsres())/10000)",
    )
    run_command("r.null", map="yield_pix1", null=0)
    run_command("r.null", map="morphometric_features", null=0)

    exprmap = "frict_surf_extr = pix_cross + if(yield_pix1<=0, 99999) + if(morphometric_features==6, 99999)"

    if rivers != "":
        run_command("v.to.rast",
                    input=rivers,
                    output="rivers",
                    use="val",
                    overwrite=True)
        run_command("r.null", map="rivers", null=0)
        rivers = "rivers"
        exprmap += "+ if(" + rivers + ">=1, 99999)"

    if lakes != "":
        run_command("v.to.rast",
                    input=lakes,
                    output="lakes",
                    use="val",
                    overwrite=True)
        run_command("r.null", map="lakes", null=0)
        lakes = "lakes"
        exprmap += "+ if(" + lakes + ">=1, 99999)"

    # morphometric_features==6 -> peaks
    # run_command("r.mapcalc", overwrite=ow,expression='frict_surf_extr = if(morphometric_features==6, 99999) + if(rivers>=1 || lakes>=1, 99999) + if(yield_pix1<=0, 99999) + pix_cross')
    run_command("r.mapcalc", overwrite=ow, expression=exprmap)

    run_command(
        "r.cost",
        overwrite=ow,
        input="frict_surf_extr",
        output="extr_dist",
        stop_points=vector_forest,
        start_rast=forest_roads,
        max_cost=1500,
    )

    run_command(
        "r.slope.aspect",
        overwrite=ow,
        elevation=opts["dtm"],
        slope="slope",
        format="percent",
    )
    run_command("r.mapcalc", overwrite=ow, expression=CCEXTR)
    run_command("r.mapcalc", overwrite=ow, expression=FWEXTR)
    run_command("r.mapcalc", overwrite=ow, expression=OEXTR)
    run_command("r.null", map="cable_crane_extraction", null=0)
    run_command("r.null", map="forwarder_extraction", null=0)
    run_command("r.null", map="other_extraction", null=0)
    run_command(
        "r.mapcalc",
        overwrite=ow,
        expression=
        "technical_surface = cable_crane_extraction + forwarder_extraction + other_extraction",
    )
    # run_command("r.statistics", overwrite=ow,
    #             base="compartment", cover="technical_surface", method="sum",
    #             output="techn_pix_comp")
    # run_command("r.mapcalc", overwrite=ow,
    #             expression='yield_pix2 = yield/(technical_surface*@techn_pix_comp)')
    # run_command("r.null", map="yield_pix2", null=0)
    # run_command("r.mapcalc", overwrite=ow,
    #             expression=YPIX % (1 if flgs['u'] else 0, 0 if flgs['u'] else 1,))

    run_command("r.mapcalc", overwrite=ow, expression="yield_pix=yield_pix1")

    run_command("r.mapcalc", overwrite=ow, expression=EHF)
    run_command("r.mapcalc", overwrite=ow, expression=ECC)
    run_command("r.mapcalc", overwrite=ow, expression=ET)

    with RasterRow(tech_bioenergy) as pT:
        T = np.array(pT)

    print("Resulted maps: " + output + "_tech_bioenergyHF, " + output +
          "_tech_bioenergyC, " + output + "_tech_bioenergy")
    print("Total bioenergy stimated (Mwh): %.2f" % np.nansum(T))

    if flgs["r"]:
        remove_map(opts, flgs)
Exemplo n.º 41
0
def createVect(view_nat):

    grass.run_command('v.in.ogr',
                      input="PG:",
                      layer=layer,
                      output=ogr,
                      overwrite=True,
                      flags='t',
                      key=key,
                      type=typ,
                      quiet=True)

    # if vector already exits, remove dblink (original table)
    if grass.find_file(view_nat, element='vector')['fullname']:
        grass.run_command('v.db.connect',
                          map=view_nat,
                          flags='d',
                          layer='1',
                          quiet=True)

        grass.run_command('v.db.connect',
                          map=view_nat,
                          flags='d',
                          layer='2',
                          quiet=True)

    grass.run_command('v.category',
                      input=ogr,
                      output=view_nat,
                      option="transfer",
                      overwrite=True,
                      layer="1,2",
                      quiet=True)

    grass.run_command('v.db.connect',
                      map=view_nat,
                      table=layer,
                      key=key,
                      layer='1',
                      quiet=True)

    grass.run_command('v.db.connect',
                      map=view_nat,
                      table=view,
                      key=key,
                      layer='2',
                      quiet=True)

    if options['color']:
        setColor(view_nat)
Exemplo n.º 42
0
def main():
    infile = options['input']
    output = options['output']
    method = options['method']
    dtype = options['type']
    fs = options['separator']
    x = options['x']
    y = options['y']
    z = options['z']
    value_column = options['value_column']
    vrange = options['vrange']
    vscale = options['vscale']
    percent = options['percent']
    pth = options['pth']
    trim = options['trim']
    workers = int(options['workers'])
    scan_only = flags['s']
    shell_style = flags['g']
    ignore_broken = flags['i']

    if workers is 1 and "WORKERS" in os.environ:
        workers = int(os.environ["WORKERS"])

    if not os.path.exists(infile):
        grass.fatal(_("Unable to read input file <%s>") % infile)

    addl_opts = {}
    if pth:
        addl_opts['pth'] = '%s' % pth
    if trim:
        addl_opts['trim'] = '%s' % trim
    if value_column:
        addl_opts['value_column'] = '%s' % value_column
    if vrange:
        addl_opts['vrange'] = '%s' % vrange
    if vscale:
        addl_opts['vscale'] = '%s' % vscale
    if ignore_broken:
        addl_opts['flags'] = 'i'

    if scan_only or shell_style:
        if shell_style:
            doShell = 'g'
        else:
            doShell = ''
        grass.run_command('r.in.xyz', flags='s' + doShell, input=infile,
                          output='dummy', sep=fs, x=x, y=y, z=z,
                          **addl_opts)
        sys.exit()

    if dtype == 'float':
        data_type = 'FCELL'
    else:
        data_type = 'DCELL'

    region = grass.region(region3d=True)

    if region['nsres'] != region['nsres3'] or region['ewres'] != region['ewres3']:
        grass.run_command('g.region', flags='3p')
        grass.fatal(_("The 2D and 3D region settings are different. Can not continue."))

    grass.verbose(_("Region bottom=%.15g  top=%.15g  vertical_cell_res=%.15g  (%d depths)")
                  % (region['b'], region['t'], region['tbres'], region['depths']))

    grass.verbose(_("Creating slices ..."))

    # to avoid a point which falls exactly on a top bound from being
    # considered twice, we shrink the
    # For the top slice we keep it though, as someone scanning the bounds
    # may have set the bounds exactly to the data extent (a bad idea, but
    # it happens..)
    eps = 1.0e-15

    # if there are thousands of depths hopefully this array doesn't get too
    # large and so we don't have to worry much about storing/looping through
    # all the finished process infos.
    proc = {}
    pout = {}

    depths = range(1, 1 + region['depths'])

    for i in depths:
        tmp_layer_name = 'tmp.r3xyz.%d.%s' % (os.getpid(), '%05d' % i)

        zrange_min = region['b'] + (region['tbres'] * (i - 1))

        if i < region['depths']:
            zrange_max = region['b'] + (region['tbres'] * i) - eps
        else:
            zrange_max = region['b'] + (region['tbres'] * i)

        # spawn depth layer import job in the background
        #grass.debug("slice %d, <%s>  %% %d" % (band, image[band], band % workers))
        grass.message(_("Processing horizontal slice %d of %d [%.15g,%.15g) ...")
                      % (i, region['depths'], zrange_min, zrange_max))

        proc[i] = grass.start_command('r.in.xyz', input=infile, output=tmp_layer_name,
                                      sep=fs, method=method, x=x, y=y, z=z,
                                      percent=percent, type=data_type,
                                      zrange='%.15g,%.15g' % (zrange_min, zrange_max),
                                      **addl_opts)

        grass.debug("i=%d, %%=%d  (workers=%d)" % (i, i % workers, workers))
        #print sys.getsizeof(proc)  # sizeof(proc array)  [not so big]

        if i % workers is 0:
            # wait for the ones launched so far to finish
            for p_i in depths[:i]:
                pout[p_i] = proc[p_i].communicate()[0]
                if proc[p_i].wait() is not 0:
                    grass.fatal(_("Trouble importing data. Aborting."))

    # wait for jSobs to finish, collect any stray output
    for i in depths:
        pout[i] = proc[i].communicate()[0]
        if proc[i].wait() is not 0:
            grass.fatal(_("Trouble importing data. Aborting."))

    del proc

    grass.verbose(_("Assembling 3D cube ..."))

    #input order: lower most strata first
    slices = grass.read_command('g.list', type='raster', sep=',',
                                pattern='tmp.r3xyz.%d.*' % os.getpid()).rstrip(os.linesep)
    grass.debug(slices)

    try:
        grass.run_command('r.to.rast3', input=slices, output=output)
    except CalledModuleError:
        grass.message(_("Done. 3D raster map <%s> created.") % output)
Exemplo n.º 43
0
 def tearDownClass(cls):
     """Remove copied vector"""
     run_command("g.remove", type="vector", name="myroads", flags="f")
Exemplo n.º 44
0
def remove_map(opts, flgs):

    run_command("g.remove",
                type="raster",
                flags="f",
                name="cable_crane_extraction")
    run_command("g.remove",
                type="raster",
                flags="f",
                name="forwarder_extraction")
    run_command("g.remove", type="raster", flags="f", name="other_extraction")
    run_command("g.remove", type="raster", flags="f", name="technical_surface")
    run_command("g.remove", type="raster", flags="f", name="frict_surf_extr")
    run_command("g.remove", type="raster", flags="f", name="extr_dist")
    run_command("g.remove", type="raster", flags="f", name="yield_pix")
    run_command("g.remove", type="raster", flags="f", name="yield_pix1")
    run_command("g.remove", type="raster", flags="f", name="yield_pix2")
    run_command("g.remove", type="raster", flags="f", name="pix_cross")
    run_command("g.remove", type="raster", flags="f", name="slope_deg")
    run_command("g.remove", type="raster", flags="f", name="slope")
def landform(scanned_elev, new, size=25, zscale=1, env=None):
    gcore.run_command('r.param.scale', overwrite=True, input=scanned_elev, output=new, size=size, param='feature', zscale=zscale, env=env)
Exemplo n.º 46
0
 def setUpClass(cls):
     gcore.use_temp_region()
     gcore.run_command("g.region", raster="elevation")
def erosion(scanned_elev, rain_value, depth, detachment, transport, shearstress, niter, flux, erdep, slope=None, aspect=None, env=None):
    pid = str(os.getpid())
    options = {}
    if slope:
        options['slope'] = slope
    if aspect:
        options['aspect'] = aspect
    dc, tc, tau = 'dc' + pid, 'tc' + pid, 'tau' + pid
    gcore.run_command('r.slope.aspect', elevation=scanned_elev, dx='dx_' + pid, dy='dy' + pid, overwrite=True, env=env, **options)
    gcore.run_command('r.sim.water', elevation=scanned_elev, dx='dx_' + pid, dy='dy' + pid, rain_value=rain_value, depth=depth, nwalk=10000, niter=niter, overwrite=True, env=env)
    gcore.run_command('r.mapcalc', expression="{dc} = {detachment}".format(dc=dc, detachment=detachment), overwrite=True, env=env)
    gcore.run_command('r.mapcalc', expression="{tc} = {transport}".format(tc=tc, transport=transport), overwrite=True, env=env)
    gcore.run_command('r.mapcalc', expression="{tau} = {shearstress}".format(tau=tau, shearstress=shearstress), overwrite=True, env=env)
    gcore.run_command('r.sim.sediment', elevation=scanned_elev, dx='dx_' + pid, dy='dy' + pid, wdepth=depth, det=dc, tran=tc, tau=tau, flux=flux, erdep=erdep, niter=niter, nwalk=10000, overwrite=True, env=env)
    gcore.run_command('g.remove', rast=[dc, tc, tau, 'dx_' + pid, 'dy' + pid], env=env)
Exemplo n.º 48
0
def main():
    # default option
    gisrc = read_gisrc(os.environ['GISRC'])
    user = os.environ['USER']
    # start optparse
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("-U",
                      "--user",
                      dest="user",
                      default=user,
                      help="PostgreSQL user [default=%default]")
    parser.add_option("-P",
                      "--password",
                      dest="passwd",
                      default=None,
                      help="PostgreSQL password for user [default=%default]")
    parser.add_option("-D",
                      "--database",
                      dest="db",
                      default='pygrassdb_doctest',
                      help="PostgreSQL database name [default=%default]")

    (opts, args) = parser.parse_args()
    #
    # Create DB
    #
    print("\n\nCreate a new DB: %s...\n" % opts.db)
    createdb = [
        'createdb', '--encoding=UTF-8',
        '--owner=%s' % opts.user, '--host=localhost',
        '--username=%s' % opts.user, opts.db
    ]
    if opts.passwd:
        print opts.passwd
        createdb.append("--password=%s" % opts.passwd)
    else:
        createdb.append("--no-password")
    subprocess.Popen(createdb)

    #
    # set postgreSQL
    #
    print("\n\nSet Postgres connection...\n")
    grasscore.run_command('db.connect',
                          driver='pg',
                          database='host=localhost,dbname=%s' % opts.db)

    grasscore.run_command('db.login', user=opts.user)
    print("\n\nCopy the map from PERMANENT to user1...\n")
    grasscore.run_command('g.copy',
                          vect="boundary_municp@PERMANENT,boundary_municp_pg",
                          overwrite=True)
    print("\n\nBuild topology...\n")
    grasscore.run_command('v.build', map='boundary_municp_pg', overwrite=True)

    #
    # set sqlite
    #
    db = [
        gisrc['GISDBASE'], gisrc['LOCATION_NAME'], gisrc['MAPSET'], 'sqlite.db'
    ]
    print("\n\nSet Sqlite connection...\n")
    grasscore.run_command('db.connect',
                          driver='sqlite',
                          database=os.path.join(db))
    print("\n\nCopy the map from PERMANENT to user1...\n")
    grasscore.run_command(
        'g.copy',
        vect="boundary_municp@PERMANENT,boundary_municp_sqlite",
        overwrite=True)
    print("\n\nBuild topology...\n")
    grasscore.run_command('v.build',
                          map='boundary_municp_sqlite',
                          overwrite=True)
def slope_aspect(scanned_elev, slope, aspect, env, zfactor=1.):
    gcore.run_command('r.slope.aspect', elevation=scanned_elev,  zfactor=zfactor, aspect=aspect, slope=slope, overwrite=True, env=env)
    gcore.run_command('r.colors', map=aspect, color='aspectcolr', env=env)
Exemplo n.º 50
0
def main():
    infile = options['input']

    # create temporary directory
    global tmp_dir
    tmp_dir = grass.tempdir()
    grass.debug('tmp_dir = %s' % tmp_dir)

    # check if the input file exists
    if not os.path.exists(infile):
        grass.fatal(_("File <%s> not found") % infile)

    # copy the files to tmp dir
    input_base = os.path.basename(infile)
    shutil.copyfile(infile, os.path.join(tmp_dir, input_base))
    os.chdir(tmp_dir)
    tar = tarfile.TarFile.open(name=input_base, mode='r')
    try:
        data_name = tar.getnames()[0]
    except:
        grass.fatal(_("Pack file unreadable"))

    if flags['p']:
        # print proj info and exit
        try:
            for fname in ['PROJ_INFO', 'PROJ_UNITS']:
                f = tar.extractfile(fname)
                sys.stdout.write(f.read().decode())
        except KeyError:
            grass.fatal(
                _("Pack file unreadable: file '{}' missing".format(fname)))
        tar.close()

        return 0

    # set the output name
    if options['output']:
        map_name = options['output']
    else:
        map_name = data_name

    # grass env
    gisenv = grass.gisenv()
    mset_dir = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
                            gisenv['MAPSET'])

    new_dir = os.path.join(mset_dir, 'vector', map_name)

    gfile = grass.find_file(name=map_name, element='vector', mapset='.')
    overwrite = os.getenv('GRASS_OVERWRITE')
    if gfile['file'] and overwrite != '1':
        grass.fatal(_("Vector map <%s> already exists") % map_name)
    elif overwrite == '1' and gfile['file']:
        grass.warning(
            _("Vector map <%s> already exists and will be overwritten") %
            map_name)
        grass.run_command('g.remove',
                          flags='f',
                          quiet=True,
                          type='vector',
                          name=map_name)
        shutil.rmtree(new_dir, True)

    # extract data
    tar.extractall()
    tar.close()
    if os.path.exists(os.path.join(data_name, 'coor')):
        pass
    elif os.path.exists(os.path.join(data_name, 'cell')):
        grass.fatal(
            _("This GRASS GIS pack file contains raster data. Use "
              "r.unpack to unpack <%s>" % map_name))
    else:
        grass.fatal(_("Pack file unreadable"))

    # check projection compatibility in a rather crappy way
    loc_proj = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_INFO')
    loc_proj_units = os.path.join(mset_dir, '..', 'PERMANENT', 'PROJ_UNITS')

    skip_projection_check = False
    if not os.path.exists(os.path.join(tmp_dir, 'PROJ_INFO')):
        if os.path.exists(loc_proj):
            grass.fatal(
                _("PROJ_INFO file is missing, unpack vector map in XY (unprojected) location."
                  ))
        skip_projection_check = True  # XY location

    if not skip_projection_check:
        diff_result_1 = diff_result_2 = None
        if not grass.compare_key_value_text_files(filename_a=os.path.join(
                tmp_dir, 'PROJ_INFO'),
                                                  filename_b=loc_proj,
                                                  proj=True):
            diff_result_1 = diff_files(os.path.join(tmp_dir, 'PROJ_INFO'),
                                       loc_proj)

        if not grass.compare_key_value_text_files(filename_a=os.path.join(
                tmp_dir, 'PROJ_UNITS'),
                                                  filename_b=loc_proj_units,
                                                  units=True):
            diff_result_2 = diff_files(os.path.join(tmp_dir, 'PROJ_UNITS'),
                                       loc_proj_units)

        if diff_result_1 or diff_result_2:
            if flags['o']:
                grass.warning(
                    _("Projection information does not match. Proceeding..."))
            else:
                if diff_result_1:
                    grass.warning(
                        _("Difference between PROJ_INFO file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_1)))
                if diff_result_2:
                    grass.warning(
                        _("Difference between PROJ_UNITS file of packed map "
                          "and of current location:\n{diff}").format(
                              diff=''.join(diff_result_2)))
                grass.fatal(
                    _("Projection of dataset does not appear to match current location."
                      " In case of no significant differences in the projection definitions,"
                      " use the -o flag to ignore them and use"
                      " current location definition."))

    # new db
    fromdb = os.path.join(tmp_dir, 'db.sqlite')
    # copy file
    shutil.copytree(data_name, new_dir)
    # exist fromdb
    if os.path.exists(fromdb):
        # the db connection in the output mapset
        dbconn = grassdb.db_connection(force=True)
        todb = dbconn['database']
        # return all tables
        list_fromtable = grass.read_command('db.tables',
                                            driver='sqlite',
                                            database=fromdb).splitlines()

        # return the list of old connection for extract layer number and key
        dbln = open(os.path.join(new_dir, 'dbln'), 'r')
        dbnlist = dbln.readlines()
        dbln.close()
        # check if dbf or sqlite directory exists
        if dbconn['driver'] == 'dbf' and not os.path.exists(
                os.path.join(mset_dir, 'dbf')):
            os.mkdir(os.path.join(mset_dir, 'dbf'))
        elif dbconn['driver'] == 'sqlite' and not os.path.exists(
                os.path.join(mset_dir, 'sqlite')):
            os.mkdir(os.path.join(mset_dir, 'sqlite'))
        # for each old connection
        for t in dbnlist:
            # it split the line of each connection, to found layer number and key
            if len(t.split('|')) != 1:
                values = t.split('|')
            else:
                values = t.split(' ')

            from_table = values[1]
            layer = values[0].split('/')[0]
            # we need to take care about the table name in case of several layer
            if options["output"]:
                if len(dbnlist) > 1:
                    to_table = "%s_%s" % (map_name, layer)
                else:
                    to_table = map_name
            else:
                to_table = from_table

            grass.verbose(
                _("Coping table <%s> as table <%s>") % (from_table, to_table))

            # copy the table in the default database
            try:
                grass.run_command('db.copy',
                                  to_driver=dbconn['driver'],
                                  to_database=todb,
                                  to_table=to_table,
                                  from_driver='sqlite',
                                  from_database=fromdb,
                                  from_table=from_table)
            except CalledModuleError:
                grass.fatal(
                    _("Unable to copy table <%s> as table <%s>") %
                    (from_table, to_table))

            grass.verbose(
                _("Connect table <%s> to vector map <%s> at layer <%s>") %
                (to_table, map_name, layer))

            # and connect the new tables with the right layer
            try:
                grass.run_command('v.db.connect',
                                  flags='o',
                                  quiet=True,
                                  driver=dbconn['driver'],
                                  database=todb,
                                  map=map_name,
                                  key=values[2],
                                  layer=layer,
                                  table=to_table)
            except CalledModuleError:
                grass.fatal(
                    _("Unable to connect table <%s> to vector map <%s>") %
                    (to_table, map_name))

    grass.message(_("Vector map <%s> successfully unpacked") % map_name)
def aspect(scanned_elev, new, env):
    gcore.run_command('r.slope.aspect', elevation=scanned_elev, aspect=new, overwrite=True, env=env)
Exemplo n.º 52
0
                c_ok = libvect.Vect_get_area_cats(self.c_mapinfo, a_id,
                                                  ctypes.byref(line_c))
                if c_ok == 0:  # Centroid found

                    ok = libvect.Vect_cat_get(ctypes.byref(line_c), field,
                                              ctypes.byref(cat))
                    if ok > 0:
                        pcat = cat.value

                l.append((a_id, pcat, ctypes.string_at(barray, size.value)))
                libgis.G_free(barray)

            return l
        return None


if __name__ == "__main__":
    import doctest
    from grass.pygrass import utils
    utils.create_test_vector_map(test_vector_name)
    doctest.testmod()
    """Remove the generated vector map, if exist"""
    from grass.pygrass.utils import get_mapset_vector
    from grass.script.core import run_command
    mset = get_mapset_vector(test_vector_name, mapset='')
    if mset:
        run_command("g.remove",
                    flags='f',
                    type='vector',
                    name=test_vector_name)
def flowacc(scanned_elev, new, env):
    gcore.run_command('r.flow', elevation=scanned_elev, flowaccumulation=new, overwrite=True, env=env)
Exemplo n.º 54
0
        else:
            newname = self.name
        try:
            cur.execute(sql.CREATE_TAB.format(tname=newname, coldef=coldef))
            self.conn.commit()
        except OperationalError:  # OperationalError
            if overwrite:
                self.drop(force=True)
                cur.execute(sql.CREATE_TAB.format(tname=newname, coldef=coldef))
                self.conn.commit()
            else:
                print("The table: %s already exist." % self.name)
        cur.close()
        self.columns.update_odict()


if __name__ == "__main__":
    import doctest
    from grass.pygrass import utils

    utils.create_test_vector_map(test_vector_name)
    doctest.testmod()

    """Remove the generated vector map, if exist"""
    from grass.pygrass.utils import get_mapset_vector
    from grass.script.core import run_command

    mset = get_mapset_vector(test_vector_name, mapset="")
    if mset:
        run_command("g.remove", flags="f", type="vector", name=test_vector_name)
def change_detection(before, after, change, height_threshold, cells_threshold, add, max_detected, env):
    tmp_regions = []
    env = get_environment(tmp_regions, rast=before, n='n-20', s='s+20', e='e-20', w='w+20')
    diff_thr = 'diff_thr_' + str(os.getpid())
    diff_thr_clump = 'diff_thr_clump_' + str(os.getpid())
    change_vector = 'change_vector_' + str(os.getpid())
    if add:
        gcore.run_command('r.mapcalc', expression="{diff_thr} = if(({after} - {before}) > {thr1} &&"
                                                  " ({after} - {before}) < {thr2}, 1, null())".format(diff_thr=diff_thr,  after=after,
                                                                                                      before=before, thr1=height_threshold[0],
                                                                                                      thr2=height_threshold[1]), env=env)
    else:
        gcore.run_command('r.mapcalc', expression="{diff_thr} = if(({before} - {after}) > {thr}, 1, null())".format(diff_thr=diff_thr,
                          after=after, before=before, thr=height_threshold), env=env)

    gcore.run_command('r.clump', input=diff_thr, output=diff_thr_clump, env=env)

    stats = gcore.read_command('r.stats', flags='cn', input=diff_thr_clump, sort='desc', env=env).strip().split(os.linesep)
    if len(stats) > 0 and stats[0]:
        print stats
        cats = []
        found = 0
        for stat in stats:
            if found >= max_detected:
                break
            if float(stat.split()[1]) < cells_threshold[1] and float(stat.split()[1]) > cells_threshold[0]: # larger than specified number of cells
                found += 1
                cat, value = stat.split()
                cats.append(cat)
        if cats:
            expression = '{change} = if(('.format(change=change)
            for i, cat in enumerate(cats):
                if i != 0:
                    expression += ' || '
                expression += '{diff_thr_clump} == {val}'.format(diff_thr_clump=diff_thr_clump, val=cat)
            expression += '), 1, null())'
            gcore.run_command('r.mapcalc', overwrite=True, env=env, expression=expression)
            remove_vector(change_vector)
            gcore.run_command('r.to.vect', flags='st', input=change, output=change_vector, type='area', env=env)
            remove_vector(change)
            gcore.run_command('v.to.points', flags='t', input=change_vector, type='centroid', output=change, env=env)
            remove_vector(change_vector)
        else:
            gcore.warning("No change found!")
    else:
        gcore.warning("No change found!")
    
    gcore.run_command('g.remove', rast=[diff_thr, diff_thr_clump])
    remove_temp_regions(tmp_regions)
Exemplo n.º 56
0
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)
Exemplo n.º 57
0
def main():
    infile = options["input"]
    compression_off = flags["c"]

    global basedir
    basedir = grass.tempdir()

    # check if vector map exists
    gfile = grass.find_file(infile, element="vector")
    if not gfile["name"]:
        grass.fatal(_("Vector map <%s> not found") % infile)

    # check if input vector map is in the native format
    if vector.vector_info(gfile["fullname"])["format"] != "native":
        grass.fatal(
            _("Unable to pack vector map <%s>. Only native format supported.")
            % gfile["fullname"])

    # split the name if there is the mapset name
    if infile.find("@"):
        infile = infile.split("@")[0]

    # output name
    if options["output"]:
        outfile = options["output"]
    else:
        outfile = infile + ".pack"

    # check if exists the output file
    if os.path.exists(outfile):
        if os.getenv("GRASS_OVERWRITE"):
            grass.warning(
                _("Pack file <%s> already exists and will be overwritten") %
                outfile)
            try_remove(outfile)
        else:
            grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))

    # prepare for packing
    grass.verbose(_("Packing <%s>...") % (gfile["fullname"]))

    # write tar file, optional compression
    if compression_off:
        tar = tarfile.open(name=outfile, mode="w:")
    else:
        tar = tarfile.open(name=outfile, mode="w:gz")
    tar.add(gfile["file"], infile)

    # check if exist a db connection for the vector
    db_vect = vector.vector_db(gfile["fullname"])
    if not db_vect:
        grass.verbose(
            _("There is not database connected with vector map <%s>") %
            gfile["fullname"])
    else:
        # for each layer connection save a table in sqlite database
        sqlitedb = os.path.join(basedir, "db.sqlite")
        for i, dbconn in db_vect.items():
            grass.run_command(
                "db.copy",
                from_driver=dbconn["driver"],
                from_database=dbconn["database"],
                from_table=dbconn["table"],
                to_driver="sqlite",
                to_database=sqlitedb,
                to_table=dbconn["table"],
            )
        tar.add(sqlitedb, "db.sqlite")

    # add to the tar file the PROJ files to check when unpack file
    gisenv = grass.gisenv()
    for support in ["INFO", "UNITS", "EPSG"]:
        path = os.path.join(gisenv["GISDBASE"], gisenv["LOCATION_NAME"],
                            "PERMANENT", "PROJ_" + support)
        if os.path.exists(path):
            tar.add(path, "PROJ_" + support)
    tar.close()

    grass.message(
        _("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
def usped(scanned_elev, k_factor, c_factor, flowacc, slope, aspect, new, env):
    """!Computes net erosion and deposition (USPED model)"""
    sedflow = 'sedflow_' + str(os.getpid())
    qsx = 'qsx_' + str(os.getpid())
    qsxdx = 'qsxdx_' + str(os.getpid())
    qsy = 'qsy_' + str(os.getpid())
    qsydy = 'qsydy_' + str(os.getpid())
    slope_sm = 'slope_sm' + str(os.getpid())
    gcore.run_command('r.neighbors', overwrite=True, input=slope, output=slope_sm, size=5, env=env)
    gcore.run_command('r.mapcalc', expression="{sedflow} = 270. * {k_factor} * {c_factor} * {flowacc} * sin({slope})".format(c_factor=c_factor, k_factor=k_factor, slope=slope_sm, flowacc=flowacc, sedflow=sedflow), overwrite=True, env=env)
    gcore.run_command('r.mapcalc', expression="{qsx} = {sedflow} * cos({aspect})".format(sedflow=sedflow, aspect=aspect, qsx=qsx), overwrite=True, env=env)
    gcore.run_command('r.mapcalc', expression="{qsy} = {sedflow} * sin({aspect})".format(sedflow=sedflow, aspect=aspect, qsy=qsy), overwrite=True, env=env)
    gcore.run_command('r.slope.aspect', elevation=qsx, dx=qsxdx, overwrite=True, env=env)
    gcore.run_command('r.slope.aspect', elevation=qsy, dy=qsydy, overwrite=True, env=env)
    gcore.run_command('r.mapcalc', expression="{erdep} = {qsxdx} + {qsydy}".format(erdep=new, qsxdx=qsxdx, qsydy=qsydy), overwrite=True, env=env)
    gcore.write_command('r.colors', map=new,  rules='-', stdin='-15000 100 0 100\n-100 magenta\n-10 red\n-1 orange\n-0.1 yellow\n0 200 255 200\n0.1 cyan\n1 aqua\n10 blue\n100 0 0 100\n18000 black', env=env)

    gcore.run_command('g.remove', rast=[sedflow, qsx, qsxdx, qsy, qsydy, slope_sm])
Exemplo n.º 59
0
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..."))
        if grass.run_command('v.in.ascii',
                             flags=do3D,
                             input=digfile,
                             output=name,
                             format='standard') != 0:
            grass.fatal(
                _('An error occurred on creating "%s", please check') % name)
Exemplo n.º 60
0
 def setUpClass(cls):
     """Copy vector."""
     run_command("g.copy", vector="roadsmajor,myroads")