#NEWLINE# @ cli . command ( 'set-workspace' ) #NEWLINE# @ click . argument ( 'workspace' , default = '@-' , required = False ) #NEWLINE# @ click . option ( '--id' , help = 'If provided, update the workspace with this id' ) #NEWLINE# @ click . option ( '--aoi' , help = 'The geometry to use' ) #NEWLINE# @ click . option ( '--name' , help = 'Workspace name' ) #NEWLINE# @ click . option ( '--create' , is_flag = True , help = 'Specify workspace creation' ) #NEWLINE# @ click . option ( '--where' , nargs = 3 , multiple = True , help = 'Provide additional search criteria. See https://www.planet.com/docs/v0/scenes/#metadata for search metadata fields.' ) #NEWLINE# def set_workspace( id , aoi , name , create , workspace , where ) : #NEWLINE# #INDENT# 'Create or modify a workspace' #NEWLINE# workspace = read ( workspace ) #NEWLINE# try : #NEWLINE# #INDENT# workspace = ( json . loads ( workspace ) if workspace else None ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# raise click . ClickException ( 'workspace must be JSON' ) #NEWLINE# #UNINDENT# cl = client ( ) #NEWLINE# if ( ( workspace is None ) and id ) : #NEWLINE# #INDENT# workspace = cl . get_workspace ( id ) . get ( ) #NEWLINE# #UNINDENT# if ( not id ) : #NEWLINE# #INDENT# id = workspace . get ( 'id' , None ) #NEWLINE# #UNINDENT# if create : #NEWLINE# #INDENT# id = None #NEWLINE# #UNINDENT# aoi = read_aoi ( aoi ) #NEWLINE# if aoi : #NEWLINE# #INDENT# geom = api . utils . geometry_from_json ( aoi ) #NEWLINE# if ( geom is None ) : #NEWLINE# #INDENT# raise click . ClickException ( 'unable to find geometry in aoi' ) #NEWLINE# #UNINDENT# workspace [ 'filters' ] = { #NEWLINE# #INDENT# 'geometry' : { #NEWLINE# #INDENT# 'intersects' : geom , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if name : #NEWLINE# #INDENT# workspace [ 'name' ] = name #NEWLINE# #UNINDENT# if where : #NEWLINE# #INDENT# if ( 'filters' not in workspace ) : #NEWLINE# #INDENT# workspace [ 'filters' ] = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# filters = workspace [ 'filters' ] #NEWLINE# for ( k , c , v ) in where : #NEWLINE# #INDENT# if ( k not in filters ) : #NEWLINE# #INDENT# filters [ k ] = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# group = filters . get ( k ) #NEWLINE# if ( ( v == '-' ) and ( c in geom ) ) : #NEWLINE# #INDENT# group . pop ( c ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# group [ c ] = v #NEWLINE# #UNINDENT# if ( not group ) : #NEWLINE# #INDENT# filters . pop ( k ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( not workspace ) : #NEWLINE# #INDENT# raise click . ClickException ( 'nothing to do' ) #NEWLINE# #UNINDENT# echo_json_response ( call_and_wrap ( cl . set_workspace , workspace , id ) , pretty )