code: def _add_id_column(layer):
      
      layer_purpose = layer.keywords['layer_purpose']
      mapping = {
          layer_purpose_exposure['key']: exposure_id_field,
          layer_purpose_hazard['key']: hazard_id_field,
          layer_purpose_aggregation['key']: aggregation_id_field
      }
  
      has_id_column = False
      for layer_type, field in list(mapping.items()):
          if layer_purpose == layer_type:
              safe_id = field
              if layer.keywords['inasafe_fields'].get(field['key']):
                  has_id_column = True
              break
  
      if not has_id_column:
          LOGGER.info(
              'We add an ID column in {purpose}'.format(purpose=layer_purpose))
  
          layer.startEditing()
  
          id_field = QgsField()
          id_field.setName(safe_id['field_name'])
          if isinstance(safe_id['type'], list):
              # Use the first element in the list of type
              id_field.setType(safe_id['type'][0])
          else:
              id_field.setType(safe_id['type'][0])
          id_field.setPrecision(safe_id['precision'])
          id_field.setLength(safe_id['length'])
  
          layer.addAttribute(id_field)
  
          new_index = layer.fields().lookupField(id_field.name())
  
          for feature in layer.getFeatures():
              layer.changeAttributeValue(
                  feature.id(), new_index, feature.id())
  
          layer.commitChanges()
  
          layer.keywords['inasafe_fields'][safe_id['key']] = (
              safe_id['field_name'])
docs: Add an ID column if it's not present in the attribute table.
  
      :param layer: The vector layer.
      :type layer: QgsVectorLayer