[GRASS-SVN] r73622 - grass-addons/grass7/vector/v.stream.inbasin
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Oct 29 19:46:08 PDT 2018
Author: awickert
Date: 2018-10-29 19:46:08 -0700 (Mon, 29 Oct 2018)
New Revision: 73622
Modified:
grass-addons/grass7/vector/v.stream.inbasin/v.stream.inbasin.py
Log:
v.stream.inbasin: specific choice of pour point
Modified: grass-addons/grass7/vector/v.stream.inbasin/v.stream.inbasin.py
===================================================================
--- grass-addons/grass7/vector/v.stream.inbasin/v.stream.inbasin.py 2018-10-29 13:17:16 UTC (rev 73621)
+++ grass-addons/grass7/vector/v.stream.inbasin/v.stream.inbasin.py 2018-10-30 02:46:08 UTC (rev 73622)
@@ -43,6 +43,12 @@
#% required: no
#%end
+#%option G_OPT_R_INPUT
+#% key: draindir
+#% label: Drainage directions (needed if exact coordinates used)
+#% required: no
+#%end
+
#%option
#% key: cat
#% label: Farthest downstream segment category
@@ -82,6 +88,12 @@
#% required: no
#%end
+#%flag
+#% key: s
+#% description: Snap provided coordinates to nearest segment endpoint
+#% guisection: Settings
+#%end
+
##################
# IMPORT MODULES #
##################
@@ -128,6 +140,8 @@
output_basins = options['output_basin']
output_streams = options['output_streams']
output_pour_point = options['output_pour_point']
+ draindir = options['draindir']
+ snapflag = flags['s']
#print options
#print flags
@@ -138,73 +152,89 @@
else:
gscript.fatal('You must set either "cat" or "x_outlet" and "y_outlet".')
+
# NEED TO ADD IF-STATEMENT HERE TO AVOID AUTOMATIC OVERWRITING!!!!!!!!!!!
- if downstream_cat == '':
- # Need to find outlet pour point -- start by creating a point at this
- # location to use with v.distance
- try:
- v.db_droptable(table='tmp', flags='f')
- except:
- pass
- tmp = vector.Vector('tmp')
- _cols = [(u'cat', 'INTEGER PRIMARY KEY'),
- (u'x', 'DOUBLE PRECISION'),
- (u'y', 'DOUBLE PRECISION'),
- (u'strcat', 'DOUBLE PRECISION')]
- tmp.open('w', tab_name='tmp', tab_cols=_cols)
- point0 = Point(x_outlet,y_outlet)
- tmp.write(point0, cat=1, attrs=(str(x_outlet), str(y_outlet), 0), )
- tmp.table.conn.commit()
- tmp.build()
- tmp.close()
- # Now v.distance
- gscript.run_command('v.distance', from_='tmp', to=streams, upload='cat', column='strcat')
- #v.distance(_from_='tmp', to=streams, upload='cat', column='strcat')
- downstream_cat = gscript.vector_db_select(map='tmp', columns='strcat')
- downstream_cat = int(downstream_cat['values'].values()[0][0])
+ if snapflag or (downstream_cat != ''):
+ if downstream_cat == '':
+ # Need to find outlet pour point -- start by creating a point at this
+ # location to use with v.distance
+ try:
+ v.db_droptable(table='tmp', flags='f')
+ except:
+ pass
+ tmp = vector.Vector('tmp')
+ _cols = [(u'cat', 'INTEGER PRIMARY KEY'),
+ (u'x', 'DOUBLE PRECISION'),
+ (u'y', 'DOUBLE PRECISION'),
+ (u'strcat', 'DOUBLE PRECISION')]
+ tmp.open('w', tab_name='tmp', tab_cols=_cols)
+ point0 = Point(x_outlet,y_outlet)
+ tmp.write(point0, cat=1, attrs=(str(x_outlet), str(y_outlet), 0), )
+ tmp.table.conn.commit()
+ tmp.build()
+ tmp.close()
+ # Now v.distance
+ gscript.run_command('v.distance', from_='tmp', to=streams, upload='cat', column='strcat')
+ #v.distance(_from_='tmp', to=streams, upload='cat', column='strcat')
+ downstream_cat = gscript.vector_db_select(map='tmp', columns='strcat')
+ downstream_cat = int(downstream_cat['values'].values()[0][0])
- # Attributes of streams
- colNames = np.array(vector_db_select(streams)['columns'])
- colValues = np.array(vector_db_select(streams)['values'].values())
- tostream = colValues[:,colNames == 'tostream'].astype(int).squeeze()
- cats = colValues[:,colNames == 'cat'].astype(int).squeeze() # = "fromstream"
+ # Attributes of streams
+ colNames = np.array(vector_db_select(streams)['columns'])
+ colValues = np.array(vector_db_select(streams)['values'].values())
+ tostream = colValues[:,colNames == 'tostream'].astype(int).squeeze()
+ cats = colValues[:,colNames == 'cat'].astype(int).squeeze() # = "fromstream"
- # Find network
- basincats = [downstream_cat] # start here
- most_upstream_cats = [downstream_cat] # all of those for which new cats must be sought
- while True:
- if len(most_upstream_cats) == 0:
- break
- tmp = list(most_upstream_cats) # copy to a temp file: old values
- most_upstream_cats = [] # Ready to accept new values
- for ucat in tmp:
- most_upstream_cats += list(cats[tostream == int(ucat)])
- basincats += most_upstream_cats
-
- basincats = list(set(list(basincats)))
+ # Find network
+ basincats = [downstream_cat] # start here
+ most_upstream_cats = [downstream_cat] # all of those for which new cats must be sought
+ while True:
+ if len(most_upstream_cats) == 0:
+ break
+ tmp = list(most_upstream_cats) # copy to a temp file: old values
+ most_upstream_cats = [] # Ready to accept new values
+ for ucat in tmp:
+ most_upstream_cats += list(cats[tostream == int(ucat)])
+ basincats += most_upstream_cats
+
+ basincats = list(set(list(basincats)))
- basincats_str = ','.join(map(str, basincats))
-
- # Many basins out -- need to use overwrite flag in future!
- #SQL_OR = 'rnum = ' + ' OR rnum = '.join(map(str, basincats))
- #SQL_OR = 'cat = ' + ' OR cat = '.join(map(str, basincats))
- SQL_LIST = 'cat IN (' + ', '.join(map(str, basincats)) + ')'
- if len(basins) > 0:
- v.extract(input=basins, output=output_basins, where=SQL_LIST, overwrite=gscript.overwrite(), quiet=True)
- if len(streams) > 0:
- v.extract(input=streams, output=output_streams, cats=basincats_str, overwrite=gscript.overwrite(), quiet=True)
+ basincats_str = ','.join(map(str, basincats))
+
+ # Many basins out -- need to use overwrite flag in future!
+ #SQL_OR = 'rnum = ' + ' OR rnum = '.join(map(str, basincats))
+ #SQL_OR = 'cat = ' + ' OR cat = '.join(map(str, basincats))
+ SQL_LIST = 'cat IN (' + ', '.join(map(str, basincats)) + ')'
+ if len(basins) > 0:
+ v.extract(input=basins, output=output_basins, where=SQL_LIST, overwrite=gscript.overwrite(), quiet=True)
+ if len(streams) > 0:
+ v.extract(input=streams, output=output_streams, cats=basincats_str, overwrite=gscript.overwrite(), quiet=True)
+ else:
+ # Have coordinates and will limit the area that way.
+ r.water_outlet(input=draindir, output='tmp', coordinates=(x_outlet, y_outlet), overwrite=True)
+ r.to_vect(input='tmp', output='tmp', type='area', overwrite=True)
+ v.clip(input=basins, clip='tmp', output=output_basins, overwrite=True)
+ basincats = gscript.vector_db_select('basins_inbasin').values()[0].keys()
+ basincats_str = ','.join(map(str, basincats))
+ if len(streams) > 0:
+ v.extract(input=streams, output=output_streams, cats=basincats_str, overwrite=gscript.overwrite(), quiet=True)
+
# If we want to output the pour point location
if len(output_pour_point) > 0:
- _pp = gscript.vector_db_select(map=streams, columns='x2,y2', where='cat='+str(downstream_cat))
- _xy = np.squeeze(_pp['values'].values())
- _x = float(_xy[0])
- _y = float(_xy[1])
# NEED TO ADD IF-STATEMENT HERE TO AVOID AUTOMATIC OVERWRITING!!!!!!!!!!!
try:
v.db_droptable(table=output_pour_point, flags='f')
except:
pass
+ if snapflag or (downstream_cat != ''):
+ _pp = gscript.vector_db_select(map=streams, columns='x2,y2', where='cat='+str(downstream_cat))
+ _xy = np.squeeze(_pp['values'].values())
+ _x = float(_xy[0])
+ _y = float(_xy[1])
+ else:
+ _x = x_outlet
+ _y = y_outlet
pptmp = vector.Vector(output_pour_point)
_cols = [(u'cat', 'INTEGER PRIMARY KEY'),
(u'x', 'DOUBLE PRECISION'),
@@ -216,6 +246,5 @@
pptmp.build()
pptmp.close()
-
if __name__ == "__main__":
main()
More information about the grass-commit
mailing list