def run_command(self): """ Run the command. Read the layer file from a GeoJSON file, create a GeoLayer object, and add to the GeoProcessor's geolayer list. Returns: None. Raises: RuntimeError if any warnings occurred during run_command method. """ logger = logging.getLogger(__name__) # Obtain the parameter values. pv_SpatialDataFile = self.get_parameter_value("SpatialDataFile") pv_GeoLayerID = self.get_parameter_value("GeoLayerID", default_value='%f') # Expand for ${Property} syntax. pv_GeoLayerID = self.command_processor.expand_parameter_value(pv_GeoLayerID, self) # Convert the SpatialDataFile parameter value relative path to an absolute path and expand for ${Property} # syntax spatial_data_file_absolute = io_util.verify_path_for_os( io_util.to_absolute_path(self.command_processor.get_property('WorkingDir'), self.command_processor.expand_parameter_value(pv_SpatialDataFile, self))) # If the pv_GeoLayerID is a valid %-formatter, assign the pv_GeoLayerID the corresponding value. if pv_GeoLayerID in ['%f', '%F', '%E', '%P', '%p']: pv_GeoLayerID = io_util.expand_formatter(spatial_data_file_absolute, pv_GeoLayerID) # Run the checks on the parameter values. Only continue if the checks passed. if self.__should_read_geolayer(spatial_data_file_absolute, pv_GeoLayerID): try: # Create a QGSVectorLayer object with the GeoJSON SpatialDataFile. qgs_vector_layer = qgis_util.read_qgsvectorlayer_from_file(spatial_data_file_absolute) # Create a GeoLayer and add it to the geoprocessor's GeoLayers list. geolayer_obj = GeoLayer(geolayer_id=pv_GeoLayerID, geolayer_qgs_vector_layer=qgs_vector_layer, geolayer_source_path=spatial_data_file_absolute) self.command_processor.add_geolayer(geolayer_obj) # Raise an exception if an unexpected error occurs during the process. except Exception as e: self.warning_count += 1 message = "Unexpected error reading GeoLayer {} from GeoJSON file {}.".format(pv_GeoLayerID, pv_SpatialDataFile) recommendation = "Check the log file for details." self.logger.error(message, exc_info=True) self.command_status.add_to_log(CommandPhaseType.RUN, CommandLogRecord(CommandStatusType.FAILURE, message, recommendation)) # Determine success of command processing. Raise Runtime Error if any errors occurred if self.warning_count > 0: message = "There were {} warnings proceeding this command.".format(self.warning_count) logger.error(message) raise RuntimeError(message) # Set command status type as SUCCESS if there are no errors. else: self.command_status.refresh_phase_severity(CommandPhaseType.RUN, CommandStatusType.SUCCESS)
def run_command(self): """ Run the command. Read the tabular data from the Excel workbook/worksheet. Create a Table object, and add to the GeoProcessor's tables list. Returns: None. Raises: RuntimeError if any warnings occurred during run_command method. """ # Obtain the parameter values. pv_InputFile = self.get_parameter_value("InputFile") pv_Worksheet = self.get_parameter_value("Worksheet") pv_TableID = self.get_parameter_value("TableID") # Convert the InputFile parameter value relative path to an absolute path and expand for ${Property} syntax. file_absolute = io_util.verify_path_for_os( io_util.to_absolute_path( self.command_processor.get_property('WorkingDir'), self.command_processor.expand_parameter_value( pv_InputFile, self))) # If the pv_TableID is a valid %-formatter, assign the pv_GeoLayerID the corresponding value. if pv_TableID in ['%f', '%F', '%E', '%P', '%p']: pv_TableID = io_util.expand_formatter(file_absolute, pv_TableID) # Run the checks on the parameter values. Only continue if the checks passed. if self.__should_read_table(file_absolute, pv_Worksheet, pv_TableID): try: # Assign the Worksheet parameter to the name of the first Excel worksheet, if it was not specified. if pv_Worksheet is None: pv_Worksheet = pandas_util.create_excel_workbook_obj( file_absolute).sheet_names[0] # Assign the TableID parameter to the name of the first Excel worksheet, if it was not specified. if pv_TableID is None: pv_TableID = pv_Worksheet # Create a Pandas Data Frame object. df = pandas_util.create_data_frame_from_excel( file_absolute, pv_Worksheet) # Create a Table and add it to the geoprocessor's Tables list. table_obj = Table(pv_TableID, df, file_absolute) self.command_processor.add_table(table_obj) # Raise an exception if an unexpected error occurs during the process. except Exception as e: self.warning_count += 1 message = "Unexpected error reading Table {} from Excel file {}.".format( pv_TableID, pv_InputFile) recommendation = "Check the log file for details." self.logger.error(message, exc_info=True) self.command_status.add_to_log( CommandPhaseType.RUN, CommandLogRecord(CommandStatusType.FAILURE, message, recommendation)) # Determine success of command processing. Raise Runtime Error if any errors occurred if self.warning_count > 0: message = "There were {} warnings proceeding this command.".format( self.warning_count) raise RuntimeError(message) # Set command status type as SUCCESS if there are no errors. else: self.command_status.refresh_phase_severity( CommandPhaseType.RUN, CommandStatusType.SUCCESS)
def run_command(self): """ Run the command. Read the layer file from a delimited file, create a GeoLayer object, and add to the GeoProcessor's geolayer list. Returns: None. Raises: RuntimeError if any warnings occurred during run_command method. """ # Obtain the parameter values. pv_DelimitedFile = self.get_parameter_value("DelimitedFile") pv_Delimiter = self.get_parameter_value("Delimiter", default_value=',') pv_GeometryFormat = self.get_parameter_value("GeometryFormat") pv_XColumn = self.get_parameter_value("XColumn", default_value=None) pv_YColumn = self.get_parameter_value("YColumn", default_value=None) pv_WKTColumn = self.get_parameter_value("WKTColumn", default_value=None) pv_CRS = self.get_parameter_value("CRS") pv_GeoLayerID = self.get_parameter_value("GeoLayerID", default_value='%f') # Convert the DelimitedFile parameter value relative path to an absolute path and expand for ${Property} # syntax delimited_file_abs = io_util.verify_path_for_os( io_util.to_absolute_path( self.command_processor.get_property('WorkingDir'), self.command_processor.expand_parameter_value( pv_DelimitedFile, self))) # If the pv_GeoLayerID is a valid %-formatter, assign the pv_GeoLayerID the corresponding value. if pv_GeoLayerID in ['%f', '%F', '%E', '%P', '%p']: pv_GeoLayerID = io_util.expand_formatter(delimited_file_abs, pv_GeoLayerID) # Run the checks on the parameter values. Only continue if the checks passed. if self.__should_read_geolayer(delimited_file_abs, pv_Delimiter, pv_GeometryFormat, pv_XColumn, pv_YColumn, pv_WKTColumn, pv_CRS, pv_GeoLayerID): try: if pv_GeometryFormat.upper() == "XY": # Create a QGSVectorLayer object with the delimited file. qgs_vector_layer = qgis_util.read_qgsvectorlayer_from_delimited_file_xy( delimited_file_abs, pv_Delimiter, pv_CRS, pv_XColumn, pv_YColumn) else: # Create a QGSVectorLayer object with the delimited file. qgs_vector_layer = qgis_util.read_qgsvectorlayer_from_delimited_file_wkt( delimited_file_abs, pv_Delimiter, pv_CRS, pv_WKTColumn) # Create a GeoLayer and add it to the geoprocessor's GeoLayers list. geolayer_obj = GeoLayer(pv_GeoLayerID, qgs_vector_layer, delimited_file_abs) self.command_processor.add_geolayer(geolayer_obj) # Raise an exception if an unexpected error occurs during the process. except Exception as e: self.warning_count += 1 message = "Unexpected error reading GeoLayer {} from delimited file {}.".format( pv_GeoLayerID, pv_DelimitedFile) recommendation = "Check the log file for details." self.logger.error(message, exc_info=True) self.command_status.add_to_log( CommandPhaseType.RUN, CommandLogRecord(CommandStatusType.FAILURE, message, recommendation)) # Determine success of command processing. Raise Runtime Error if any errors occurred if self.warning_count > 0: message = "There were {} warnings proceeding this command.".format( self.warning_count) raise RuntimeError(message) # Set command status type as SUCCESS if there are no errors. else: self.command_status.refresh_phase_severity( CommandPhaseType.RUN, CommandStatusType.SUCCESS)
def run_command(self): """ Run the command. Read all spatial data files within the folder. For each desired spatial data file (can be specified by the Subset_Pattern parameter), create a GeoLayer object, and add to the GeoProcessor's geolayer list. Returns: None. Raises: RuntimeError if any warnings occurred during run_command method. """ # Obtain the parameter values. pv_SpatialDataFolder = self.get_parameter_value("SpatialDataFolder") pv_Subset_Pattern = self.get_parameter_value("Subset_Pattern") pv_GeoLayerID_prefix = self.get_parameter_value("GeoLayerID_prefix") # Convert the SpatialDataFolder parameter value relative path to an absolute path sd_folder_abs = io_util.verify_path_for_os( io_util.to_absolute_path(self.command_processor.get_property('WorkingDir'), self.command_processor.expand_parameter_value(pv_SpatialDataFolder, self))) # Run the initial checks on the parameter values. Only continue if the checks passed. if self.__should_read_folder(sd_folder_abs): # Determine which files within the folder should be processed. All files will be processed if # pv_Subset_Pattern is set to None. Otherwise only files that match the given pattern will be processed. # Check that each file in the folder is: # 1. a file # 2. a spatial data file (ends in .shp or .geojson) # 3. follows the given pattern (if Subset_Pattern parameter value does not equal None) if pv_Subset_Pattern: spatial_data_files_abs = [os.path.join(sd_folder_abs, source_file) for source_file in glob.glob(os.path.join(sd_folder_abs, pv_Subset_Pattern)) if os.path.isfile(os.path.join(sd_folder_abs, source_file)) and (source_file.endswith(".shp") or source_file.endswith(".geojson"))] else: spatial_data_files_abs = [os.path.join(sd_folder_abs, source_file) for source_file in os.listdir(sd_folder_abs) if os.path.isfile(os.path.join(sd_folder_abs, source_file)) and (source_file.endswith(".shp") or source_file.endswith(".geojson"))] # Iterate through the desired spatial data files for spatial_data_file_absolute in spatial_data_files_abs: # Determine the GeoLayerID. if pv_GeoLayerID_prefix: geolayer_id = "{}_{}".format(pv_GeoLayerID_prefix, io_util.expand_formatter(spatial_data_file_absolute, '%f')) else: geolayer_id = io_util.expand_formatter(spatial_data_file_absolute, '%f') # Run the secondary checks on the parameter values. Only continue if the checks passed. if self.__should_read_geolayer(geolayer_id): try: # Create a QGSVectorLayer object with the GeoJSON SpatialDataFile qgs_vector_layer = qgis_util.read_qgsvectorlayer_from_file(spatial_data_file_absolute) # Create a GeoLayer and add it to the geoprocessor's GeoLayers list geolayer_obj = GeoLayer(geolayer_id, qgs_vector_layer, spatial_data_file_absolute) self.command_processor.add_geolayer(geolayer_obj) # Raise an exception if an unexpected error occurs during the process except Exception as e: self.warning_count += 1 message = "Unexpected error reading GeoLayer {} from" \ " file {}.".format(geolayer_id, spatial_data_file_absolute) recommendation = "Check the log file for details." self.logger.error(message, exc_info=True) self.command_status.add_to_log(CommandPhaseType.RUN, CommandLogRecord(CommandStatusType.FAILURE, message, recommendation)) # Determine success of command processing. Raise Runtime Error if any errors occurred if self.warning_count > 0: message = "There were {} warnings proceeding this command.".format(self.warning_count) raise RuntimeError(message) # Set command status type as SUCCESS if there are no errors. else: self.command_status.refresh_phase_severity(CommandPhaseType.RUN, CommandStatusType.SUCCESS)