def register(): #""" register the Calculation module (CM)to the main web services (MWS)- #the CM will send its SIGNATURE to the main web service, CM SIGNATURE contains elements to identity the CM and #how to handle it and the list of input it needs on the user interface. CM SIGNATURE can be find on app/constants.py file, this file must be change manually #Also constants.py must contains a CM_ID that is a unique number that as to be defined by the CREM (Centre de Recherches Energetiques et Municipales de Martigny) # --- # definitions: # Color: # type: string # #responses: # 200: # description: MWS is aware of the CM # examples: # results: {"response": {"category": "Buildings", "cm_name": "calculation_module_1", "layers_needed": ["heat_density_tot"], "cm_description": "this computation module allows to ....", "cm_url": "http://127.0.0.1:5002/", "cm_id": 1, "inputs_calculation_module": [{"input_min": 1, "input_value": 1, "input_unit": "none", "input_name": "Reduction factor", "cm_id": 1, "input_type": "input", "input_parameter_name": "multiplication_factor", "input_max": 10}, {"input_min": 10, "input_value": 50, "input_unit": "", "input_name": "Blablabla", "cm_id": 1, "input_type": "range", "input_parameter_name": "bla", "input_max": 1000}]}} # """ # about to send the external IP print('CM will begin register ') ip = socket.gethostbyname(socket.gethostname()) # retrive dynamic url base_url = 'http://' + str(ip) + ':' + str(constant.PORT) + '/' signature_final = SIGNATURE calculation_module_rpc = CalculationModuleRpcClient() signature_final["cm_url"] = base_url payload = json.dumps(signature_final) response = calculation_module_rpc.call(payload) return response
def computeTask(data,payload,cm_id): """ :param data: :param payload: :param cm_id: :return:Rdeturns the calculation of a calculation module """ inputs_vector_selection = None #****************** RETRIVE INPUT DATA ***************************************************' #transforme stringify array to json layer_needed = payload['layers_needed'] type_layer_needed = payload['type_layer_needed'] vectors_needed = payload['vectors_needed'] #retriving scale level 3 possiblity hectare,nuts, lau scalevalue = data['scalevalue'] if scalevalue == 'hectare': #****************** BEGIN RASTER CLIP FOR HECTAR *************************************************** areas = payload['areas'] geom = helper.area_to_geom(areas) inputs_raster_selection = model.get_raster_from_csv(DATASET_DIRECTORY ,geom,layer_needed, type_layer_needed, UPLOAD_DIRECTORY) inputs_vector_selection = model.retrieve_vector_data_for_calculation_module(vectors_needed, scalevalue, geom) #print ('inputs_raster_selection',inputs_raster_selection) #****************** FINISH RASTER CLIP FOR HECTAR ***************************************************' else: #****************** BEGIN RASTER CLIP FOR NUTS OR LAU ***************************************************' id_list = payload['nuts'] shapefile_path = model.get_shapefile_from_selection(scalevalue,id_list,UPLOAD_DIRECTORY) inputs_raster_selection = model.clip_raster_from_shapefile(DATASET_DIRECTORY ,shapefile_path,layer_needed, type_layer_needed, UPLOAD_DIRECTORY) if vectors_needed != None: inputs_vector_selection = model.retrieve_vector_data_for_calculation_module(vectors_needed, scalevalue, id_list) #****************** FINISH RASTER CLIP FOR NUTS OR LAU *************************************************** data = generate_payload_for_compute(data,inputs_raster_selection,inputs_vector_selection) # send the result to the right CM #****************** WILL SEND PAYLOAD TO CM WITH ID {} ***************************************************'.format(cm_id)) calculation_module_rpc = CalculationModuleRpcClient() response = calculation_module_rpc.call(cm_id,data.encode('utf-8')) #'****************** RETRIVED RESULT FROM CM WITH ID {} ***************************************************'.format(cm_id)) data_output = json.loads(response) helper.test_display(data_output) #****************** WILL GENERATE TILES ***************************************************'.format(cm_id)) try: print ('time to generate tilexs generateTiles') if data_output['result']['raster_layers'] is not None and len(data_output['result']['raster_layers'])>0: raster_layers = data_output['result']['raster_layers'] generateTiles(raster_layers) except: # no raster_layers pass try: if data_output['result']['vector_layers'] is not None and len(data_output['result']['vector_layers'])>0: vector_layers = data_output['result']['vector_layers'] except: # no vector_layers pass print ('data_output',json.dumps(data_output)) return data_output
def computeTask(data, payload, cm_id): """ :param data: :param payload: :param cm_id: :return:Rdeturns the calculation of a calculation module """ signal.signal(signal.SIGALRM, timeout_signal_handler) signal.alarm(DEFAULT_TIMEOUT) try: #****************** RETRIVE INPUT DATA ***************************************************' #transforme stringify array to json layer_needed = [ l for l in payload['layers_needed'] if l['data_type'] == 'raster' ] type_layer_needed = payload['type_layer_needed'] vectors_needed = [ l for l in payload['layers_needed'] if l['data_type'] == 'vector' ] #retriving scale level 3 possiblity hectare,nuts, lau scalevalue = data['scalevalue'] nuts_within = None inputs_vector_selection = None areas = payload['areas'] if scalevalue == 'hectare': #****************** BEGIN RASTER CLIP FOR HECTAR *************************************************** geom = helper.area_to_geom(areas) nuts_within = model.nuts_within_the_selection(geom) inputs_raster_selection = model.get_raster_from_csv( geom, layer_needed, UPLOAD_DIRECTORY) inputs_vector_selection = model.retrieve_vector_data_for_calculation_module( vectors_needed, scalevalue, areas) #nut2_nuts3_area = #print ('inputs_raster_selection',inputs_raster_selection) #****************** FINISH RASTER CLIP FOR HECTAR ***************************************************' else: #****************** BEGIN RASTER CLIP FOR NUTS OR LAU ***************************************************' scale = scalevalue[:-1] # id_list = payload['areas'] nuts_within = model.nuts2_within_the_selection_nuts_lau( scale, areas) shapefile_path = model.get_shapefile_from_selection( scale, areas, UPLOAD_DIRECTORY) inputs_raster_selection = model.clip_raster_from_shapefile( shapefile_path, layer_needed, UPLOAD_DIRECTORY) if vectors_needed != None: inputs_vector_selection = model.retrieve_vector_data_for_calculation_module( vectors_needed, scalevalue, areas) #****************** FINISH RASTER CLIP FOR NUTS OR LAU *************************************************** print(inputs_raster_selection, inputs_vector_selection) data = generate_payload_for_compute(data, inputs_raster_selection, inputs_vector_selection, nuts_within) # send the result to the right CM #****************** WILL SEND PAYLOAD TO CM WITH ID {} ***************************************************'.format(cm_id)) calculation_module_rpc = CalculationModuleRpcClient() response = calculation_module_rpc.call(cm_id, data.encode('utf-8')) response = response.decode("utf-8") data_output = json.loads(response) #'****************** RETRIVED RESULT FROM CM WITH ID {} ***************************************************'.format(cm_id)) helper.test_display(data_output) #****************** WILL GENERATE TILES ***************************************************'.format(cm_id)) try: for indicator in data_output['result']['indicator']: indicator['value'] = str(indicator['value']) except: pass try: if data_output['result']['raster_layers'] is not None and len( data_output['result']['raster_layers']) > 0: raster_layers = data_output['result']['raster_layers'] generateTiles(raster_layers) except: # no raster_layers pass try: if data_output['result']['vector_layers'] is not None and len( data_output['result']['vector_layers']) > 0: vector_layers = data_output['result']['vector_layers'] except: # no vector_layers pass return data_output except TimeoutError: return None