code: def create_memory_layer(
          layer_name, geometry, coordinate_reference_system=None, fields=None):
      
  
      if geometry == QgsWkbTypes.PointGeometry:
          wkb_type = QgsWkbTypes.MultiPoint
      elif geometry == QgsWkbTypes.LineGeometry:
          wkb_type = QgsWkbTypes.MultiLineString
      elif geometry == QgsWkbTypes.PolygonGeometry:
          wkb_type = QgsWkbTypes.MultiPolygon
      elif geometry == QgsWkbTypes.NullGeometry:
          wkb_type = QgsWkbTypes.NoGeometry
      else:
          raise MemoryLayerCreationError(
              'Layer geometry must be one of: Point, Line, '
              'Polygon or Null, I got %s' % geometry)
  
      if coordinate_reference_system is None:
          coordinate_reference_system = QgsCoordinateReferenceSystem()
      if fields is None:
          fields = QgsFields()
      elif not isinstance(fields, QgsFields):
          # fields is a list
          new_fields = QgsFields()
          for f in fields:
              new_fields.append(f)
          fields = new_fields
      memory_layer = QgsMemoryProviderUtils. \
          createMemoryLayer(name=layer_name,
                            fields=fields,
                            geometryType=wkb_type,
                            crs=coordinate_reference_system)
  
      memory_layer.dataProvider().createSpatialIndex()
      memory_layer.keywords = {
          'inasafe_fields': {}
      }
      return memory_layer
docs: Create a vector memory layer.
  
      :param layer_name: The name of the layer.
      :type layer_name: str
  
      :param geometry: The geometry of the layer.
      :rtype geometry: QgsWkbTypes (note:
                       from C++ QgsWkbTypes::GeometryType enum)
  
      :param coordinate_reference_system: The CRS of the memory layer.
      :type coordinate_reference_system: QgsCoordinateReferenceSystem
  
      :param fields: Fields of the vector layer. Default to None.
      :type fields: QgsFields
  
      :return: The memory layer.
      :rtype: QgsVectorLayer